1. 使用Eclipse创建Maven项目
2. 配置pom.xml引入需要的依赖包
1 <dependencies> 2 <dependency> 3 <groupId>junit</groupId> 4 <artifactId>junit</artifactId> 5 <version>3.8.1</version> 6 <scope>test</scope> 7 </dependency> 8 9 <!-- struts2 --> 10 <dependency> 11 <groupId>org.apache.struts</groupId> 12 <artifactId>struts2-core</artifactId> 13 <version>2.5.20</version> 14 </dependency> 15 <dependency> 16 <groupId>org.apache.struts</groupId>
17 <artifactId>struts2-convention-plugin</artifactId> 18 <version>2.5.20</version> 19 </dependency> 20 21 <!-- spring --> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-core</artifactId> 25 <version>5.1.5.RELEASE</version> 26 </dependency> 27 <dependency> 28 <groupId>org.springframework</groupId> 29 <artifactId>spring-web</artifactId> 30 <version>5.1.5.RELEASE</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework</groupId> 34 <artifactId>spring-context</artifactId> 35 <version>5.1.5.RELEASE</version> 36 </dependency> 37 <dependency> 38 <groupId>org.springframework</groupId> 39 <artifactId>spring-beans</artifactId> 40 <version>5.1.5.RELEASE</version> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework</groupId> 44 <artifactId>spring-aspects</artifactId> 45 <version>5.1.5.RELEASE</version> 46 </dependency> 47 48 <!-- hibernate --> 49 <dependency> 50 <groupId>org.hibernate</groupId> 51 <artifactId>hibernate-core</artifactId> 52 <version>5.4.2.Final</version> 53 </dependency> 54 55 <!-- spring + hibernate5 --> 56 <dependency> 57 <groupId>org.springframework</groupId> 58 <artifactId>spring-orm</artifactId> 59 <version>5.1.5.RELEASE</version> 60 </dependency> 61 <!-- struts2 + spring --> 62 <dependency> 63 <groupId>org.apache.struts</groupId> 64 <artifactId>struts2-spring-plugin</artifactId> 65 <version>2.5.20</version> 66 </dependency> 67 68 69 <!-- 数据库 --> 70 <dependency> 71 <groupId>commons-dbcp</groupId> 72 <artifactId>commons-dbcp</artifactId> 73 <version>1.4</version> 74 </dependency> 75 76 <dependency> 77 <groupId>sqlserver</groupId> 78 <artifactId>sqljdbc4</artifactId> 79 <version>4.0</version> 80 </dependency> 81 <!-- log4j-core --> 82 <dependency> 83 <groupId>org.apache.logging.log4j</groupId> 84 <artifactId>log4j-core</artifactId> 85 <version>2.11.2</version> 86 </dependency> 87 </dependencies>
3. 在源码路径下配置hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.c3p0.max_size">10</property> <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</property> </session-factory> </hibernate-configuration>
数据源依赖的数据可以放入spring配置中
4. 在源码路径下配置Spring配置文件 我这里为Spring.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:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <context:component-scan base-package="com.hugh.ssh.dao" /> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> <property name="url" value="jdbc:sqlserver://IP地址:1433;DatabaseName=pz_test;" /> <property name="username" value="sa"/> <property name="password" value="密码"/> </bean> <!-- 配置session工厂 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" lazy-init="false"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- //加载实体类的映射文件位置及名称 --> <property name="mappingLocations" value="classpath:com/hugh/ssh/domain/*.hbm.xml"></property> </bean> <!-- 配置Spring声明式事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 配置事务事务属性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" read-only="true"/> <tx:method name="*"/> </tx:attributes> </tx:advice> <!-- 配置事务切点,并把切点和事务属性关联起来 --> <aop:config> <aop:pointcut expression="execution(* com.hugh.ssh.dao.impl.*.*(..))" id="txPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config> </beans>
5. 在源码路径下配置struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" "http://struts.apache.org/dtds/struts-2.5.dtd"> <struts> <!-- 指定由spring负责action对象的创建 --> <constant name="struts.objectFactory" value="spring" /> <!-- 所有匹配*.action的请求都由struts2处理 --> <constant name="struts.action.extension" value="action," /> <!-- 是否启用开发模式 --> <constant name="struts.devMode" value="true" /> <!-- struts配置文件改动后,是否重新加载 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 设置浏览器是否缓存静态内容 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 --> <constant name="struts.i18n.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 让struts2支持动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名称中是否还是用斜线 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允许标签中使用表达式语法 --> <constant name="struts.tag.altSyntax" value="true" /> <!-- 对于WebLogic,Orion,OC4J此属性应该设置成true --> <constant name="struts.dispatcher.parametersWorkaround" value="false" /> <package name="basicstruts2" extends="struts-default"> <action name="index"> <result>/index.jsp</result> </action> </package> </struts>
6. 配置项目的web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns: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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5" metadata-complete="true"> <display-name>ssh</display-name> <!-- 整合Spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Spring.xml</param-value> </context-param> <!-- 整合Struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
7. 源码路径下配置 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" /> </Console> <RollingFile name="RollingFile" filename="log/test.log" filepattern="${logPath}/%d{YYYYMMddHHmmss}-fargo.log"> <PatternLayout pattern="%d{YYYY-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" /> <Policies> <SizeBasedTriggeringPolicy size="10 MB" /> </Policies> <DefaultRolloverStrategy max="20" /> </RollingFile> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console" /> <AppenderRef ref="RollingFile" /> </Root> </Loggers> </Configuration>
spring5不支持log4j.properties
8. 说明
- 整合中主要是通过web.xml配置为项目设定spring的监听器进行自动装配
- 通过web.xml配置过滤器拦截请求使用struts2作为MVC框架
- struts2配置文件指定spring为其生成action对象,如果不指定,action中就无法自动注入
- Spring整合hibernate主要是把hibernate的session工厂配置成bean
- log4j.properties是spring报错时候需要用到的
整合后系统的一个运行大概是:请求先被spring进行监听,启动spring容器进行加载各种bean,然后处理完毕后通过struts2的拦截器拦截到请求,根据配置执行action得到结果返回前端,而hibernate的sessionFactory则通过spring直接注入