读书人

SSH调整-XML版和Annotation版

发布时间: 2012-09-29 10:30:01 作者: rapoo

SSH整合-XML版和Annotation版
1. SSH整合1.1 Spring + Hibernate

1)导入相关jar包

Spring的jar包 :(13个)

核心jar包:

org.springframework.asm-3.1.1.RELEASE.jar 字字码处理

org.springframework.core-3.1.1.RELEASE.jar spring核心,访问资源

org.springframework.beans-3.1.1.RELEASE.jar核心容器

org.springframework.context-3.1.1.RELEASE.jarApplicationContext等类在这个包中

org.springframework.expression-3.1.1.RELEASE.jarSpring3 EL

commons-logging.jar---spring所必须依赖的唯一第三方包,输出日志

AOP有关的jar包:

aopalliance.jar aop联盟

aspectjweaver.jar AspectJ

cglib-nodep-2.1_3.jar 生成代理类

org.springframework.aop-3.1.1.RELEASE.jaraop 核心包

Jdbc有关的jar包:

org.springframework.jdbc-3.1.1.RELEASE.jar 提供了jdbc操作模版

与Hibernate整合需要的jar包:

org.springframework.orm-3.1.1.RELEASE.jar

与事务有关的jar包:

org.springframework.transaction-3.1.1.RELEASE.jar

Hibernate的jar包:

antlr-2.7.6.jar一个语言转换工具,Hibernate利用它实现 HQL 到 SQL 的转换

commons-collections-3.1.jarcollectionsApache 的工具集,用来增强Java对集合的处理能力

dom4j-1.6.1.jar dom4j XML 解析器

hibernate3.jar Hibernate核心包

javassist-3.9.0.GA.jar 代码生成工具, Hibernate用它在运行时扩展 Java类

jta-1.1.jar标准的 JAVA 事务处理接口

log4j.jarhibernate使用的一个日志系统,结合slf4j-api-1.5.8.jar和slf4j-log4j12.jar一起使用

slf4j-api-1.5.8.jar

slf4j-log4j12.jar

数据库连接的jar包:

数据库使用MySQL,连接池使用dbcp。

commons-pool.jar dbcp连接池

commons-dbcp.jar dbcp连接池

mysql-connector-java.jar MySQL数据库连接驱动

2)核心代码和配置

dao实现类使用了Spring提供的HibernateTemplete模版。

publicclass AccoutDAOImpl extendsHibernateDaoSupportimplements IAccountDAO {

@Override

publicvoid save(Account a) {

getHibernateTemplate().save(a);

}

@Override

publicvoid update(Long id,Account a) {

a.setId(id);

getHibernateTemplate().update(a);

}

//省略部分方法.......

配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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">

<importresource="beans-db.xml"/>

<!--让Spring来创建和管理SessionFactory ,用工厂Bean来创建 hibernate3-->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!--LocalSessionFactoryBean的父类依赖数据源 -->

<propertyname="dataSource"ref="dataSource"/>

<!--注入依赖的属性 Hibernate特有-->

<propertyname="hibernateProperties">

<!--是一个Properties类型的属性 -->

<props>

<propkey="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<propkey="hibernate.show_sql">true</prop>

<propkey="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<!--指定映射文件的目录 -->

<propertyname="mappingDirectoryLocations">

<array>

<value>com/maple/spring/domain</value>

</array>

</property>

</bean>

<!--配置accountDAO -->

<beanid="accountDAO"class="com.maple.spring.dao.impl.AccoutDAOImpl">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--配置TransferServiceImpl -->

<beanid="transferService"class="com.maple.spring.service.impl.TransferServiceImpl">

<!--注入所依赖的dao -->

<propertyname="accountDAO"ref="accountDAO"/>

</bean>

<!--配置事务管理器,Hibernate专用的,注意:这里用的是Hibernate3 -->

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<!--注入依赖的sessionFactory -->

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--启动事务。扫描注解 -->

<tx:annotation-driventransaction-manager="transactionManager"/>

</beans>

1.2 Spring + Strust2

1)导入相关jar包

Spring的jar包 :

核心jar包:

org.springframework.asm-3.1.1.RELEASE.jar 字字码处理

org.springframework.core-3.1.1.RELEASE.jarspring核心,访问资源

org.springframework.beans-3.1.1.RELEASE.jar核心容器

org.springframework.context-3.1.1.RELEASE.jarApplicationContext等类在这个包中

org.springframework.expression-3.1.1.RELEASE.jarSpring3 EL

commons-logging.jar---spring所必须依赖的唯一第三方包,输出日志

AOP有关的jar包:

aopalliance.jar aop联盟

aspectjweaver.jar AspectJ

cglib-nodep-2.1_3.jar 生成代理类

org.springframework.aop-3.1.1.RELEASE.jaraop 核心包

Jdbc有关的jar包:

