day03_1spring3
事务管理的几种方式、spring整合Junit、spring整合web、ssh整合
一、事务管理的几种方式:
1.介绍前提我们需要导入:spring-tx-3.2.0.RELEASE.jar的包里面含有spring管理事务的一系列的接口和类
关于里面我要了解三个顶级接口:

① PlatformTransactionManager 平台事务管理器,spring要管理事务,必须使用事务管理器
进行事务配置时,必须配置事务管理器。
②TransactionDefinition:事务详情(事务定义、事务属性),spring用于确定事务具体详情,
例如:隔离级别、是否只读、超时时间 等
进行事务配置时,必须配置详情。spring将配置项封装到该对象实例。
③TransactionStatus:事务状态,spring用于记录当前事务运行状态。例如:是否有保存点,事务是否完成。
spring底层根据状态进行相应操作。
1.2 PlatformTransactionManager平台事务管理器
常见的平台事务管理器:

想使用这个两个事务管理器则分别需要导入相应的jar包:

DataSourceTransactionManager ,jdbc开发时事务管理器,采用JdbcTemplate
HibernateTransactionManager,hibernate开发时事务管理器,整合hibernate
关于这个接口的api详解
TransactionStatus getTransaction(TransactionDefinition definition) ,事务管理器 通过“事务详情”,获得“事务状态”,从而管理事务。
void commit(TransactionStatus status) 根据状态提交
void rollback(TransactionStatus status) 根据状态回滚
1.3 TransactionDefinition 事务的描述(或者叫做事务详情)
关于这个接口的api详解:

其中传播行为的种类的描述:

传播行为:在两个业务之间如何共享事务。
PROPAGATION_REQUIRED , required , 必须 【默认值】
支持当前事务,A如果有事务,B将使用该事务。
如果A没有事务,B将创建一个新的事务。
PROPAGATION_SUPPORTS ,supports ,支持
支持当前事务,A如果有事务,B将使用该事务。
如果A没有事务,B将以非事务执行。
PROPAGATION_MANDATORY,mandatory ,强制
支持当前事务,A如果有事务,B将使用该事务。
如果A没有事务,B将抛异常。
PROPAGATION_REQUIRES_NEW , requires_new ,必须新的
如果A有事务,将A的事务挂起,B创建一个新的事务
如果A没有事务,B创建一个新的事务
PROPAGATION_NOT_SUPPORTED ,not_supported ,不支持
如果A有事务,将A的事务挂起,B将以非事务执行
如果A没有事务,B将以非事务执行
PROPAGATION_NEVER ,never,从不
如果A有事务,B将抛异常
如果A没有事务,B将以非事务执行
PROPAGATION_NESTED ,nested ,嵌套
A和B底层采用保存点机制,形成嵌套事务。
掌握:PROPAGATION_REQUIRED、PROPAGATION_REQUIRES_NEW、PROPAGATION_NESTED
其中隔离级别进行描述:这里的隔离级别,和数据库中的隔离级别是一个意思

