java - 从 jsf 托管 bean 使用 spring bean 的问题

标签 java spring jsf dependency-injection jsf-2

我有一个使用 jsf 2.0 和 spring 3.0 的网络应用程序 问题是:jsf managed beans can't use spring beans using dependency injection 有我的配置文件:

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>

计算 Controller :

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";
}

当我运行我的网络应用程序并调用我的 Controller 操作时,变量“hibernateUtil”为空并且没有异常。但是我可以使用以下方法获取 spring beans:

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

如何使用依赖注入(inject)获取这个 spring bean?

最佳答案

为什么不在 spring-beans.xml 中声明 faces managed 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 并像以前一样使用它。

关于java - 从 jsf 托管 bean 使用 spring bean 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4913743/

相关文章:

java - 找出数字中位数并打印出来的更快方法?

java - Spring Boot-抛出错误并停止应用程序根据某些条件启动

java - JBoss AS 7.1.1 没有获取我的 JSF 实现

java - 解析传入的 JSON 并删除包装样板

java - Restful 服务端点为 ResponseEntity< HashMap<String, Serialized>> 生成 XML 响应

image - GlassFish 替代文档根路径应该如何在 h :graphicImage? 中显示图像

css - 对齐 h :outputText inside a p:datatable

java - Solr 长多值字段不起作用

java - 如何创建 Jahia Ajax 调用(JSP、JQuery)

JavaFX - 在表格 View 中移动列