簡體   English   中英

Spring bean已創建但未自動裝配

[英]Spring beans created but not autowired

我知道有很多類似的問題。 我看了很多,但問題仍然存在。

我有一個已創建但未自動裝配的服務。 在項目和測試中都沒有(!)手動啟動(就像在這個問題中一樣

Tomcat輸出說,這兩個bean都被發現並創建。 至少第一項服務不會注入應有的位置。 我不知道第二個。

服務(接口):

public interface SchoolService {
  public School getSchool(String id);
}

服務(實施):

@Service
@Transactional
public class SchoolServiceImpl implements SchoolService {

  @Autowired
  private SchoolDAO schoolDAO;

  public School getSchool(String id) {
    //database things
    return school;
  }

}

它被“召喚”的地方

public class SchoolMenu implements Serializable {

  @Autowired
  private SchoolService schoolService;

  public SchoolMenu () {
    //here schoolService is null
    School school = schoolService.getSchool("id");
  }

}

應用程序的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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">


    <bean id="SchoolServiceImpl" class="content_management.School.service.SchoolServiceImpl"/>
    <bean id="SchoolDAOImpl" class="content_management.School.dao.SchoolDAOImpl"/>
</beans>

Tomcat輸出:

org.springframework.beans.factory.support.DefaultListableBeanFactory: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@57003970: defining beans [SchoolServiceImpl,SchoolDAOImpl]; root of factory hierarchy
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating shared instance of singleton bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating instance of bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Eagerly caching bean 'SchoolServiceImpl' to allow for resolving potential circular references
org.springframework.beans.factory.support.DefaultListableBeanFactory: Finished creating instance of bean 'SchoolServiceImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating shared instance of singleton bean 'SchoolDAOImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Creating instance of bean 'SchoolDAOImpl'
org.springframework.beans.factory.support.DefaultListableBeanFactory: Eagerly caching bean 'SchoolDAOImpl' to allow for resolving potential circular references
org.springframework.beans.factory.support.DefaultListableBeanFactory: Finished creating instance of bean 'SchoolDAOImpl'

我的錯誤在哪里?

為了讓@Autowired工作, SchoolMenu也必須是一個Spring bean。 如果不是,您可以從應用程序上下文獲取schoolService 像這樣的東西

ApplicationContext appContext = new ClassPathXmlApplicationContext("application-context.xml");
SchoolService schoolService = (SchoolService) appContext.getBean(SchoolService.class);

您正在schoolService的構造函數中訪問SchoolMenu

public SchoolMenu () {
    //here schoolService is null
    School school = schoolService.getSchool("id");
}

那時, schoolService尚未注入。 它不可能 - 在調用構造函數之前不會發生任何事情。

通過使用javax.annotation.PostConstruct注釋對bean進行批注,可以在初始化bean之后聲明要調用的方法。 執行需要在該方法中注入依賴項的操作,而不是在構造函數中。

(還有一些其他方法可以實現相同的功能,例如實現InitializingBean接口,但這些方法有點過時)

例:

public SchoolMenu () {
}

@PostConstruct
public void init() {
    // schoolService is NOT null here
    School school = schoolService.getSchool("id");
}

您可以通過將此配置添加到應用程序上下文來使用組件掃描,而不是在xml中定義bean,Spring將自動掃描並檢測bean。

<context:component-scan base-package="content_management.School" />

您還需要在上下文命名空間中添加,以便application-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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 <context:component-scan base-package="content_management.School" />

 </beans>

暫無
暫無

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

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