1.4 TransactionStatus事务的状态:
TransactionStatus接口是事务状态,描述了某一时间点上的事务的状态信息,包含6个操作,具体如下:
- void flush()刷新事务
- boolean hasSavepoint()获取是否存在保存点
- boolean isCompleted()获取事务是否完成
- boolean isNewTransaction获取是否是新事务
- bolean isRollbackOnly():获取是否回滚
- void setRollbackOnly()设置事务回滚
2.转账经典案例的编写:
前提我们需要导入:①spring的4+1的jar包,4个核心,一个依赖,数据源这里我们导入c3p0.jar包和它的依赖包mchange.jar包最后就是mysql驱动jar包。②创建表t_user 并创建相应的JavaBean数据模型。
①数据库的代码:
create database ee19_spring_day03;
use ee19_spring_day03;
create table account(
id int primary key auto_increment,
username varchar(50),
money int
);
insert into account(username,money) values('jack','');
insert into account(username,money) values('rose','');
创建表
②数据库对应JavaBean数据模型:
public class User {
private Integer id;
private String username;
private Integer money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getMoney() {
return money;
}
public void setMoney(Integer money) {
this.money = money;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", money="
+ money + "]";
}
}
User
③service层代码
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, Integer money) {
accountDao.out(outer, money);
//断电
// int i = 1/0;
accountDao.in(inner, money);
}
}
service
④dao层代码:
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void out(String outer, Integer money) {
this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);
}
@Override
public void in(String inner, Integer money) {
this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);
}
}
dao
⑤spring配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd
"> <!-- 1 datasource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ee19_spring_day03"></property>
<property name="user" value="root"></property>
<property name="password" value="1234"></property>
</bean> <!-- 2 dao -->
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 3 service -->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
测试代码如下:
public class Test01 {
@Test
public void fun01(){
String xmlPath="cn/itcast/b_transactionTemplate_TransactionProxyFactoryBean/applicationContext.xml";
ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);//加载spring配置文件
UserSerivce userSerivce=(UserSerivce) applicationContext.getBean("userService");
//手动去在spring里面配置事务(我们可以在转账的过程中出点异常来查看)
accountSerivce.convery("张三", "李四", 200);
}
}
上面的例子是没有添加事务的,如果发生转账异常时不能回滚数据容易出现问题!
2.1手动添加事务【了解】(上面部分代码是拷的笔记的如果出现问题还是以笔记为准)
前提:这里我们使用了context标签所以①需要添加命名空间(spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html找这个页面里面有所有的命名空间)②(以上面导入为基础还需要导入)手动添加spring的事务涉及到了事务所以导入spring-tx-3.2.0.RELEASE.jar这样才能使用事务模板
spring底层使用 TransactionTemplate 事务模板进行操作。
操作
1.service 需要获得 TransactionTemplate
2.spring 配置模板,并注入给service
3.模板需要注入事务管理器
4.配置事务管理器:DataSourceTransactionManager ,需要注入DataSource
所以我们将修改service层代码和spring配置即可:
修改代码如下:(我这里拷的是我自己敲的代码,因为笔记上面是UserService 和t_user 而我建的是AccountService和t_Account代码逻辑是一样的至此说明)
service层:
public class AccountServiceImpl implements AccountSerivce{
private AccountDao dao;//提供set方法让spring注入
public void setDao(AccountDao dao) {
this.dao = dao;
}
//我们手动的去配置事务,要知道spring里为我们提供了TransactionTemplate事务模板我们需要手动的去spring里去配置这个模板
private TransactionTemplate transactionTemplate ;
public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
this.transactionTemplate = transactionTemplate;
}
public void convery(final String outUser, final String inUser, final Integer money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
dao.outMoney(outUser, money);//扣钱
int i=1/0;
dao.inMoney(inUser, money);//收钱
System.out.println("转账成功");
}
});
}
/*@Test
public void fun(){
AccountServiceImpl impl=new AccountServiceImpl();
impl.convery("张三", "李四", 100);
}*/
}
AccountService
spring配置文件代码:
<?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"
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"> <!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/spring"></property>
</bean> <!-- service层的实例配置 -->
<bean id="accountService" class="cn.itcast.a_transactionTemplate_shoudong.AccountServiceImpl">
<!-- 注入dao层的实例 -->
<property name="dao" ref="dao"></property>
<!-- 注入spring提供的事务模板 -->
<property name="transactionTemplate" ref="transactionTemplate"></property>
</bean> <!-- 配置dao的实例 -->
<bean id="dao" class="cn.itcast.a_transactionTemplate_shoudong.AccountDao">
<!-- 注入数据源创建jdbcTemplate -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置 TransactionTemplate的实例-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<!-- spring里提到事务就一定要创建事务管理器,所以事务模板里需要注入一个事务管理器 -->
<property name="transactionManager" ref="txManager"></property>
</bean> <!-- 创建事务管理器实例 DataSourceTransactionManager这个是关于JDBC的事务管理器
HibernateTransactionManager 这个是关于Hibernate的事务管理
事务管理依赖着数据源所以我们在事务管理里面还要注入数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
上面的修改的确解决了事务问题但是手动去添加事务比较麻烦不容易管理
2.2工厂bean生成代理【半自动】
spring提供 管理事务的代理工厂bean, TransactionProxyFactoryBean,不需要事务模板了所以Dao的代码需要修改
①.getBean() 获得代理对象
②.spring 配置一个代理
所以这个代理工厂就相当于是包装了我们的目标接口实现类的实例在里面包裹上了事务,我们spring中配置这个代理类的实例,将接口实现类类和事务管理器注入进去,还有就是事务详情也可以在里面配置。
创建工厂来得到处理事务的bean
和AOP有点像,需要接口+实现类+事务管理器(在spring里只要跟事务有关的都要事务管理器)+事务详情
所以修改后的spring配置文件如下:
<?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"
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"> <context:property-placeholder location="classpath:cn/itcast/b_transactionTemplate_TransactionProxyFactoryBean/jdbcInfo.properties "/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass" value="${jdbc.ClassDriver}"></property>
<property name="user" value="${jdbc.User}"></property>
<property name="password" value="${jdbc.Password}"></property>
<property name="jdbcUrl" value="${jdbc.JdbcUrl}"></property>
</bean> <!-- service层的实例配置 -->
<bean id="accountService" class="cn.itcast.b_transactionTemplate_TransactionProxyFactoryBean.AccountServiceImpl">
<!-- 注入dao层的实例 -->
<property name="dao" ref="dao"></property>
</bean> <!-- 配置dao的实例 -->
<bean id="dao" class="cn.itcast.b_transactionTemplate_TransactionProxyFactoryBean.AccountDao">
<!-- 注入数据源创建jdbcTemplate -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 创建工厂来得到处理事务的bean
和AOP有点像,需要接口+实现类+事务管理器(在spring里只要跟事务有关的都要事务管理器)+事务详情
-->
<bean id="proxyService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="proxyInterfaces" value="cn.itcast.b_transactionTemplate_TransactionProxyFactoryBean.AccountSerivce"></property>
<property name="target" ref="accountService"></property>
<!-- 事务管理器 -->
<property name="transactionManager" ref="txManager"></property>
<!-- 事务详情 这个属性放在了properties里面-->
<property name="transactionAttributes" >
<props>
<!-- key表示方法 *表示所有方法
PROPAGATION_REQUIRED默认传播行为
ISOLATION_DEFAULT 默认隔离级别
readOnly只读
+Exception有异常任然提交
-Exception有异常不提交
-->
<prop key="convery">PROPAGATION_REQUIRED,ISOLATION_DEFAULT,+java.lang.ArithmeticException</prop>
</props>
</property>
</bean> <!-- 创建事务管理器实例 DataSourceTransactionManager这个是关于JDBC的事务管理器
HibernateTransactionManager 这个是关于Hibernate的事务管理
事务管理依赖着数据源所以我们在事务管理里面还要注入数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
Dao层不需要事务模板类了来手动管理代码全部交给代理类来完成
public class AccountServiceImpl implements AccountSerivce{
private AccountDao dao;//提供set方法让spring注入
public void setDao(AccountDao dao) {
this.dao = dao;
}
//我们使用工厂创建事务Bean来处理事务,这样的好处是所有的事务的逻辑处理都交给spring
public void convery( String outUser, String inUser, Integer money) {
dao.outMoney(outUser, money);//扣钱
int i=1/0;
dao.inMoney(inUser, money);//收钱
System.out.println("转账成功");
}
}
Dao层的代码
测试代码:如下
public class Test01 {
@Test
public void fun01(){
String xmlPath="cn/itcast/b_transactionTemplate_TransactionProxyFactoryBean/applicationContext.xml";
ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);//加载spring配置文件
//得到代理事务的类这样才能在方法上加上事务相当于包装了一下
AccountSerivce accountSerivce=(AccountSerivce) applicationContext.getBean("proxyService");
//代理类里面已经在spring中处理好了事务
accountSerivce.convery("张三", "李四", 200);
}
}
上面的这个修改是把事务交给了工厂bean来生产代理事务的bean,这样比手动的去管理要方便很多,但是这样生成代理的方式不够灵活需要将每一个接口和实现类重复的去编写ProxyFactoryBean,既然是代理我们这里使用动态代理这样会更加减少代码量spring中的AOP编程底层就是动态代理,在spring的AOP编程有两种方式,一种是基于xml一种是基于注解,接下来我们将研究这两种动态代理处理事务的方式。
3、基于xml的AOP编程之事务管理(我们可以把事务定义在切面类里面这样就可以动态管理事务)
前提:以上面导入为基础还需要导入aop联盟规范,spring aop,aspect规范,spring aspect,这四个jar包,而且还需要导入命名空间aop和tx
关于代码我们只需要修改spring配置文件即可,将spring配置文件里面以aop编程添加事务
spring配置文件修改为:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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:property-placeholder location="classpath:cn/itcast/c_transactionTemplate_Aop/jdbcInfo.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" > <property name="driverClass" value="${jdbc.ClassDriver}"></property>
<property name="user" value="${jdbc.User}"></property>
<property name="password" value="${jdbc.Password}"></property>
<property name="jdbcUrl" value="${jdbc.JdbcUrl}"></property>
</bean> <!-- service层的实例配置 -->
<bean id="accountService" class="cn.itcast.c_transactionTemplate_Aop.AccountServiceImpl">
<!-- 注入dao层的实例 -->
<property name="dao" ref="dao"></property>
</bean> <!-- 配置dao的实例 -->
<bean id="dao" class="cn.itcast.c_transactionTemplate_Aop.AccountDao">
<!-- 注入数据源创建jdbcTemplate -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 创建事务管理器实例 DataSourceTransactionManager这个是关于JDBC的事务管理器
HibernateTransactionManager 这个是关于Hibernate的事务管理
事务管理依赖着数据源所以我们在事务管理里面还要注入数据源
-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!--
4.2 事务详情(事务通知) , 在aop筛选基础上,对ABC三个确定使用什么样的事务。例如:AC读写、B只读 等
<tx:attributes> 用于配置事务详情(属性属性)
<tx:method name=""/> 详情具体配置
propagation 传播行为 , REQUIRED:必须;REQUIRES_NEW:必须是新的
isolation 隔离级别 --> <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="convery" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> <!-- 进行AOP编程,将事务通知和切入点连接起来.目标类有ABCD(4个连接点),切入点表达式 确定增强的连接器,从而获得切入点:ABC -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.c_transactionTemplate_Aop.AccountServiceImpl.*(..))" id="myPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut" />
</aop:config>
</beans>
4、基于注解的AOP编程之事务管理
这里的注解只是给方法或者类上加上注解来代替spring基于xml的的注解配置,只需要在spring配置文件中加入:<tx:annotation-driven transaction-manager="txManager" />这样就可以基于注解来管理事务
spring的部分代码如下
<!-- 4 事务管理 -->
<!-- 4.1 事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 4.2 将管理器交予spring
* transaction-manager 配置事务管理器
* proxy-target-class
true : 底层强制使用cglib 代理
-->
<tx:annotation-driven transaction-manager="txManager"/><!--这样就可以把事务用注解来表示-->

