<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    想飛就別怕摔

    大爺的并TM罵人

    用annotation來配置spring2.5+hibernate3.2+struts2 (轉)

    此文章轉自:http://pipe.javaeye.com/blog/290644
    在這3種框架搭配使用的時候,我們往往需要寫很多xml配置文件來配置各個框架的依賴關系。大的項目中,xml配置文件的過多,過于繁瑣,導致查找起來會很不方便。
    在這種情況下,我們需要簡化我們的配置文件,同時結合部分xml來進行配置,這時候我們想到了annotation,這個近幾年炒得很火的玩意。annotation和xml各自的好處和弊端我就不多說了,看代碼吧。
    開發環境要求:jdk6.0以上。tomcat5.5以上(也許tomcat5.0也行 不過沒試過)
    先從hibernate入手吧:
    按照以往的寫法,我們需要有.hbm文件來完成po映射。現在我們通過annotation省去了這部分工作。
    具體代碼如下:這是一個po類
     1 import javax.persistence.Column;   
     2 import javax.persistence.Entity;   
     3 import javax.persistence.GeneratedValue;   
     4 import javax.persistence.Id;   
     5 import javax.persistence.Table;   
     6   
     7 @Entity  
     8 @Table(name = "userlog")   
     9 public class UserLog {   
    10     @Id  
    11     @GeneratedValue  
    12     private Long id;   
    13   
    14     @Column(name = "loginName")   
    15     private String loginName;   
    16   
    17 .下面是setter/getter方法。  
    18 

    我們沒在spring配置文件中配置hibernate連接信息,還是采用傳統的hibernate.cfg.xml,當然也可以在spring中配置。代碼如下:

     1 <?xml version="1.0" encoding="UTF-8"?>   
     2 <!DOCTYPE hibernate-configuration PUBLIC   
     3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
     4         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">   
     5 <hibernate-configuration>   
     6     <session-factory>   
     7         <property name="connection.datasource">java:/comp/env/jdbc/ExampleDB</property>   
     8          
     9         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>   
    10            
    11         <!--  <property name="hbm2ddl.auto">create</property>-->   
    12         <property name="show_sql">true</property>   
    13         <mapping class="com.nuctech.po.UserLog"/>   
    14           
    15     </session-factory>   
    16 </hibernate-configuration>  

    通過mapping class 我們就完成了po映射。

    OK!我們再看dao層:

     1 @Component  
     2 public class TestDao{   
     3  @Resource  
     4     private SessionFactory sessionFactory;   
     5   
     6     public SessionFactory getSessionFactory() {   
     7     return sessionFactory;   
     8     }   
     9   
    10     public void setSessionFactory(SessionFactory sf) {   
    11     this.sessionFactory = sf;   
    12     }   
    13    public Session getSession() {   
    14         return sessionFactory.getCurrentSession();   
    15     }   
    16    public void save(Object obj){   
    17     getSession().save(obj);   
    18     }   
    19 }  
    20 

    在這里,我們的dao采用了@Component 表示它是一個組件,在別的類中將會去調用。
    @Resource 引用SessionFactory 的bean.
    關于annotation 可以參考Spring-Reference_zh_CN.chm

    再來看我們的Action:
     1 @Component("TestAction")   
     2 public class TestAction extends ActionSupport {   
     3    @Resource  
     4     private TestDao dao; //這里引用上面的Component    
     5     private UserLog log;   
     6    setter/getter方法   
     7   
     8      其他的寫法都一樣了。   
     9   
    10 }  
    11 

    再看我們的struts配置文件
     1 <!DOCTYPE struts PUBLIC   
     2         "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"  
     3         "http://struts.apache.org/dtds/struts-2.0.dtd">   
     4 <struts>   
     5     <include file="struts-default.xml"/>   
     6         <constant name="struts.objectFactory" value="spring" />   
     7         <constant name="struts.devMode" value="true" />   
     8      <package name="com.nuctech.action" extends="struts-default">   
     9                  <action name="queryUserLogByPage" class="TestAction" method="queryUserLogByPage">   
    10         省略.   
    11          </action>   
    12     </package>   
    13        
    14 </struts>  
    15 

    注意這個action名字與@Component("TestAction")一致。
    在沒有annotation的情況下,我們在spring的配置文件中需要有很多的bean注入。現在都已經在類中注入了 那么我們現在來看spring配置文件:

     1 <?xml version="1.0" encoding="UTF-8"?>   
     2 <beans xmlns="http://www.springframework.org/schema/beans"  
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
     4     xmlns:context="http://www.springframework.org/schema/context"  
     5     xmlns:tx="http://www.springframework.org/schema/tx"  
     6     xmlns:jee="http://www.springframework.org/schema/jee"  
     7     xsi:schemaLocation="   
     8     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
     9     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd   
    10     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd   
    11     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">   
    12     <context:annotation-config />   
    13   
    14     <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>   
    15        
    16     <bean id="sessionFactory"  
    17         class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
    18         <property name="configLocation" value="classpath:/hibernate.cfg.xml" />   
    19     </bean>   
    20     <bean id="transactionManager"  
    21         class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
    22         <property name="sessionFactory" ref="sessionFactory" />   
    23     </bean>   
    24   
    25     <context:component-scan base-package="com.xxxx"/>   
    26     <tx:annotation-driven/>   
    27 </beans>

    我們只在這個配置文件中配置了sessionFactory.以前需要配置的bean不見了。
    另外附上我們的jndi配置文件,在WebContent(WebRoot)下面的META-INF文件夾下面的context.xml。
     1 <?xml version="1.0" encoding="UTF-8"?>   
     2 <Context antiResourceLocking="false">   
     3     <!-- 以下段配置session在tomcat重啟時的持久化策略,saveOnRestart為false時不進行持久化,方便調試時使用 -->   
     4     <Manager className="org.apache.catalina.session.PersistentManager"  
     5         debug="0" saveOnRestart="false" maxActiveSessions="-1"  
     6         minIdleSwap="-1" maxIdleSwap="-1" maxIdleBackup="-1">   
     7         <Store className="org.apache.catalina.session.FileStore"  
     8             directory="mydir" />   
     9     </Manager>   
    10     <!-- MySQL配置-->   
    11         <Resource name="jdbc/ExampleDB" type="javax.sql.DataSource"  
    12             driverClassName="com.mysql.jdbc.Driver"  
    13             url="jdbc:mysql://localhost:3306/book?useUnicode=true&amp;characterEncoding=utf-8"  
    14             username="root" password="123" validationQuery="select 1"  
    15             maxIdle="4" maxWait="5000" maxActive="8" removeAbandoned="true"  
    16             removeAbandonedTimeout="120">   
    17         </Resource>   
    18        
    19 </Context>  
    20 

    注意這個jndi名字與hibernate.cfg.xml中一致。

    先就這樣吧。

    posted on 2009-10-22 21:58 生命的綻放 閱讀(700) 評論(0)  編輯  收藏 所屬分類: S2SH

    <2009年10月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿(5)

    隨筆分類(94)

    隨筆檔案(93)

    文章分類(5)

    文章檔案(5)

    相冊

    JAVA之橋

    SQL之音

    兄弟之窗

    常用工具下載

    積分與排名

    最新評論

    閱讀排行榜

    主站蜘蛛池模板: 中文字幕乱码免费看电影| 国产精品美女午夜爽爽爽免费| 久久久国产精品亚洲一区| 成年女人免费碰碰视频| 一级毛片视频免费| 亚洲一区在线视频观看| 免费永久国产在线视频| 国产裸模视频免费区无码| 久久国产精品免费一区| 亚洲avav天堂av在线网爱情| 亚洲另类少妇17p| 国产视频精品免费视频| 亚洲国产精品久久久久秋霞影院| 免费一级黄色毛片| 免费在线观看h片| 在线播放免费人成视频网站| 亚洲情综合五月天| 在线a人片天堂免费观看高清| 成人无码WWW免费视频| 久久亚洲AV成人无码| 免费成人午夜视频| 57PAO成人国产永久免费视频 | 亚洲日韩国产欧美一区二区三区| 亚洲最大av无码网址| 亚洲精品动漫免费二区| 久久99精品国产免费观看| 深夜a级毛片免费无码| 亚洲高清视频在线| 久久亚洲国产成人精品性色| 黑人大战亚洲人精品一区 | 亚洲性天天干天天摸| 亚洲无码高清在线观看| 性做久久久久免费看| 九九精品免费视频| 69pao强力打造免费高清| 国产免费无码一区二区| 男女污污污超污视频免费在线看| 中国china体内裑精亚洲日本| 亚洲黄色片在线观看| 亚洲天天做日日做天天欢毛片| 久久久久国产成人精品亚洲午夜 |