簡體   English   中英

無法正確加載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。 自動接線之前

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM