Spring注解开发系列Ⅵ --- AOP&事务
注解开发 --- AOP
AOP称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等待,Struts2的拦截器设计就是基于AOP的思想,横向重复,纵向抽取。详细的AOP介绍请看这里,本篇主要是讨论AOP在spring注解开发中的运用。
AOP的使用
1.导入aop模块(spring-aspects)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
2.定义一个业务逻辑类(com.wang.aop.MathCalculator),在业务逻辑运行时将日志进行打印(方法之前,方法结束,方法异常都会打印)。
public class MathCalculator {
public int div(int x,int y){
return x/y;
}
}
3.定义一个日志切面类(com.wang.aop.LogAspects),切面类的方法需要动态感知MathCalculator的div方法运行到哪里,然后执行通知方法。
通知方法:
1).前置通知(@Before):logStart(),在目标方法运行之前运行
2).后置通知(@After):logEnd(),在目标方法运行之后运行(无论方法正常结束或异常结束都会调用)
3).返回通知(@AfterReturning):logRet(),在目标方法正常返回之后执行
4).异常通知(@AfterThrowing):logException(),在目标方法运行异常之后运行
5).环绕通知(@Around):动态代理,手动推进目标方法运行(joinPoint.procced())
4.给切面类的方法标注通知注解
/**
* 切面类
*/
@Aspect
public class LogAspects {
//抽取公共表达式
//本类引用:pointCut()
//其他切面类引用:com.wang.aop.LogAspects.pointCut()
//@Pointcut("execution(public int com.wang.aop.MathCalculator.div(int ,int ))")
@Pointcut("execution(public int com.wang.aop.MathCalculator.*(..))") public void pointCut(){ }
//在目标方法之前切人,切入点表达式
@Before("pointCut()")
public void logStart(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName()+"方法开始运行...@Before,参数列表是"+Arrays.asList(joinPoint.getArgs()));
}
@After("pointCut()")
public void logEnd(JoinPoint joinPoint){
System.out.println(joinPoint.getSignature().getName()+"方法结束...@After,参数列表是"+Arrays.asList(joinPoint.getArgs()));
}
@AfterReturning(value = "pointCut()",returning = "result")
public void logRet(JoinPoint joinPoint,Object result){ //joinPoint必须放在参数第一位,否则则会报错
System.out.println(joinPoint.getSignature().getName()+"方法结果打印...@AfterReturning,运行结果"+result);
}
@AfterThrowing(value = "pointCut()",throwing = "exception")
public void logException(Exception exception){
System.out.println("方法异常...@AfterThrowing,异常结果"+exception);
}
}
5.将切面类和业务逻辑类(目标方法所在类)都加入到容器中
@Configuration
@EnableAspectJAutoProxy //开启spring的aop注解功能
public class AOPConfig {
@Bean
public MathCalculator calculator(){
return new MathCalculator();
}
@Bean
public LogAspects logAspects(){
return new LogAspects();
}
}
6.必须告诉spring哪个类是切面类(给切面类加上注解@Aspect)(@Aspect)
7.给配置类中添加@EnableAspectJAutoProxy,开启基于注解的AOP模式
注意:
1.使用aop的对象不能自己new创建,需要去spring容器中获取,否则AOP方法不会执行
2.JoinPoint必须放在参数第一位,否则会报错
注解开发 --- 事务
Spring事务其实就是Spring AOP,底层创建动态代理对象,在代码的开头结尾封装了开启事务和事务回滚操作,关于事务的基本使用。
声明式事务
1.环境搭建:导入相关依赖:数据源,数据库驱动
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
2.配置数据源,spring-jdbc模块(jdbctmplate,也可以导入mybatis以及hibernate,jpa)
@Bean
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("123456");
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
return comboPooledDataSource;
}
3.给方法标注:@Transactional 表示当前方法是一个事务方法
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void insert(){
String sql = "INSERT INTO `tb_user`(username,age) VALUES(?,?)";
String substring = UUID.randomUUID().toString().substring(0, 5);
jdbcTemplate.update(sql,substring,11);
}
}
@Service
public class UserService {
@Autowired
private UserDao userDao;
@Transactional
public void insertUser(){
userDao.insert();
int i = 1/0;
System.out.println("插入成功");
}
}
4.配置类上加上@EnableTransactionManagement开启基于注解的事务管理
@ComponentScan({"com.wang.tx","com.wang.service","com.wang.dao"})
@Configuration
@EnableTransactionManagement
public class TxConfig {
@Bean
public DataSource dataSource() throws PropertyVetoException {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("123456");
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
return comboPooledDataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource){
//spring对Configuration类有特殊处理,给容器中加组件的方法,多次调用都会从容器中找组件
JdbcTemplate template = new JdbcTemplate(dataSource);
return template;
}
@Bean
public PlatformTransactionManager platformTransactionManager(DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
}
5.在配置类配置事务管理器控制事务
测试结果:查看数据库,若抛出异常没有数据插入到数据库说明事务注解生效了
public class TxTest {
@Test
public void txTest(){
AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TxConfig.class);
UserService userservice = annotationConfigApplicationContext.getBean(UserService.class);
userservice.insertUser();
annotationConfigApplicationContext.close();
}
}
Spring注解开发系列Ⅵ --- AOP&事务的更多相关文章
- Spring注解开发系列专栏
这个系列主要是讲Spring注解的使用,可以为后面SpringBoot的学习带来一定的帮助.我觉得从Spring直接过度到SpringBoot还是有点快,还是得需要一个演变的过程.从Spring开发, ...
- Spring注解开发系列VIII --- SpringMVC
SpringMVC是三层架构中的控制层部分,有过JavaWEB开发经验的同学一定很熟悉它的使用了.这边有我之前整理的SpringMVC相关的链接: 1.SpringMVC入门 2.SpringMVC进 ...
- spring注解开发-声明式事务(源码)
1. 环境搭建与测试 1)导入相关依赖 数据源.数据库驱动.Spring-jdbc模块 <dependency> <groupId>org.springframework< ...
- Spring注解开发系列Ⅰ--- 组件注册(上)
传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点:1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文件 ...
- Spring注解开发系列Ⅴ --- 自动装配&Profile
自动装配: spring利用依赖注入和DI完成对IOC容器中各个组件的依赖关系赋值.自动装配的优点有: 自动装配可以大大地减少属性和构造器参数的指派. 自动装配也可以在解析对象时更新配置. 自动装配的 ...
- Spring注解开发系列VII --- Servlet3.0
Servlet3.0简介 Servlet 3.0 作为 Java EE 6 规范体系中一员,随着 Java EE 6 规范一起发布.该版本在前一版本(Servlet 2.5)的基础上提供了若干新特性用 ...
- Spring注解开发系列Ⅱ --- 组件注册(下)
1.@Import注册组件 @Import主要功能是通过导入的方式实现把实例加入springIOC容器中, /** * 给容器注册组件 * 1.包扫描+组件标注注解(@Controller,@Serv ...
- Spring注解开发系列Ⅲ --- 生命周期
Bean的生命周期 Spring Bean 的生命周期在整个 Spring 中占有很重要的位置,掌握这些可以加深对 Spring 的理解. 首先看下生命周期图: 再谈生命周期之前有一点需要先明确: S ...
- Spring注解开发系列Ⅳ --- 属性赋值
在Spring框架中,属性的注入我们有多种方式,我们可以通过构造方法注入,可以通过set方法注入,也可以通过p名称空间注入,方式多种多样,对于复杂的数据类型比如对象.数组.List集合.map集合.P ...
随机推荐
- 2020年. NET Core面试题
第1题,什么是ASP net core? 首先ASP net core不是 asp net的升级版本.它遵循了dot net的标准架构, 可以运行于多个操作系统上.它更快,更容易配置,更加模块化,可扩 ...
- 0003 HTML常用标签(含base、锚点)、路径
学习目标 理解: 相对路径三种形式 应用 排版标签 文本格式化标签 图像标签 链接 相对路径,绝对路径的使用 1. HTML常用标签 首先 HTML和CSS是两种完全不同的语言,我们学的是结构,就只写 ...
- Linux 学习笔记 4 创建、复制、移动、文件的基本操作
写在前面 通过上一节的学习,我们基本的了解到在Linux 里面对于设备的挂载.卸载以及设备存在的目录.挂载目录.都有了一个基本的了解 本节主要了解文件.以及目录的相关操作,比如文件.目录的创建.以及删 ...
- 那天晚上和@FeignClient注解的深度交流
废话篇 那晚,我和@FeignClient注解的深度交流了一次,爽! 主要还是在技术群里看到有同学在问相关问题,比如: contextId是干嘛的?name相同的多个Client会报错? 然后觉得有必 ...
- my_mysql
###一键偷懒YUM安装MySQbL### 1.安装mysql数据库 #yum install -y mariadb-server mariadb 2.登录mysql数据库常用选项 -h:指定服务端 ...
- javascript-void keyword
javascript-void keyword 写在前面 ECMA-262定义了ECMAScript所支持的关键字(keyword),关键字不能用作ECMAScript程序的标识符(Indetifie ...
- 基于CentOS 7 部署MySQL 5.7的基本操作
关闭selinux # sed -i '/SELINUX/s/enforcing/disabled/' /etc/selinux/config重启后生效# sestatus 修改提示符配置# vi / ...
- linux下配置vnc-server 和gnome-session
机器比较老,安装时间也十分久远,所以也不知道实验室系统当时是不是完全安装,最近需要使用vnc登录显示界面,结果问题就来了...没有安装vnc-server. (1)机器系统是rhel6.2的,所以就从 ...
- react super中的props
有的小伙伴每次写组件都会习惯性在constructor和super中写上props,那么这个是必要的吗?? 首先要明确很重要的一点就是: 可以不写constructor,一旦写了constructor ...
- Hyperledger Fabric1.4 安装
Hyperledger Fabric 依赖的软件版本查看官方 github 地址 https://github.com/hyperledger/fabric 下文件 /docs/source/prer ...