0

我正在使用Spring4。 我有3個類:MyController,ADao和BDao。Spring依賴注入失敗

viewAs()方法在MyController類中調用ADao中的getAs()方法, getao()方法從ADao調用BDao中的getB()方法。

ADao類中的SessionFactory對象被注入,但BDao類中的sessionFactory對象沒有被注入。 我的問題是爲什麼它沒有被注入?由於BDao類中sessionFactory對象爲null,所以我得到Null指針異常。

是因爲我從另一個dao打電話給一個dao嗎?

@Controller 
public class MyController { 
    @Autowired 
    private ADao aDao; 

    @RequestMapping(value="viewAllItems") 
    public String viewAs(HttpServletRequest req, HttpServletResponse res){ 
     List<Item> list = new ArrayList<Item>(); 
     list = aDao.getAs(); 
     return ""; 
    } 
} 

@Repository 
public class ADao { 
    @Autowired 
    private SessionFactory sessionFactory;//objected gets injected. 

    public ADao(){} 

    public List<A> getAs() throws HibernateException{ 
     session = sessionFactory.openSession(); 
     tx = session.beginTransaction(); 
     new B().getB(null); 

     return null; 
    } 
} 

@Repository 
public class BDao { 

    @Autowired 
    private SessionFactory sessionFactory; 

    private Session session; 

    public BDao(){} 

    public void getB(B b) throws HibernateException{ 
     session = sessionFactory.openSession();// Object does not get injected. Causes NullPointerException 
    } 
} 

編輯: 調度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
         http://www.springframework.org/schema/context 
         http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- JSR-303 support will be detected on classpath and enabled automatically --> 
    <mvc:annotation-driven/> 

     <context:component-scan base-package="com.karmacrafts.web.controller" /> 
     <context:component-scan base-package="com.karmacrafts.model.dao"/> 
     <context:property-placeholder location="classpath:application.properties" /> 
     <context:annotation-config/> 

     <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
      <property name="viewClass" 
       value="org.springframework.web.servlet.view.JstlView" /> 
      <property name="prefix" value="/WEB-INF/jsp/" /> 
      <property name="suffix" value=".jsp" /> 
     </bean> 


      <bean id="sessionFactory" 
      class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
       <property name="dataSource" ref="dataSource" /> 
       <property name="packagesToScan" value="com.karmacrafts.model.impl" /> 
       <property name="hibernateProperties"> 
       <props> 
        <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       </props> 
       </property> 
      </bean> 

      <bean id="dataSource" 
      class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> 
       <property name="driverClassName" value="${jdbc.driverClassName}" /> 
       <property name="url" value="${jdbc.databaseurl}" /> 
       <property name="username" value="${jdbc.username}" /> 
       <property name="password" value="${jdbc.password}" /> 
      </bean> 

      <bean id="transactionManager" 
      class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
       <property name="sessionFactory" ref="sessionFactory" /> 
      </bean> 

      <bean id="persistenceExceptionTranslationPostProcessor" 
      class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 


     <bean id="ADao" class="com.ADao" /> 
     <bean id="BDao" class="com.BDao"/> 
</beans> 
+0

請更新與應用程序上下文XML文件,你的問題。 – 2014-09-02 21:34:25

+4

在新B中使用new運算符的時刻()。getB(null); - 它不再是一個spring管理bean,所以B中的自動裝入sessionFactory將不起作用。 – Prasad 2014-09-02 21:37:28

+0

@Prasad這似乎是正確的。謝謝。如果你可以重新發表你的評論作爲答案,我會接受它。 – Susie 2014-09-02 21:43:50

回答

1

在ADAO類getAs()方法替換new B(),您正在使用新的運營商作爲

new B().getB(null); 

,這是不是一個春天的託管Bean。所以自動裝配將無法獲取注入到BDao類中的sessionFactory。

相反,你可以通過爲自動裝配在ADAO注入BDAO:

@Repository 
public class ADao { 
    @Autowired 
    private BDao bdao;//use this to call getB method 
    ... 
} 
1

添加在ADAO類如下:

**@Autowired 

private BDao bdao;//objected gets injected.** 

而且使用這個對象調用BDAO方法,而比使用新運營商

1

當你說new B()你從Spring上下文。你自己創建了一個bean,它沒有從spring上下文注入任何東西。與context.getBean()

或者自動裝配BDAO在ADAO