[英]Problem with using spring bean from eagerly-fetched JSF managed bean in Glassfish
[英]problem with using spring beans from jsf managed bean
我的Web应用程序使用jsf 2.0和spring 3.0,但问题是:jsf受管bean不能通过依赖注入使用spring bean有我的配置文件:
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>
弹簧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";
}
}
当我运行Web应用程序并调用控制器操作变量“ hibernateUtil”为null时,没有例外。 但是我可以使用以下方法得到春豆:
hibernateUtil = (HibernateUtil) FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("hibernateUtil");
我如何使用依赖注入获得这个Spring bean?
为什么不在spring-beans.xml中声明人脸托管bean?
也许您还需要在faces-config中添加它:
<application>
<view-handler>com.sun.facelets.FaceletViewHandler</view-handler>
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
</application>
我对此不是100%肯定,因为我现在不在我的工作站上。 但是我认为它必须与上面类似。
因此,您只需将“ CalcBean”移动到spring-beans.xml并像以前一样使用它。
org.springframework.web.jsf.el.SpringBeanFacesELResolver
有问题。 本文( http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely/ )提出了一种更好的解决方案。
另外,请看一下我的以下答案: 如何在JSF管理的bean中使用Spring服务?
您缺少的是JSF依赖项注入了解Spring及其bean的方法。 只需将其添加到您的faces-config.xml
:
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
非常感谢您的回答。 这是我的问题的解决方案:我更改行:
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
至:
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
但是不一定要在spring-beans.xml中添加CalculatorBean
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.