无法正确加载 Spring 上下文
Can't load Spring Context correctly
我在做什么:我正在将我的 Spring 应用程序外部 JAR 加载到另一个非 spring 应用程序中。我是这样做的:
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 用于服务实现
- com.test.domain.dao 用于 DAO 接口
- com.test.domain.dao.impl 用于 DAO 实现
问题:为什么我收到这个错误?
编辑:关于我的应用程序的更多信息。
申请-context.xml
<?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;
}
}
这是可见性问题,请检查您是否列出了dispatcher-servlet.xml
中的所有包。添加,
<context:component-scan base-package="com.test.domain" />
让您的调度员可以看到您的所有包裹
还要确保,您尝试自动装配的 bean 应该具有 @Component
或 @Service
的有效限定符
如果您正在使用 xml
配置,请确保您在调度程序中定义了 bean。在自动装配之前
我在做什么:我正在将我的 Spring 应用程序外部 JAR 加载到另一个非 spring 应用程序中。我是这样做的:
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 用于服务实现
- com.test.domain.dao 用于 DAO 接口
- com.test.domain.dao.impl 用于 DAO 实现
问题:为什么我收到这个错误?
编辑:关于我的应用程序的更多信息。
申请-context.xml
<?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;
}
}
这是可见性问题,请检查您是否列出了dispatcher-servlet.xml
中的所有包。添加,
<context:component-scan base-package="com.test.domain" />
让您的调度员可以看到您的所有包裹
还要确保,您尝试自动装配的 bean 应该具有 @Component
或 @Service
如果您正在使用 xml
配置,请确保您在调度程序中定义了 bean。在自动装配之前