JSF2.0 与Spring 3.0 集成

JSF2.0 与Spring 3.0 集成

JSF2.0 与Spring 3.0 集成

同以前的JSF1.2Spring2.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.21.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 >
    
<!--  配置事务管理  -->
    
<!--  事务通知类  -->
    
<!--
        <bean id="profiler"
        class="cn.xiangyunsoft.business.service.SimpleProfiler"> order
        值可以决定通知的先后顺序 ,与后面的order的值的大小,决定了是先通知再执行,还是先执行再通知 <property
        name="order" value="2" /> </bean>
    
-->
    
< bean  id ="transactionManager"
        class
="org.springframework.orm.hibernate3.HibernateTransactionManager" >
        
< property  name ="sessionFactory"  ref ="sessionFactory"   />
    
</ bean >
    
< aop:config >
        
<!--  此处的IService 是表示对所有实现IService接口的类管理事务  -->
        
< aop:advisor
            
pointcut ="execution(* cn.xiangyunsoft.*.service..*ServiceImpl.*(..))"
            advice-ref
="txAdvice"   />
        
<!--
            加入之后事务不起作用> <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开头的方法是只读事务  -->
            
< tx:method  name ="*"  read-only ="true"   />
            
<!-- <tx:method name="find*" read-only="true" /> -->
            
<!-- <tx:method name="load*" read-only="true" /> -->
            
<!--  其他方法是默认,事务隔离级别为:保证一个事务修改的数据提交后才能被另外一个事务读取  -->
            
< tx:method  name ="save*"  isolation ="REPEATABLE_READ"
                propagation
="REQUIRED"   />
            
< tx:method  name ="delete*"  isolation ="REPEATABLE_READ"
                propagation
="REQUIRED"   />
                
        
</ tx:attributes >
    
</ tx:advice >
</ beans >


一个注入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 ;
    }
}


这样集成就完毕了。很简单,很强大。




你可能感兴趣的:(JSF2.0 与Spring 3.0 集成)