无法正确加载Spring Context

时间:2015-01-30 12:13:01

标签: 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");

这是我收到的例外情况:

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 for service Interfaces
  • com.test.domain.services.impl 用于服务实施
  • com.test.domain.dao for DAO interfaces
  • 用于DAO实现的
  • com.test.domain.dao.impl

问题:为什么我收到此错误?

编辑:有关我的应用的更多信息。

应用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;
    }
}

1 个答案:

答案 0 :(得分:0)

这是可见性问题,请检查您是否列出了dispatcher-servlet.xml中的所有包裹。添加,

<context:component-scan base-package="com.test.domain" />

让您的调度员可以看到您的所有包裹

同时确保您尝试自动装配的bean具有有效限定符@Component@Service

如果您使用xml配置,请确保在调度程序中定义bean。在自动装配之前

相关问题