Spring的依赖注入失败

时间:2014-09-02 21:33:09

标签: java spring spring-mvc web-applications dependency-injection

我正在使用Spring4。 我有3个类:MyController,ADao和BDao。

MyController类中的viewAs()方法在ADao中调用getAs()方法, 来自ADao的getAs()方法在BDao中调用getB()方法。

ADao类中的SessionFactory对象被注入但是BDao类中的sessionFactory对象没有被注入。 我的问题是为什么不注射?我得到Null指针异常,因为在BDao类中sessionFactory对象为null。

是因为我从另一个人那里打电话给一个人吗?

@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>

3 个答案:

答案 0 :(得分:1)

在ADAO课程中添加以下内容:

**@Autowired

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

并使用此对象调用BDao方法而不是使用new运算符

答案 1 :(得分:1)

当你说new B()你离开Spring Context时。您已经创建了一个bean,它不会从spring上下文中注入任何内容。将new B()替换为context.getBean()

或在您的ADao中自动装配BDao

答案 2 :(得分:1)

在ADao类的getAs()方法中,您使用new运算符作为

    new B().getB(null);

它不是一个Spring托管bean。因此,自动装配将无法在BDao类中注入sessionFactory。

相反,您可以通过自动装配将ADao注入ADao中:

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