想飞就别怕摔

大爷的并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 生命的绽放 阅读(671) 评论(0)  编辑  收藏 所属分类: S2SH


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年10月>
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