org.springframework.jdbc-3.1.1.RELEASE.jar 提供了jdbc操作模版

与Struts2整合需要的jar包:

org.springframework.web-3.1.1.RELEASE.jar

与事务有关的jar包:

org.springframework.transaction-3.1.1.RELEASE.jar

Strust2s的jar包:

struts2-core-2.1.8.1.jar :Struts 2框架的核心类库。

xwork-core-2.1.6.jar :XWork类库,Struts 2在其上构建。

ognl-2.7.3.jar :对象图导航语言(Object Graph NavigationLanguage), struts2框架通过其读写对象的属性。

freemarker-2.3.15.jar :Struts 2的UI标签的模板使用FreeMarker编写

commons-logging-1.1.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。

commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后需要加入此文件

commons-io-1.3.2.jar,上传文件依赖的jar包

与Spring整合用的插件包:

struts2-spring-plugin-2.3.1.1.jar

2)核心代码和配置

publicclass HelloAction {

//依赖业务层

private IHelloServiceservice;

public String execute()throws Exception {

//得到容器

ApplicationContext acx =WebApplicationContextUtils.getWebApplicationContext(ServletActionContext.getServletContext());

service = (IHelloService)acx.getBean("helloService");

System.out.println(service.hashCode());

service.sysHi();

ActionContext.getContext().put("msg","执行成功。。。。");

return Action.SUCCESS;

}

}

beans.xml配置文件:

<beanid="helloService"class="com.maple.web.service.HelloServiceImpl">

<propertyname="msg"value="大家好。。。我是张三!"/>

</bean>

web.xml配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">

<!--自定义Spring启动加载的配置的文件的位置

指定后就不用在WEN-INF下有applicationContext.xml文件了

名字固定为:contextConfigLocation可以在ContextLoaderListener的父类ContextLoader中的常量(CONFIG_LOCATION_PARAM)中获得-->

<context-param>

<param-name>contextConfigLocation</param-name>

<!-- classpath指定强制到classpath下找指定文件 -->

<param-value>classpath:beans.xml</param-value>

</context-param>

<!--配置Spring的监听器,以启动Spring,启动时,默认到WEB-INF目录

下加载applicationContext.xml文件 -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!--配置Struts2的过滤器,以启动Strust2-->

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>

struts.xml配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC

"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!--引入Struts2的Spring插件后, 让Spring容器按类型注入Action所依赖的对象,默认是按名字注入,在default.properties文件可以看到 -->

<constantname="struts.objectFactory.spring.autoWire"value="type"/>

<packagename="hello"extends="struts-default">

<actionname="hello"class="com.maple.web.action.HelloAction">

<result>/WEB-INF/success.jsp</result>

</action>

</package>

</struts>

1.3 Strust2 + Spring + Hibernate

完整整合代码参见附件:

1) 导入相关jar包

Strust2s的jar包:

struts2-core-2.1.8.1.jar :Struts 2框架的核心类库。

xwork-core-2.1.6.jar :XWork类库,Struts 2在其上构建。

ognl-2.7.3.jar :对象图导航语言(Object Graph NavigationLanguage), struts2框架通过其读写对象的属性。

freemarker-2.3.15.jar :Struts 2的UI标签的模板使用FreeMarker编写

commons-logging-1.1.x.jar :ASF出品的日志包,Struts 2框架使用这个日志包来支持Log4J和JDK 1.4+的日志记录。

commons-fileupload-1.2.1.jar 文件上传组件,2.1.6版本后需要加入此文件

commons-io-1.3.2.jar,上传文件依赖的jar包

与Spring整合用的插件包:

struts2-spring-plugin-2.3.1.1.jar

Spring的jar包 :

核心jar包:

org.springframework.asm-3.1.1.RELEASE.jar 字字码处理

org.springframework.core-3.1.1.RELEASE.jarspring核心,访问资源

org.springframework.beans-3.1.1.RELEASE.jar核心容器

org.springframework.context-3.1.1.RELEASE.jarApplicationContext等类在这个包中

org.springframework.expression-3.1.1.RELEASE.jarSpring3 EL

commons-logging.jar---spring所必须依赖的唯一第三方包,输出日志

AOP有关的jar包:

aopalliance.jar aop联盟

aspectjweaver.jar AspectJ

cglib-nodep-2.1_3.jar 生成代理类

org.springframework.aop-3.1.1.RELEASE.jaraop 核心包

Jdbc有关的jar包:

org.springframework.jdbc-3.1.1.RELEASE.jar 提供了jdbc操作模版

与Struts2整合需要的jar包:

org.springframework.web-3.1.1.RELEASE.jar

与事务有关的jar包:

org.springframework.transaction-3.1.1.RELEASE.jar

Hibernate的jar包:

antlr-2.7.6.jar一个语言转换工具,Hibernate利用它实现 HQL 到 SQL 的转换