这里我们展示service的代码,看看是如何来用注解来管理事务的
service层:如果在类上加上@Transactional注解就是将这个类所有的方法都加上事务,如果给一个方法上面加@Transactional表示只对这个方法开启事务
其中@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)还可以在注解中加入参数值分别表示传播行为和隔离级别如果不给就会是默认值(默认值分别为REQUIRED,DEFAULT)
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.DEFAULT)
public class AccountServiceImpl implements AccountSerivce{ private AccountDao dao;//提供set方法让spring注入
public void setDao(AccountDao dao) {
this.dao = dao;
} @Transactional//(表示给这个方法单独配置事务)我们使用工厂创建事务Bean来处理事务,这样的好处是所有的事务的逻辑处理都交给spring
public void convery( String outUser, String inUser, Integer money) { dao.outMoney(outUser, money);//扣钱
int i=1/0;//模仿断电后出现异常
dao.inMoney(inUser, money);//收钱
System.out.println("转账成功");
} }
二、整合spring配置文件的加载和Junit的整合(注意这里整合就需要整合jar包!)
前提:导入jar包 我们为了测试方便引用的上面事务的例子,所以jar包在上面的基础上,还需要spring-test-3.2.0.RELEASE.jar。
我们一直都是用IOC的容器来加载配置文件,我们所用到的IOC容器分别是BeanFactory和ApplicationContext,我们可以将这个加载的步骤简化一下
整合到Junit里面去!
我们展示在测试页面中如何用Junit去加载配置文件的:可以看到我们的@Autowired注解将会自动去注入AccountSerivce这个实例,使用注解前需要扫描注解类,但是和junit整合不需要扫描
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:cn/itcast/e_Junit/applicationContext.xml ")
public class Test01 {
@Autowired //与junit整合,不需要在spring xml配置扫描
private AccountSerivce accountSerivce;
@Test
public void fun01(){
//因为这些代码都是重复代码,为了简化代码我们整合Junit通知spring加载配置文件
/*String xmlPath="cn/itcast/d_transactionTemplate_annotation/applicationContext.xml";
ApplicationContext applicationContext =new ClassPathXmlApplicationContext(xmlPath);//加载spring配置文件
//得到代理事务的类这样才能在方法上加上事务相当于包装了一下
AccountSerivce accountSerivce=(AccountSerivce) applicationContext.getBean("accountService");*/ accountSerivce.convery("张三", "李四", 200); } }
三、整合spring配置文件的加载和web的整合(注意这里整合就需要整合jar包!)
上面我们整合了Junit用来加载spring配置文件,当我们做web项目时需要在第一时间去加载spring配置文件这样才能为每个实例都注入值,所以我们希望是在开启服务器时就会加载
而加载服务器会第一时间去加载web.xml,所以我们要在web.xml上动脑筋。
在学习javaweb时我们学习了三大组件,Servlet,Filter,Listener这三个组件都可以行!方式分别为:
servlet --> init(ServletConfig) --> 在web.xml上面配置<load-on-startup>2
filter --> init(FilterConfig) --> web.xml注册过滤器自动调用初始化
listener --> ServletContextListener --> servletContext对象监听在web.xml上进行配置
前提:在上面jar包的基础上,加入spring-web-3.2.0.RELEASE.jar
但是spring里面提供了一个监听器ContextLoaderListener加载文件的监听,然后在web.xml上面进行配置
<listener><listener-class>....
1.如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml

