3

我有一个叫做FooBean的抽象超类bean,我所有的其他bean都扩展了这个类。我已经在applicationContext文件中连接了我的dao,并且我想将它注入到这个超类bean中,这样每次我进入一个bean时,我都可以访问dao。问题是,每当我尝试访问子类中的dao时,我都会得到NullPointerException。下面的文件显示只有春天设置的相关配置细节:Spring bean没有被注入JSF托管bean(这是一个抽象的超类)

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> 
    

    事情我已经没有成功尝试@ManagedProperty(值=“#{佛oService}“)在FooBean类字段上。

  • 试图限定faces-config.xml中的Bean注入弹簧豆:

    com.foo.beans.FooBean 无 FooService接口 #{FooService接口}

  • 定义具有空指针的子类bean作为spring bean,并通过spring beans xml文件注入属性。

讽刺的是,我能够把下面的代码在FooBean抽象类的构造函数和DAO注入(但我不想使用此代码,我想春天来注入的话):

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(getRequest().getServletContext()); 
     dao = (FooService)ctx.getBean("fooService"); 
+0

任何人有什么想法?我尝试了所有我能想到的。 – Adam 2011-04-15 00:00:30

回答

1

(我会写这个的评论,但我没有为它足够的声誉)

如果使用@ManagedProperty,那么你也应该申报它@ManagedBean。

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

0

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