commons-collections-3.1.jarcollectionsApache 的工具集,用来增强Java对集合的处理能力

dom4j-1.6.1.jar dom4j XML 解析器

hibernate3.jar Hibernate核心包

javassist-3.9.0.GA.jar 代码生成工具, Hibernate用它在运行时扩展 Java类

jta-1.1.jar标准的 JAVA 事务处理接口

log4j.jarhibernate使用的一个日志系统,结合slf4j-api-1.5.8.jar和slf4j-log4j12.jar一起使用

slf4j-api-1.5.8.jar

slf4j-log4j12.jar

2)基于XML的整合

配置文件:

web.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"xsi:schemaLocation="http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5">

<!--配置Struts2的过滤器,以启动Struts2 -->

<filter>

<filter-name>struts2</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>struts2</filter-name>

<url-pattern>/*</url-pattern><!--过滤所有的请求 -->

</filter-mapping>

<!--配置Spring的监听器,以启动Spring -->

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

</web-app>

struts.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPEstrutsPUBLIC

"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<!--设置Spring管理Struts时按类型注入-->

<constantname="struts.objectFactory.spring.autoWire"value="type"/>

<packagename="transfer"extends="struts-default">

<actionname="transfer"class="com.maple.ssh.action.TransferAction">

<result>/WEB-INF/success.jsp</result>

</action>

</package>

</struts>

applicationContext.xml:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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">

<!--引入sessionFactory数据源 指定到classpath下找-->

<importresource="classpath:beans-db.xml"/>

<!--配置dao所依赖的sessionFactory ,从工厂Bean中获取-->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!--该工厂Bean的父类依赖一个数据源dataSource,需要注入 -->

<propertyname="dataSource"ref="dataSource"/>

<!--配置Hibernate创建SessionFactory需要的其他属性 -->

<propertyname="hibernateProperties">

<props>

<propkey="dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<propkey="show_sql">true</prop>

<propkey="hbm2ddl.auto">update</prop>

</props>

</property>

<!--指定Hibernate映射文件的目录 -->

<propertyname="mappingDirectoryLocations">

<array>

<!--指定要classpath的目录下找 -->

<value>classpath:com/maple/ssh/domain</value>

</array>

</property>

</bean>

<!--配置业务Bean所依赖的DAO Bean,由于DAO的实现继承了HibernateDaoSupport,

该类初始化时依赖SessionFactory,所以要注入SessionFactory -->

<beanid="accountDAO"class="com.maple.ssh.dao.impl.AccoutDAOImpl">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--配置业务Bean -->

<beanid="transferService"class="com.maple.ssh.service.impl.TransferServiceImpl">

<!--注入依赖的Bean -->

<propertyname="accountDAO"ref="accountDAO"/>

</bean>

<!--配置事务管理器 要注入其管理的SessionFactory-->

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--配置事务切入点 -->

<tx:adviceid="transferServiceAdvice"transaction-manager="transactionManager">

<tx:attributes>

<!--所有方法都需要事务 也可以单独指定某个方法-->

<tx:methodname="*"propagation="REQUIRED"/>

</tx:attributes>

</tx:advice>

<!--配置事务切面 -->

<aop:config>

<aop:advisoradvice-ref="transferServiceAdvice"pointcut="execution(*com.maple.ssh.service.impl..*(..))"/>

</aop:config>

</beans>

2) 基于Annotation的整合

applicationContext.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<beansxmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

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/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<!--引入sessionFactory数据源 指定到classpath下找-->

<importresource="classpath:beans-db.xml"/>

<!--配置dao所依赖的sessionFactory ,从工厂Bean中获取-->

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!--该工厂Bean的父类依赖一个数据源dataSource,需要注入 -->

<propertyname="dataSource"ref="dataSource"/>

<!--配置Hibernate创建SessionFactory需要的其他属性 -->

<propertyname="hibernateProperties">

<props>

<propkey="dialect">org.hibernate.dialect.MySQL5Dialect</prop>

<propkey="show_sql">true</prop>

<propkey="hbm2ddl.auto">update</prop>

</props>

</property>

<!--指定Hibernate映射文件的目录 -->

<propertyname="mappingDirectoryLocations">

<array>

<!--指定要classpath的目录下找 -->

<value>classpath:com/maple/ssh/domain</value>

</array>

</property>

</bean>

<!--配置事务管理器 -->

<beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<propertyname="sessionFactory"ref="sessionFactory"/>

</bean>

<!--启动注解注入 -->

<context:annotation-config/>

<!--启动组件扫描,会自动找到指定包下加了注解的组件创建对象并放IoC容器 -->

<context:component-scanbase-package="com.maple.ssh"/>

<!--启动注解配置的事务 -->

<tx:annotation-driventransaction-manager="transactionManager"/>

</beans>


源码下载地址:http://download.csdn.net/detail/xiaojianfengye1/4588993

读书人网 >XML SOAP

热点推荐