整合逻辑:先整合spring,再整合hibernate,最后整合Struts2.
spring整合:
1.导入相关jar包:导入spring相关jar包:ioc,aop,tx,web,junit整合包
2.在src目录下创建config.spring的包,在其中创建applicationContext.xml文件,引入约束文件。本项目IOC与DI均采用注解的方式实现
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.maeeki"/>
</beans>
3.在web.xml中配置applicationContext.xml文件为应用加载时,随着servletContext对象的创建而加载,因此需要配置servletContext对象的监听器
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.接着需要指定加载applicationtionContext.xml文件的位置,若是将该文件放入WEB-INF目录下,那么就不用进行此配置,因为spring默认就是去加载WEB-INF下的applicationContext.xml文件的。但是为了便于配置文件的统一管理,建议还是将配置文件放于一处去管理
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/spring/applicationContext.xml</param-value>
</context-param>
备注:为什么要加classpath呢?因为spring默认的加载路径是整个项目的根目录,而配置文件的保存位置是从类的根目录(src)起始的,加上classpath:进行指定当前的相对路径是类的根目录。
整合hibernate(spring接管hibernate配置文件,不再另外创建hibernate.cfg.xml文件):
1.导入hibernate的相关jar包
2.在config.spring下面创建文件:applicationContext-jdbc.xml
如下配置applicationContext-jdbc.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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">
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 连接数据库 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate -->
<property name="hibernateProperties">
<props>
<!-- 配置MySQL数据库的方言 -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 配置使得控制台可以展示自动生成的sql语句 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 配置使得展示的sql语句以一定的格式输出 -->
<prop key="hibernate.format_sql">true</prop>
<!-- 配置值为update时,若数据库中不存在表时自动生成,存在表时在原表上操作,若不想自动生成改为none即可 -->
<prop key="hibernate.hbm2ddl.auto">update</prop>
<!-- 将session绑定到当前线程上,用于控制线程安全问题 -->
<prop key="hibernate.current_session_context_class">
org.springframework.orm.hibernate5.SpringSessionContext
</prop>
</props>
</property>
<!-- 引入映射文件 -->
<property name="mappingLocations">
<array>
<value>classpath:com/maeeki/domain/*.hbm.xml</value>
</array>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://locahost:3306/crm"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
</bean>
<!-- 配置hibernateTemplate -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>l
</bean>
</beans>
3.配置事务 创建applicationContext-tx.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 配置事务管理器 -->
<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="*" propagation="REQUIRED" read-only="false"/><tx:method name="find*" propagation="SUPPORTS" read-only="true"/></tx:attributes></tx:advice>配置切面 <aop:config><aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/></aop:config>-->
</beans>
4.配置aop 创建applicationContext-aop.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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">
<!-- 配置切入点 -->
<aop:config>
<aop:pointcut expression="execution(* com.maeeki.service.impl.*.*(..))" id="pt1"/>
</aop:config>
</beans>
5.将以上的三个配置导入整合到applicationContext.xml中
<import resource="applicationContext-jdbc.xml"/>
<import resource="applicationContext-tx.xml"/>
<import resource="applicationContext-aop.xml"/>
6.创建实体类及其对应的映射xml文件
映射文件的模板如下 :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.maeeki.domain">
<class name="Customer" table="cst_customer">
<id name="custId" column="cust_id">
<generator class="native"></generator>
</id>
<property name="custName" column="cust_name"></property>
<property name="custSource" column="custSource"></property>
<property name="custIndustry" column="custIndustry"></property>
<property name="custLevel" column="custLevel"></property>
<property name="custAddress" column="custAddress"></property>
<property name="custPhone" column="custPhone"></property>
</class>
</hibernate-mapping>
7.检测hibernate与spring整合是否成功
创建service层,创建dao层,在dao层中调用hibernateTemplate进行数据的存储操作(做Java开发的这个地方应该都很清楚的)
建立一个junit测试类,并在测试类中引入程序运行所需要加载的配置文件,测试类代码如下:
package com.maeeki.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.maeeki.domain.Customer;
import com.maeeki.service.ICustomerService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:config/spring/applicationContext.xml"})
public class SpringHibernateTest {
@Autowired
private ICustomerService customerService;
@Test
public void Test1(){
Customer customer = new Customer();
customer.setCustName("福光");
customer.setCustPhone("34252525");
customerService.addCustomer(customer);
}
}
Struts2整合
1.导入Struts2 jar包
2.将action动作类交于spring进行管理
3.创建spring.struts包,在该包中创建struts.xml文件,(注意spring接管动作类后在Struts.xml中的class有属性应与spring定义的动作类id一致)示例代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 配置开发者模式 --> <constant name="struts.devMode" value="true"></constant> <!-- 配置URL后缀名 --> <constant name="struts.action.extension" value="action"></constant> <!-- 配置页面主题 --> <constant name="struts.ui.theme" value="simple"></constant> <!-- 配置公共包 --> <package name="MyDefault" extends="struts-default" abstract="true"> </package> <!-- 载入其他配置文件 --> <include file="config/struts2/struts-customer.xml"></include> </struts>
4.创建struts-customer.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="customer" extends="MyDefault" namespace="/customer"> <action name="findAllCustomer" class="customerAction" method="findAllCustomer"> <result name="findAllCustomer">/jsp/customer/list.jsp</result> </action> </package> </struts>
5.在Struts.xml中加载该文件
6.在web.xml中,配置Struts2的核心过滤器,并在过滤器中配置其初始化参数,指定Struts配置文件的位置,最终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" 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"> <display-name>day038_crm_S2S4H503_Test</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置监听器,用于监听servletContext对象的状态 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置servletContext对象创建时需要加载的内容 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/spring/applicationContext.xml</param-value> </context-param> <!-- 配置Struts2的核心过滤器 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <!-- 配置核心过滤器初始化时加载的内容 --> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,config/struts2/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
7.创建动作类CustomerAction
package com.maeeki.web.action; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import com.maeeki.domain.Customer; import com.maeeki.service.ICustomerService; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; @Controller("customerAction") public class CustomerAction extends ActionSupport implements ModelDriven<Customer>{ @Autowired private ICustomerService customerService; private Customer customer = new Customer(); @Override public Customer getModel() { return customer; } private List<Customer> customers; public List<Customer> getCustomers() { return customers; } public String findAllCustomer(){ customers = customerService.findAllCustomer(); return "findAllCustomer"; } }