目标:整合SSH-Spring+Struts2+Hibernate
为什么要整合呢?因为每个框架各有优势呢,Hibernate在数据持久化层做得好;Struts2在处理请求、调用方法和页面跳转方面不错;Sping呢就是个大管家,在笔者看来,Sping就是个leader,它会统筹兼顾,整体调度与资源调度……怎么说着说着有点像ResourceManager了..
整合可不是简单的拼凑配置文件!而是要真正的融合。收购公司也不是简单的收购,文化只有融合了,才算是较好的收购。不然就是:“整合了我的心,也整合不了我的灵魂”。尤其是Sping和Struts2整合后再加入Hibernate就有点繁琐。所以先整合Spring+Struts2;然后再是Hibernate。
Let’s Go!
目录
一、整体架构
二、Spring+Struts2
三、整合Hibernate
四、测试运行
五、总结
开发环境
IntelliJ IDEA(2017.2.5)
一、整体架构
![这里写图片描述](https://img-blog.csdn.net/20171107084711384?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbWVpY2VhdGNzZG4=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
二、Spring+Struts2
他俩整合起来比较简单,可以看做是配置文件的简单拼凑,就是导包的时候需要一个中间插件。
1、导包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
注意同一个框架内部的包包版本要保持一致。
2、配置spring 和struts2的配置文件
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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd " >
<context:component-scan base-package="com.hmc.ssh"></context:component-scan>
</beans>
struts2.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>
<constant name="struts.objectFactory" value="spring"/>
<package name="ssh" extends="struts-default">
<action name="book_list" class="bookAction" method="list">
<result name="success">/list.jsp</result>
</action>
</package>
<include file="example.xml"/>
</struts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
注意:
<constant name="struts.objectFactory" value="spring"/>
这个作用就是让struts2识别bookAction,因为bookAction只是Spring里面的bean名字,struts2并不认识,之前strts2直接引用具体的包,这里要变化引用bean.二者之间的桥梁就是靠这个strts.objectFactory建立起来的。
以上都是最最基本的配置奥,struts2也暂没有实现DMI,先保证整合不出问题,后续需要在依次加入各种配置,不要急,慢慢来。
3、配置Web.xml
为什么要配置这个?因为struts2和spring都有各自的配置文件,但是最终能我们要的是Web项目,要和Web建立关系?怎么建立呢?Struts2通过过滤器,Spring通过监听器,然后要保证整体程序可以找到各自的配置文件。搞定这几个问题,就搞定了web.xml配置。
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
<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>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
好,这个配置好后,基本就不动了。
4、编写相关业务类(为初步测试伏笔)
BookAction
package com.hmc.ssh.action;
import org.springframework.stereotype.Controller;
/**
* User:Meice
* 2017/11/6
*/
@Controller
public class BookAction {
public String list() {
System.out.println("list....");
return "success";
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
5、在struts.xml中配置action
上面已有,不在赘述。
6、测试
启动猫,在页面访问book_list,查看是否调用方法,并跳转到页面即可。
至此,最为简单的Struts2和Spring整合完毕!
三、整合Hibernate
整合Hibernate就是整合那几个配置:连接数据库、实体类映射、其他一些琐碎配置(show_sql 开发者模式 dmi)等等。把这些无缝整合到Spring就好了,但是麻烦就麻烦再这里。因为Hibernate失去了管理权,全部移交Spring.xml来管理了。
1、导包
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
2、配置spring.xml
把整个Hibernate.cfg.xml配置到spring.xml中;笔者称之为管理权滴交接,在笔者看来,就是一场收购,也是一场“杯酒释兵权”!
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/sysmgr"/>
<property name="username" value="root"/>
<property name="password" value="119913"/>
<property name="initialSize" value="10"/>
</bean>
<bean name="sessionFactort" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<array>
<value>com.hmc.ssh.model</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
需要注意的是,引入了数据源。然后各种bean统统由sessionFactory来管理。为什么呢?因为Hibernate要通过sessionFactory来开启session,开启后的session就可以直接使用其自身非常好用的API啦。
配到这里,也就差不多了,但是好戏还在后头。笔者观察在下的测试类后面的bug,就知道这个过程啦。
四、测试运行
1、BaseDao
HibernateDaoSupport
只要BaseDao继承HibernateDaoSupport之后,就可以直接引用了。注意,这之前已经配置好sessionFactory
package com.hmc.ssh.dao;
import com.hmc.ssh.model.Book;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.Nullable;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import java.util.List;
/**
* User:Meice
* 2017/11/6
*/
@Repository
public class BookDao extends HibernateDaoSupport {
@Resource(name = "sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
public List<Book> getList() {
return getSessionFactory().getCurrentSession().createQuery("from Book").list();
}
public void add(Book book) {
getSessionFactory().getCurrentSession().save(book);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
注意:HibernateDaoSupport中的属性sessionFactory是final类型的,所以我们设置名称的时候要修改为另外一个名字;而且给sessionfactory设置值后要用里面得到的sessionFactory;否则就找不到HibernateDaoSupport中的session
这个时候,测试会报各种错误的。详情在测试类-MyTest的注释里。
2、声明式事务
如果未开启声明式事务,就会报错:
Could not obtain transaction-synchronized Session for current thread
如何开启?
1)头部声明:
xmlns:tx="http://www.springframework.org/schema/tx"
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
官网都有的奥!
https://docs.spring.io/spring/docs/5.0.1.RELEASE/spring-framework-reference/data-access.html#spring-data-tier
2)声明
<bean id="txManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* com.hmc.ssh.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
3、测试类MyTest
package com.hmc.ssh.test;
import com.hmc.ssh.dao.BookDao;
import com.hmc.ssh.model.Book;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
/**
* User:Meice
* 2017/11/6
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class MyTest {
@Autowired
@Qualifier(value = "bookDao")
private BookDao bookDao;
@Test
public void testList() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
BookDao bd= ac.getBean("bookDao", BookDao.class);
List<Book> list = bd.getList();
for(Book bk:list) {
System.out.println(bk);
}
}
/**
*
* 运行结果:
* Caused by: java.lang.IllegalArgumentException:
* 'sessionFactory' or 'hibernateTemplate' is required
*/
/**
* Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
* No bean named 'sessionFactory' available
*/
/**
* java.lang.NullPointerException
*/
/**
* org.hibernate.HibernateException: Could not obtain transaction-synchronized Session for current thread
* 表明需要声明式事务支持
* 需要解决问题:
* 1、如何声明事务? HibernateTransactionManager
* 2、给谁声明事务? sessionFactory
*/
/**
* Caused by: java.lang.ClassNotFoundException: org.aspectj.weaver.reflect.ReflectionWorld$ReflectionWorldException
* 表明没有AOP的包
*/
/**
* 运行结果;
* "D:\Program Files\Java\jdk1.8.0_144\bin\java" -ea -
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
Hibernate: select book0_.bookid as bookid1_0_, book0_.bookauthor as bookauth2_0_, book0_.bookdate as bookdate3_0_, book0_.bookname as bookname4_0_, book0_.bookpress as bookpres5_0_, book0_.bookprice as bookpric6_0_ from Book book0_
Book{bookid=1, bookname='宝贝,宝贝', bookauthor='周国平', bookpress='人民文学出版社', bookprice=28.33, bookdate=2017-11-02 00:00:00.0}
Book{bookid=2, bookname='上帝掷骰子吗', bookauthor='曹天元', bookpress='中国科学出版社', bookprice=33.66, bookdate=2017-11-02 00:00:00.0}
*/
@Test
public void testAdd() {
Book book = new Book();
book.setBookname("SSH框架");
bookDao.add(book);
}
/**
* 为避免每次都要加载spring.xml文件,
* 每次都要getBean
* 所以使用JUnit4进行测试
* @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
* @Autowired
private BookDao bookDao;
* 这样就可以直接用了!
*/
@Test
public void testAdd2() {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
BookDao bookDao= ac.getBean("bookDao",BookDao.class);
Book book = new Book();
book.setBookname("SSH框架2");
bookDao.add(book);
/**
* 运行结果:
* 十一月 07, 2017 8:09:27 上午 org.junit.vintage.engine.discovery.TestClassRequestResolver determineRunnerTestDescriptor
警告: Runner org.junit.internal.runners.ErrorReportingRunner (used on com.hmc.ssh.test.MyTest) does not support filtering and will therefore be run completely.
Hibernate: insert into Book (bookauthor, bookdate, bookname, bookpress, bookprice) values (?, ?, ?, ?, ?)
*/
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
需要注意的是,上面引用了Junit4测试。可以大大简化测试步骤。之前测试需要先加载spring.xml,然后获取bean,现在加载spring.xml通过
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
搞定,获取bean通过自动装配搞定,所以就可以直接用了。
至此,流程已经打通。任督二脉畅行无阻!
五、总结
前面配置都是零碎分开的,这里总结下整体配置。
1)pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hmc.ssh</groupId>
<artifactId>ssh</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../ssh2</module>
</modules>
<name>ssh Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.13</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.12.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>ssh</finalName>
</build>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
2、web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<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>
<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>
</web-app>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
3、2个配置文件
1) 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>
<constant name="struts.objectFactory" value="spring"/>
<package name="ssh" extends="struts-default">
<action name="book_list" class="bookAction" method="list">
<result name="success">/list.jsp</result>
</action>
</package>
<include file="example.xml"/>
</struts>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
2) 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: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.hmc.ssh"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost/sysmgr"/>
<property name="username" value="root"/>
<property name="password" value="119913"/>
<property name="initialSize" value="10"/>
</bean>
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<array>
<value>com.hmc.ssh.model</value>
</array>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="allMethod" expression="execution(* com.hmc.ssh.dao.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allMethod"/>
</aop:config>
</beans>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
因为配置比较固话,比较利于“传承”,可一致性很强。所以,关键是明白每个配置的作用,没有这个配置会发生什么?具体怎么配置,不用刻意记忆,只用参考官网即可。