Spring bean没有注入JSF托管bean(这是一个抽象的超类)

时间:2011-04-14 23:00:26

标签: spring jsf dependency-injection ioc-container code-injection

我有一个名为FooBean的抽象超类bean,我的所有其他bean都扩展了这个类。我已经在applicationContext文件中连接了我的dao,我想将它注入到这个超类bean中,这样每次我进入bean时,我都可以访问dao。问题是,每当我尝试访问子类中的dao时,我都会得到一个NullPointerException。以下文件仅显示弹簧设置的相关配置详细信息:

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

面-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");

2 个答案:

答案 0 :(得分:1)

(我会把它写成评论,但我没有足够的声誉)

如果你使用了@ManagedProperty,那么你也应该将它声明为@ManagedBean。

另外,我没有在任何applicationContext.xml文件中看到FooBean,所以我不知道Spring会如何“知道”它。

答案 1 :(得分:0)

相信这与jsf bean的使用方式有关。当请求新bean时,由于Spring Injects在部署时被实例化,因此不会注入dao。它在构造函数中工作的原因是为请求创建了bean,然后你得到了应用程序上下文并抓住了dao Singleton。