Spring---AOP注解开发&jdbc模板&Spring事务管理
一、AOP注解开发
此处需要回忆一遍AOP的概念。简单的来说,AOP就是利用动态代理技术,做到不触动源代码但却扩展了功能。那么就需要一个被扩展的对象和一个“新的功能”,例如说给某类的saveUser方法增加输出日志需求,此时输出日志该事件就是一个“新的功能”。举这个例子的重点只是为了突出,AOP技术需要关注两个方面,一个是接受者,而另一个是输出点。要利用AOP,必然需要将这两个对象做出关联。而所谓AOP注解开发,就是如何将这两个对象建立关系。
当然,以上理解仅仅为个人的话语解读,不具有严谨的用词,有任何理解错误,望不吝赐教。
在AOP的xml开发中,xml核心配置如下
<aop:config>
<--切入点(接受对象)-->
<aop:pointcut expression="execution(* com.itheima.service.impl.*.*(..))" id="aa"/>
<--切面(新功能)-->
<aop:aspect ref="logger">
<aop:before method="log" pointcut-ref="aa"/>
</aop:aspect>
</aop:config>
因此,注解只需要做到清楚表达以上两点内容,再配套一些基本的要求,例如托管类。此外,为了让程序的复用性更强,我们只需要对“新功能”对象做处理,告诉它要为谁增强方法即可做到关联,以下为实例:
- 导入jar包 aop联盟包、 aspectJ实现包 、 spring-aop-xxx.jar 、 spring-aspect-xxx.jar
- 导aop约束 ,打开扫描约束开关<aop:aspectj-autoproxy/>
- 托管两个类,在类上加注解@Compone
- 然后再“新的功能”类上配置注解
@Aspect //这个注解,是和aop有关,用于表示该类是一个功能扩展类,专门用来做增强的。
public class Logger {
@Before("execution(* com.gaga.service.impl.*.*(..))")
public void log(){
System.out.println("输出日志了~~~!");
}
二、jdbc模板
所谓jdbc模板,就是Spring对于dao层的技术支持。和DBUtils的功能的目的是一样的,就是为了操作数据库。回想以下,如果什么框架都没有,只有java原生代码提供给我们的prepareStatement、createStatement、遍历resultSet那一套东西。那我们的操作便会过于臃肿。
虽然操作数据库有更专业的框架,例如Hibernate\Mybatis等等。但作为一个负责Service层的框架,自然会有向两边扩展的思想,因此便有了该部分模板。
spring其实对dao层的大部分技术有提供模板支持

- 例子:
public void testDemo(){
//数据源,连数据库。 连什么类型的数据, 哪个库, 账号 & 密码
DriverManagerDataSource dataSource =new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///stus");
dataSource.setUsername("root");
dataSource.setPassword("root");
//1. 创建jdbc模板
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource);
String sql = "insert into t_user values(null , ? , ? )";
//2. 执行添加操作,其实它的用法和以前的queryrunner一样。
jdbcTemplate.update(sql , "66666","zhangsan");
}
2.配置C3P0连接池(注入写法)
为了方便修改数据库连接信息,通常在src下使用properties文件来记录他们。主要是方便修改
- 导入Jar包 c3p0-0.9.5.2.jar mchange-commons-java-0.2.12.jar
- 配置xml
- 在src下新建一个properties文件,内容如下 名:jdbc.properties
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///user
user=root
password=root
- 在xml里面导入jdbc.properties ,并且在dataSource里面引用
<bean id="us" class="com.gaga.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean> <bean id="userDao" class="com.gaga.dao.impl.UserDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 使用c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
- 在xml里面导入jdbc.properties ,并且在dataSource里面引用
以上配置将service\dao\jdbcTemplate\连接池都用IOC做了托管,并且用了DI注入,也就是说1.例子中的一堆堆代码可以简化为如下。只需要声明模板,然后直接可以使用,创建对象什么的都交给Spring。核心便是这个
@Repository("ud")
public class UserDaoImp implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
System.out.println("userDao的set方法---");
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void save() {
String sql = "insert into bbb values(null,?)";
jdbcTemplate.update(sql, "66666666");
}
}
三、Spring事务管理
正如标题所述,Spring会对事务做管理。但需要注意只是做管理,实际操作还是依据不同的框架的做法(不同的实现)。
这里不得不提,Spring其中的伟大之一在于AOP。那么我们就可以利用它来对某部分功能加入事务的功能。而,当我们要利用Spring来进行事务管理的时候,正所谓人在屋檐下,不得不低头。你用人家的,那也得遵守别人定下的规矩。
Spring统一规定,要操作事务,必须使用管理员!
管理事务的规范是 : PlatformTransactionManager
jdbc | mybatis : DataSourceTransactionManager
hibernate : HibernateTransactionManager
Spring对事务的支持,有三种写法:编程式事务、xml声明式事务、注解声明式事务。
这三种写法中,最简介的为注解式事务。以下列出三种写法的实例,推荐后者。
- 编程式事务
@Test
public void test(){ //事务的管理员是用来管理事务的,包含了打开事务、 提交事务、 回滚事务
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql:///stus");
dataSource.setUsername("root");
dataSource.setPassword("root"); DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
transactionManager.setDataSource(dataSource); //1. 创建事务的模板对象。
TransactionTemplate transactionTemplate = new TransactionTemplate();
transactionTemplate.setTransactionManager(transactionManager); //事务事务的模板对象
transactionTemplate.execute( new TransactionCallback<Object>() { //在事务里面执行这个方法。
@Override
public Object doInTransaction(TransactionStatus arg0) { //添加操作
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(dataSource); String sql = "insert into t_user values ( null , ? , ?)";
jdbcTemplate.update(sql, "123","王五"); int a = 1 / 0 ; jdbcTemplate.update(sql, "6666","赵六"); return null;
}
}); }编程式事务
- xml声明式事务
<!-- 以下属于事务的配置 如果要用事务了,那么事务的管理员是谁啊。 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!--
以上配置,到目前为止,只是说明了,如果要开启事务,管理员是谁。 但是缺少了一个东西。
就是哪一个方法到底要用事务呀? -->
<aop:config>
<aop:pointcut expression="execution(* com.gaga.service.impl.*.*(..))" id="aa"/>
<aop:advisor advice-ref="advice01" pointcut-ref="aa"/>
</aop:config> <tx:advice id="advice01" transaction-manager="transactionManager">
<tx:attributes> <!-- 对 id="aa"表达式找到的那一堆方法,进行过滤配置,表示谁才能用事务管理, 如果是* ,表示前面找到的那一堆都用事务管理 -->
<tx:method name="save*"/>
<tx:method name="update*"/>
<tx:method name="delete*"/>
</tx:attributes>
</tx:advice>
xml声明式事务
- 注解声明式事务
a)xml中的配置
<context:component-scan base-package="gaga"/>
<aop:aspectj-autoproxy/>
1. 在xml中 声明注解事务的管理员 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"/>
<property name="jdbcUrl" value="${jdbcUrl}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
</bean>
<!-- 以下属于事务的配置 如果要用事务了,那么事务的管理员是谁啊。 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 指定注解事务使用的管理员是谁 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
b) 代码配置
2. 在业务逻辑类上或者方法上打注解 类上的注解,表示类中的所有方法都用事务, 如果在方法上打注解,表示只有这个方法才会用事务
@Transactional
public class UserServiceImpl implements UserService { }
public class UserServiceImpl implements UserService {
// @Transactional
@Override
public void save() {
}
Spring---AOP注解开发&jdbc模板&Spring事务管理的更多相关文章
- Spring入门(三)— AOP注解、jdbc模板、事务
一.AOP注解开发 导入jar包 aop联盟包. aspectJ实现包 . spring-aop-xxx.jar . spring-aspect-xxx.jar 导入约束 aop约束 托管扩展类和被扩 ...
- Spring笔记04_AOP注解开发_模板_事务
目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...
- 四、spring的JDBC模板和事务管理
Spring的JDBC模板 Spring是JavaEE开发的一站式框架,对各种持久化技术都提供了简单的模板 ORM持久化技术 模板类 JDBC org.springframework.jdbc.cor ...
- java框架之Spring(3)-JDBC模板使用&事务管理
下面内容使用到的 jar 包下载 JDBC模板使用 入门 1.导包,如要导入 Spring 的基本开发包.数据库驱动包.Spring 提供的 JDBC 模板包,如下: 2.测试: @Test publ ...
- 【Spring实战】—— 16 基于JDBC持久化的事务管理
前面讲解了基于JDBC驱动的Spring的持久化管理,本篇开始则着重介绍下与事务相关的操作. 通过本文你可以了解到: 1 Spring 事务管理的机制 2 基于JDBC持久化的事务管理 Spring的 ...
- Spring aop注解失效
问题 在spring 中使用 @Transactional . @Cacheable 或 自定义 AOP 注解时,对象内部方法中调用该对象的其他使用aop机制的方法会失效. @Transactiona ...
- Spring _day02_IoC注解开发入门
1.Spring IoC注解开发入门 1.1 注解开发案例: 创建项目所需要的jar,四个基本的包(beans core context expression ),以及两个日志记录的包,还要AOP的包 ...
- Spring使用注解开发及使用java类进行配置bean
Spring使用注解开发 说明 在spring4之后,想要使用注解形式,必须得要引入aop的包 在配置文件当中,还得要引入一个context约束 <?xml version="1.0& ...
- Spring AOP注解为什么失效?90%Java程序员不知道
使用Spring Aop注解的时候,如@Transactional, @Cacheable等注解一般需要在类方法第一个入口的地方加,不然不会生效. 如下面几种场景 1.Controller直接调用Se ...
随机推荐
- HDU 1031.Design T-Shirt【结构体二次排序】【8月21】
Design T-Shirt Problem Description Soon after he decided to design a T-shirt for our Algorithm Board ...
- 从程序员角度看ELF | Linux-Programming (转)
★概要: 这片文档从程序员的角度讨论了linux的ELF二进制格式.介绍了一些ELF执行 文件在运行控制的技术.展示了如何使用动态连接器和如何动态装载ELF. 我们也演示了如何在LINUX使用GNU ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) E. Prairie Partition 二分+贪心
E. Prairie Partition It can be shown that any positive integer x can be uniquely represented as x = ...
- AndroidEventBus总结
什么是AndroidEventBus? android事件总线,是一个发布 / 订阅的事件总线 github地址:https://github.com/greenrobot/EventBus Andr ...
- Chapter 20: Diagnostics
WHAT'S IN THIS CHAPTER?n Code contractsn Tracingn Event loggingn Performance monitoringWROX.COM CODE ...
- 高负载linux调优
调整Linux内核参数: # vi /etc/sysctl.conf# tells the Kernel it's ok if services bind to non-existant IP ADD ...
- CentOS 7下修改rabbitmq打开文件数量方法
以下为使用systemd的修改方法: 1.系统层修改: 通过修改sysctl配置,提高系统的打开文件数量 vim /etc/sysctl.conf,添加: fs.file-max = 65535 ...
- Newtonsoft.Json.dll 反序列化JSON字符串
上一篇JSON博客<JSON入门级学习小结--JSON数据结构>中已对JSON做了简单介绍,JSON字符串数组数据样式大概是这样子的: 如今因为项目需求(asp.net web网站,前台向 ...
- 从0开始学习Hadoop(2)安装JDK以及设置SSH
安装JDK 使用ppa/源方式安装 1.添加ppa sudo add-apt-repository ppa:webupd8team/java sudo apt-get update 2.安装oracl ...
- 【194】Windows 上使用 wget
本文包括两部分,首先就是在 Windows 使用 wget 来下载文件,这样固然很好,然而问题并非这么简单,在 PowerShell 4.0 版本中增加了 Invoke-WebRequest 的别名 ...