想飛就別怕摔

          大爺?shù)牟M罵人

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

          此文章轉(zhuǎn)自:http://pipe.javaeye.com/blog/290644
          在這3種框架搭配使用的時候,我們往往需要寫很多xml配置文件來配置各個框架的依賴關系。大的項目中,xml配置文件的過多,過于繁瑣,導致查找起來會很不方便。
          在這種情況下,我們需要簡化我們的配置文件,同時結合部分xml來進行配置,這時候我們想到了annotation,這個近幾年炒得很火的玩意。annotation和xml各自的好處和弊端我就不多說了,看代碼吧。
          開發(fā)環(huán)境要求:jdk6.0以上。tomcat5.5以上(也許tomcat5.0也行 不過沒試過)
          先從hibernate入手吧:
          按照以往的寫法,我們需要有.hbm文件來完成po映射。現(xiàn)在我們通過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連接信息,還是采用傳統(tǒng)的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 表示它是一個組件,在別的類中將會去調(diào)用。
          @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注入?,F(xiàn)在都已經(jīng)在類中注入了 那么我們現(xiàn)在來看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時不進行持久化,方便調(diào)試時使用 -->   
           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 生命的綻放 閱讀(708) 評論(0)  編輯  收藏 所屬分類: S2SH

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

          導航

          統(tǒng)計

          常用鏈接

          留言簿(5)

          隨筆分類(94)

          隨筆檔案(93)

          文章分類(5)

          文章檔案(5)

          相冊

          JAVA之橋

          SQL之音

          兄弟之窗

          常用工具下載

          積分與排名

          最新評論

          閱讀排行榜

          主站蜘蛛池模板: 肥西县| 清流县| 永州市| 新巴尔虎左旗| 华亭县| 南昌市| 怀宁县| 平利县| 潍坊市| 达孜县| 沂源县| 隆尧县| 新田县| 类乌齐县| 胶南市| 米林县| 嘉善县| 大邑县| 左贡县| 高阳县| 米泉市| 宁夏| 扶沟县| 安宁市| 柘城县| 兴和县| 林西县| 康乐县| 芦山县| 贵港市| 南漳县| 宝丰县| 中阳县| 东兰县| 英吉沙县| 三明市| 麻栗坡县| 乌拉特前旗| 揭阳市| 东乡族自治县| 巴彦县|