JSF2.0 與Spring 3.0 集成
同以前的JSF1.2與Spring2.5集成類似,只是有一些類名的變化。
web.xml 代碼如下:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
<listener-class> org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
Faces-config.xml中加入:
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
JSF1.2和1.2以前是加入
<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>
Spring 的配置文件就正常配置就可以了。
ApplicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!--
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="WEB-INF/jdbc.properties" /> </bean>
-->
<!-- hibernate sessionFactory -->
<context:annotation-config/>
<context:component-scan base-package="cn.xiangyunsoft" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="hibernateProperties" value="classpath:hibernate.properties" />
<property name="configLocations">
<list>
<!-- 使用hibernate.cfg.xml配置文件 -->
<value>classpath:hibernate.cfg.xml
</value>
</list>
</property>
</bean>
<!-- 配置事務(wù)管理 -->
<!-- 事務(wù)通知類 -->
<!--
<bean id="profiler"
class="cn.xiangyunsoft.business.service.SimpleProfiler"> order
值可以決定通知的先后順序 ,與后面的order的值的大小,決定了是先通知再執(zhí)行,還是先執(zhí)行再通知 <property
name="order" value="2" /> </bean>
-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<aop:config>
<!-- 此處的IService 是表示對(duì)所有實(shí)現(xiàn)IService接口的類管理事務(wù) -->
<aop:advisor
pointcut="execution(* cn.xiangyunsoft.*.service..*ServiceImpl.*(..))"
advice-ref="txAdvice" />
<!--
加入之后事務(wù)不起作用> <aop:aspect id="profilingAspect" ref="profiler">
<aop:pointcut id="serviceMethodWithReturnValue"
expression="execution(*
cn.xiangyunsoft.*.service..*ServiceImpl.*(..))" />
<aop:after-throwing method="profile"
pointcut-ref="serviceMethodWithReturnValue" /> </aop:aspect
-->
</aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- the transactional semantics
-->
<tx:attributes>
<!-- 以get、find、load開頭的方法是只讀事務(wù) -->
<tx:method name="*" read-only="true" />
<!--<tx:method name="find*" read-only="true" />-->
<!--<tx:method name="load*" read-only="true" />-->
<!-- 其他方法是默認(rèn),事務(wù)隔離級(jí)別為:保證一個(gè)事務(wù)修改的數(shù)據(jù)提交后才能被另外一個(gè)事務(wù)讀取 -->
<tx:method name="save*" isolation="REPEATABLE_READ"
propagation="REQUIRED" />
<tx:method name="delete*" isolation="REPEATABLE_READ"
propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
</beans>
一個(gè)注入Spring bean 的 JSF bean 代碼如下:
@ManagedBean(name = ClassItemBean.NAME)
public class ClassItemBean {
public static final String NAME = "classItemBean";
/*
*在spring 中配置的service.
*/
@ManagedProperty(name = "classItemService", value = "#{classItemService}")
private ClassItemService classItemService;
public void setClassItemService(ClassItemService classItemService) {
this.classItemService = classItemService;
}
public String hello() {
System.out.println("hello." + classItemService);
Object obj = classItemService.get(ClassItem.class, "01");
System.out.println("obj = " + obj);
return null;
}
}
這樣集成就完畢了。很簡單,很強(qiáng)大。
posted on 2010-04-24 15:03
Libo 閱讀(2858)
評(píng)論(2) 編輯 收藏 所屬分類:
Spring 、
JSF 2