注册 登录
编程论坛 J2EE论坛

使用main方法通过ClassPathXmlApplicationContext调用service接口出错

小萍子 发布于 2016-09-21 20:07, 4319 次点击
我不通过controller层调用service接口,而是使用main方法通过ClassPathXmlApplicationContext调用service接口,
会报No unique bean of type [com.ultrapower.demo.service.impl.AppServiceImpl] is defined: expected single bean but found 0:
main方法代码如下:
public static void main(String[] args) {
        String path="D:\\software\\apache-tomcat-6.0.45\\webapps\\QuartzTest\\WEB-INF\\conf\\beans\\*.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:"+path);
        IAppService iAppService = applicationContext.getBean(AppServiceImpl.class);
        String appStr = iAppService.getApp();
        System.out.println(appStr);
    }
dao的配置
<bean id="iAppDao" class="com.ultrapower.demo.dao.impl.AppDaoImpl" parent="baseDao">
    </bean>
service的配置:
<bean id="iAppService" class="com.ultrapower.demo.service.impl.AppServiceImpl"
        parent="baseService" >
        <property name="iAppDao" ref="iAppDao" />
    </bean>
报错:log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ultrapower.demo.service.impl.AppServiceImpl] is defined: expected single bean but found 0:
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:269)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1083)
    at test.springtest.SpringTest1.main(SpringTest1.java:21)
1 回复
#2
枫xby2016-12-31 13:05
IAppService iAppService = applicationContext.getBean(AppServiceImpl.class); 改成IAppService iAppService = applicationContext.getBean("iAppService");
1