简体   繁体   English

无法正确加载Spring上下文

[英]Can't load Spring Context correctly

What I'm doing : I'm loading my Spring application external JAR into another non-spring application. 我正在做什么 :我正在将Spring应用程序外部JAR加载到另一个非Spring应用程序中。 I'm doing it like this: 我正在这样做:

ApplicationContext ap = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/application-context.xml");
MyService myService = (MyService) ap.getBean("myBusinessService");

this is the exception that I receive: 这是我收到的例外:

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.

Package inside those jar are: 这些罐子里的包装是:

  • com.test.domain.service for service Interfaces com.test.domain.service用于服务接口
  • com.test.domain.services.impl for service implementations com.test.domain.services.impl用于服务实现
  • com.test.domain.dao for DAO interfaces com.test.domain.dao用于DAO接口
  • com.test.domain.dao.impl for DAO implementation com.test.domain.dao.impl用于DAO实施

Question : Why am I receiving this error? 问题 :为什么会收到此错误?

EDIT: more info about my app. 编辑:有关我的应用程序的更多信息。

application-context.xml 应用程序的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 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;
    }
}

It is the visiblity problem , check whether you have listed all packages in your dispatcher-servlet.xml . 这是可见性问题,请检查是否已在dispatcher-servlet.xml列出了所有软件包。 Add, 加,

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

to make all your package visible to your dispatcher 使所有包裹对调度员可见

Also ensure that , the bean you are trying to autowire shoud have valid qualifier as @Component or @Service 还要确保您尝试自动装配的Bean具有有效的限定符,例如@Component@Service

If you are using the xml configuration make sure , you define the bean in the dispatcher . 如果要使用xml配置,请确保在调度程序中定义bean。 Before autowiring it 自动接线之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM