我有一个名为FooBean的抽象超类bean,所有其他bean都扩展了这个类。我已经在applicationContext文件中连接了我的dao,我想把它注入到这个超类bean中,这样每次我在一个bean中,我都可以访问dao。问题是,每当我试图访问子类中的dao时,都会得到NullPointerException。以下文件仅显示了spring设置的相关配置细节:
web . xml:
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<context-param>
<description>Spring configuration files location.</description>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/applicationContext-*.xml</param-value>
</context-param>
faces-config.xml:
<application>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
...
</application>
FooBean.java:
public abstract class FooBean implements Serializable {
protected transient FooService dao;
public void setFooService(FooService dao){
this.dao = dao;
}
}
applicationContext-service.xml:
<bean id="serviceTemplate" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
<property name="transactionManager" ref="transactionManager"/>
<property name="transactionAttributes">
<props>
<prop key="*"/>
<!--
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="remove*">PROPAGATION_REQUIRED</prop>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
-->
</props>
</property>
</bean>
<!-- Definition of service targets -->
<bean id="fooServiceTarget" class="com.foo.service.FooService">
<property name="fooDAO" ref="fooDAO"/>
<property name="adminDAO" ref="adminDAO"/>
<property name="channelsDAO" ref="channelsDAO"/>
</bean>
<bean id="fooService" parent="serviceTemplate">
<property name="target" ref="fooServiceTarget"/>
</bean>
applicationContext-hibernate.xml:
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/FooDataSource"/>
</bean>
<!-- Hibernate SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<!--value>com/Foo/model/General.hbm.xml</value-->
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.bytecode.use_reflection_optimizer">true</prop>
<prop key="hibernate.search.default.directory_provider">filesystem</prop>
<prop key="hibernate.search.default.indexBase">/usr/lucene/indexes</prop>
<!--prop key="hibernate.cache.provider_class">org.hibernate.cache.SwarmCacheProvider</prop-->
</props>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Spring Data Access Exception Translator Defintion -->
<bean id="jdbcExceptionTranslator" class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator"/>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
<property name="jdbcExceptionTranslator" ref="jdbcExceptionTranslator"/>
</bean>
<!-- Add DAO's here and create separate application context for service -->
<bean id="FooDAO" class="com.foo.service.dao.impl.FooDAOHibernate">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean id="adminDAO" class="com.foo.service.dao.impl.AdminDAOHibernate">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
<bean id="channelsDAO" class="com.foo.service.dao.impl.ChannelsDAOHibernate">
<property name="hibernateTemplate" ref="hibernateTemplate"/>
</bean>
我已经尝试过但没有成功的事情:
- 尝试在FooBean类字段上使用@ManagedProperty(value="#{FooService}")
尝试在faces-config.xml中定义bean以注入spring bean:
com.foo.beans.FooBean没有一个fooService# {fooService}
定义具有空指针的子类bean作为spring bean,并通过spring beans xml文件注入该属性。
具有讽刺意味的是,我可以在FooBean抽象类的构造函数中放置以下代码并注入dao(但我不想使用这段代码,我希望spring注入它):
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getRequest().getServletContext());
dao = (FooService)ctx.getBean("fooService");
(我本来想把这篇文章作为评论,但我没有足够的声誉)
如果你使用了@ManagedProperty,那么你也应该声明它为@ManagedBean。
而且,我没有在任何applicationContext.xml文件中看到FooBean,所以我不明白Spring如何"知道"它
相信这与jsf bean的使用方式有关。当请求一个新bean时,由于在部署时实例化了Spring inject,因此不会注入dao。它在构造函数中工作的原因是bean是为请求创建的,然后你获得应用程序上下文并抓取dao Singleton。