[英]Dependency injection with Spring fails
我正在使用Spring4。 我有3類:MyController,ADAo和BDao。
MyController類中的viewAs()方法調用ADao中的getAs()方法,ADAo中的getAs()方法調用BDao中的getB()方法。
注入ADao類中的SessionFactory對象,但不注入BDao類中的sessionFactory對象。 我的問題是為什么不注射? 我收到Null指針異常,因為sessionao對象在BDao類中為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
}
}
編輯: dispatcher-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>
在ADAO類中添加以下內容:
**@Autowired
private BDao bdao;//objected gets injected.**
並使用此對象來調用BDao方法,而不是使用new運算符
當您說new B()
您將退出Spring Context。 您自己創建了一個bean,它將不會從spring上下文注入任何東西。 用context.getBean()
替換new B()
context.getBean()
或在您的ADao中自動連線BDao
在ADao類的getAs()方法中,您正在使用new運算符作為
new B().getB(null);
它不是Spring管理的bean。 因此,自動裝配對在BDao類中注入sessionFactory無效。
相反,您可以通過以下方式自動裝配將BDao注入ADao:
@Repository
public class ADao {
@Autowired
private BDao bdao;//use this to call getB method
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.