2.确定配置文件位置,通过系统初始化参数
ServletContext 初始化参数 web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
web.xml配置文件的代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- 配置spring提供的监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cn/itcast/g_web_xml/applicationContext.xml</param-value>
</context-param>
<servlet>
<servlet-name>AccountServlet</servlet-name>
<servlet-class>cn.itcast.g_web_xml.AccountServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AccountServlet</servlet-name>
<url-pattern>/AccountServlet</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.我们配置了之后就会在服务器启动时加载,但是如果想要获得这个spring配置文件的IOC容器的对象怎么办
想要获取IOC容器的实例方式有两种:
这里我们用AccountServlet来展示如何获得IOC容器对象:(其实我们可以直接将AccountServlet配置在spring里面然后将AccountService的实例注入进去)
AccountServlet代码如下:
public class AccountServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
//方式1:手动从作用域中获取
//ApplicationContext applicationContext= (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
//方式2:通过工具获取
ApplicationContext applicationContext=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
AccountSerivce accountSerivce=(AccountSerivce) applicationContext.getBean("accountService");
accountSerivce.convery("张三", "李四", 200);
response.getWriter().write("转账成功");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
四、ssh整合(struts、spring、hibernate的整合)
我们学习三大框架的版本分别为:struts:2.3.15.3、hibernate : 3.6.10、spring: 3.2.0
1.关于jar包的整合:
①.struts涉及的jar包有:
struts-2.3.15.3\apps\struts2-blank\WEB-INF\lib

