2
votes

I have web application wich uses jsf 2.0 and spring 3.0 The problem is that: jsf managed beans can't use spring beans using dependency injection There are my config files:

web.xml:

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!-- Faces Servlet -->
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup> 1 </load-on-startup>
</servlet>

<!-- Faces Servlet Mapping -->
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/calc/*</url-pattern>
</servlet-mapping>


<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-beans.xml</param-value>
</context-param>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>

faces-config.xml:

<application>
  <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>


<managed-bean>
    <managed-bean-name>CalcBean</managed-bean-name>
    <managed-bean-class>timur.org.bean.CalculatorConroller</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
    <managed-property>
        <property-name>hibernateUtil</property-name>
        <value>#{hibernateUtil}</value>
    </managed-property>
</managed-bean>

spring-beans.xml:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:lang="http://www.springframework.org/schema/lang"
   xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
   http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
   http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="org.postgresql.Driver"/>
    <property name="url" value="jdbc:postgresql://localhost/timur"/>
    <property name="username" value="postgres"/>
    <property name="password" value="postgres"/>
</bean>

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="mappingResources">
        <list>
            <value>/mapping/service.xml</value>
            <value>/mapping/city.xml</value>
            <value>/mapping/timurovec.xml</value>
            <value>/mapping/client.xml</value>
            <value>/mapping/calendar.xml</value>
            <value>/mapping/order.xml</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
        </props>
    </property>

</bean>

<bean id="hibernateUtil" class="timur.org.util.HibernateUtil">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven>

CalculateController:

public class CalculatorConroller {

private HibernateUtil hibernateUtil;


public void setHibernateUtil(HibernateUtil hibernateUtil) {
    this.hibernateUtil = hibernateUtil;
}

public String action() {

    hibernateUtil.createAndStoreEvent("",new Date());
    List<CityDomain> lc = hibernateUtil.getList();
    for (int i=0; i<lc.size(); i++){
        LogManager.getLogger(this.getClass()).debug(lc.get(i).getName());
    }

    return "success";
}

}

when i run my web application and call my controller action variable 'hibernateUtil' is null and there no exceptions. But i can get spring beans using:

       hibernateUtil = (HibernateUtil) FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("hibernateUtil");

How can i get this spring bean using dependency injection?

4

4 Answers

1
votes

Why don't you declare your faces managed bean in your spring-beans.xml ?

Maybe you will also need to add this in your faces-config:

<application>
    <view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
    <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>

I am not 100 percent sure about this though because I am not at my workstation right now. But I think it has to be like the above.

So then you just move your "CalcBean" to spring-beans.xml and use it as before.

0
votes

What you're missing is a way for JSF dependency injection to know about Spring and its beans. Just add this to your faces-config.xml:

<application>
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
0
votes

thanks very much for your answers. It was solution of my problem: i change row:

 <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

to:

<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>

but it was not necessarily to add CalculatorBean to spring-beans.xml Thanks

0
votes

org.springframework.web.jsf.el.SpringBeanFacesELResolver is problematic. This article (http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/) proposes a better solution.

Also, take a look at this answer of mine: How to use Spring services in JSF-managed beans?