Java Can';t正确加载弹簧上下文
我在做什么:我正在将我的Spring应用程序外部JAR加载到另一个非Spring应用程序中。我是这样做的:Java Can';t正确加载弹簧上下文,java,spring,service,javabeans,applicationcontext,Java,Spring,Service,Javabeans,Applicationcontext,我在做什么:我正在将我的Spring应用程序外部JAR加载到另一个非Spring应用程序中。我是这样做的: ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/application-context.xml"); MyService myService = (MyService) ap.getBean("myBusinessService"); 这是我收到的例外情况: or
ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/application-context.xml");
MyService myService = (MyService) ap.getBean("myBusinessService");
这是我收到的例外情况:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBusinessService': Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.test.domain.dao.MyDAO com.test.domain.service.impl.MyBusinessService.viaggioDAO; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.test.domain.dao.MyDAO] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
这些罐子中的包装是:
- 服务接口的com.test.domain.service
- com.test.domain.services.impl用于服务实现
- 用于dao接口的com.test.domain.dao
- com.test.domain.dao.impl用于dao实现
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- Spring IoC Context -->
<context:component-scan base-package="com.test" />
<import resource="root-config.xml" />
<import resource="classpath:/root-context.xml" />
<!-- Enables the Spring MVC @Controller programming model -->
<mvc:annotation-driven />
</beans>
MyBusinessService.java
@Service(value="myBusinessService")
public class MyBusinessService implements MyService {
@Autowired
private MyDAO myDAO;
@Override
public List<Stuff> getAllStuff() throws SQLException {
List<Stuff> stuff = this.myDAO.findAllStuff();
return stuff;
}
}
@Service(value=“myBusinessService”)
公共类MyBusinessService实现MyService{
@自动连线
私人密道密道;
@凌驾
public List getAllStuff()引发SQLException{
List stuff=this.myDAO.findAllStuff();
归还物品;
}
}
这是可视性问题,请检查是否已在dispatcher servlet.xml
中列出所有包。加上
<context:component-scan base-package="com.test.domain" />
使调度员可以看到您的所有包
还要确保您尝试自动连线的bean应该具有有效的限定符,如@Component
或@Service
如果您使用的是
xml
配置,请确保在dispatcher中定义bean。在自动连接之前我的spring jar不是一个web应用程序,但我的新应用程序将在一个cointainer(即tomcat)中运行。我所有的服务和DAO都得到了它们的相对服务或存储库注释。甚至组件扫描都是在我的xml上完成的。“在自动连接之前在dispatcher中定义bean”是什么意思?请告诉我们@Repository
注释是在DAO接口还是DAO实现中给出的?它是在DAO实现中给出的。服务一也在服务实现中。@AbierTo您可以与我们共享application-context.xml和com.test.domain.Service.impl.MyBusinessService.java类吗。@erhun添加了这两个文件。@AbierTo您在哪里扫描了这个包“com.test.domain.dao.MyDAO”?是否在root-config.xml或root-context.xml上存在任何上下文:组件扫描?因为在application-context.xml中没有上下文:如我所见,为com.test.domain.dao.MyDAO定义了组件扫描。