struts 整合spring:struts2-spring-plugin-2.3.15.3.jar

模板技术 ,一般用于页面静态化
freemarker:扩展名:*.ftl
velocity :扩展名 *.vm
②.spring涉及的jar包有:
①基础:4+1 , beans、core、context、expression , commons-logging (struts已经导入)
②AOP:aop联盟、spring aop 、aspect规范、spring aspect
③ db:jdbc、tx
④测试:test
⑤web开发:spring web
⑥ 驱动:mysql
⑦连接池:c3p0
⑧ 整合hibernate:spring orm


③.关于hibernate涉及的jar包:
核心jar包hibernate3.jar
必须jar包在lib\required路径下:

规范接口包:路径\lib\jpa jpa规范 (java persistent api 持久api),hibernate注解开发 @Entity @Id 等
整合log4j的jar包
导入 log4j...jar (struts已经导入)
整合(过渡):slf4j-log4j12-1.7.5.jar

二级缓存所需要的第三方jar包
核心:ehcache-1.5.0.jar
依赖:
backport-util-concurrent-2.1.jar
commons-logging (存在)
注意要删除重复jar包:

2. spring整合hibernate:有hibernate.cfg.xml
day03_1spring3的更多相关文章
随机推荐
- vue element 表头添加斜线
<template> <div class="app-container"> <el-table :data="tableData3&quo ...
- python 解压、复制、删除 文件
一.python3解压文件 1.python 解压文件代码示例 如下代码主要实现zip.rar.tar.tar.gz四种格式的压缩文件的解压 def unzip_file(src_file, dst_ ...
- 你一定看得懂的 DDD+CQRS+EDA+ES 核心思想与极简可运行代码示例
前言 随着分布式架构微服务的兴起,DDD(领域驱动设计).CQRS(命令查询职责分离).EDA(事件驱动架构).ES(事件溯源)等概念也一并成为时下的火热概念,我也在早些时候阅读了一些大佬的分析文,学 ...
- C#设计模式学习笔记:(9)组合模式
本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7743118.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲结构型设计模式的第四个模式--组 ...
- Kotlin Tutorials系列文章
Kotlin Tutorials系列文章 想写一个有实用价值的Kotlin笔记, 让一线开发一看就懂, 看完就能上手. 当然官方文档更有参考价值了. 这个系列相对于官方文档的大而全来说, 最主要优势是 ...
- 第十周CTF解答
第十周write-up解题答案及过程 隐写诶 直接用WinRAR查看就能看到其flag{0ca175b9c0f726a831d895e269332461 } 第一题 将后缀名改为 rar ,发现压缩包 ...
- 初识Idea,部署Maven项目常见问题解决方案
一.idea 中项目右键没有run命令选项,没有maven的clean与install选项 解决方案:安装Maven Helper插件 二.Idea不识别java文件(类文件显示橙色) 解决方案: 第 ...
- Git 的 .gitignore 配置说明 (C#)
1.配置语法: 以斜杠“/”开头表示目录: 以星号“*”通配多个字符: 以问号“?”通配单个字符 以方括号“[]”包含单个字符的匹配列表: 以叹号“!”表示不忽略(跟踪)匹配到的文件或目录: 此外,g ...
- Object类、日期时间类、system类及StringBuilder字符串容器
一.Object类常用API 1.1 概述 java.lang.Object类是Java语言中的根类,即所有类的父类.Object类中描述的所有方法子类都可以使用.在对象实例化的时候,最终找的父类就是 ...
- Java之CheckedException
先来科普一下 CE 到底是什么吧.Java 要求你必须在函数的类型里面声明它可能抛出的异常.比如,你的函数如果是这样: void foo(string filename) throws FileNot ...