AOP(面向切面编程)
百度百科定义:
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
Aop相关概念:
-
Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。(所谓连接点是指那些被连接到的点,在Spring中,这些点指的就是方法)
-
Pointcut(切入点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。(指我们要对那些Joint point进行拦截的定义,通常是一个表达式)
-
Advice(通知):Advice 定义了在 Pointcut(切入点) 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。(l拦截完要做的事)
-
Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut (切入点)以及相应的 Advice(通知)。
-
Target(目标对象):织入 Advice(通知) 的目标对象.。
-
Weaving(织入):织入的过程其实就是Spring AOP帮我们把切面中的代码织入到目标代码中的过程。
切入点 (Pointcut)
在 Spring AOP 中,需要使用 AspectJ 的切点表达式来定义切点。
切点表达式点连接
AspectJ 指示器 | 描述 |
---|---|
execution () | 用于匹配连接点的执行方法 最常用 |
args () | 限制连接点的指定参数为指定类型的执行方法 |
@args () | 限制连接点匹配参数类型由指定注解标注的执行方法 |
this () | 限制连接点匹配 AOP 代理的 Bean 引用为指定类型的类 |
target () | 限制连接点匹配特定的执行对象,目标对象是指定的类型 |
@target () | 限制连接点匹配特定的执行对象,这些对象对应的类要具备指定类型注解 |
within() | 限制连接点匹配指定类型,比如哪个包下,或哪个类里面 |
@within() | 限制连接点匹配指定注释所标注的类型(当使用 Spring AOP 时,方法定义在由指定的注解所标注的类里) |
@annotation | 限制匹配带有指定注释的连接点 |
声明一个切入点
<aop:config>
<!-- 定义公用的切入点表达式,如果aspect中有多个通知,都可以通过pointcut-ref复用 -->
<aop:pointcut id="all_calc_method" expression="execution(* com.nan.spring.simple.calc.CalcImpl.*(..))" />
<aop:aspect ref="calcAspect">
<!-- 切面包含的通知(什么时间)、切入点(什么地点) -->
<aop:around method="computeTime" pointcut-ref="all_calc_method" />
</aop:aspect>
</aop:config>
通知(Advice)
通知 | 描述 |
---|---|
环绕通知(around) | ①在目标方法执行前、后被通知, 可以获取连接点对象(ProceedingJoinPoint, 该对象可以获取被拦截方法的签名、参数、返回值、包括调用与否) ②该方法的返回值,即代表了真正业务逻辑代码的返回值③可以选择终止或正常执行目标方法 |
前置通知(before) | 在目标方法调用前通知切面, 什么参数也无法获取。也不能终止目标方法执行 |
后置(返回值)通知(after returning) | 只有在目标方法 正常 执行结束后才会通知, 在通知方法中可以获取到方法的返回值 |
后置(最终)通知 (after) | 在目标方法执行结束后通知切面, 什么参数也无法获取。无论目标方法是正常执行结束还是抛出异常终止,都会被通知 |
异常通知(after throwing) | 只有在目标方法 出现异常 才会通知, 在通知方法中可以获取到抛出的异常信息 |
声明通知
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..))"/>
<!-- a before advice definition -->
<aop:before pointcut-ref="businessService"
method="doRequiredTask"/>
<!-- an after advice definition -->
<aop:after pointcut-ref="businessService"
method="doRequiredTask"/>
<!-- an after-returning advice definition -->
<!--The doRequiredTask method must have parameter named retVal -->
<aop:after-returning pointcut-ref="businessService"
returning="retVal"
method="doRequiredTask"/>
<!-- an after-throwing advice definition -->
<!--The doRequiredTask method must have parameter named ex -->
<aop:after-throwing pointcut-ref="businessService"
throwing="ex"
method="doRequiredTask"/>
<!-- an around advice definition -->
<aop:around pointcut-ref="businessService"
method="doRequiredTask"/>
...
</aop:aspect>
</aop:config>
<bean id="aBean" class="...">
...
</bean>
注解
声明一个切点(注解)
@Component//相当于XML中的<bean>
public class EatLemon implements Eat {
/**
* 通过@Pointcut注解定义切入点表达式
*/
@Pointcut("execution(* com.lanou3g.spring.eat.EatLemon.*(..))")
public String eat() {
String dd ="吃了柠檬,我酸了";
System.out.println(dd);
return dd;
}
声明一个切面和通知(注解)
@Aspect//定义一个切面
@Component
public class First {
//指定该方法是一个环绕通知,通知注解的参数代表引用一个切入点表达式
@Around("execution(* com.lanou3g.spring.eat.EatLemon.*(..))")
public void look(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("看见某些东西");
//调用目标方法
Object retVal = joinPoint.proceed();
System.out.println("你呢");
}
}
入口类(注解)
/**
* 入口类
*/
//相当于Xml中的<beans>
@Configuration
//扫描路径下的注解
@ComponentScan( basePackages = "com.lanou3g.spring.eat")
//开启对AOP相关注解的处理
@EnableAspectJAutoProxy
public class AppByAnnotation {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByAnnotation.class);
Eat el = ctx.getBean(Eat.class);
el.eat();
}
}
控制台输出结果(注解)
XML
现在XML中定义目标类
<!-- 原本要执行的代码-->
<bean id="eat" class="com.lanou3g.spring.eat.EatLemon"/>
定义一个切面类
<!-- 切面类-->
<bean id="aspect" class="com.lanou3g.spring.eat.First"/>
定义切点和通知
<!-- 切面类-->
<bean id="aspect" class="com.lanou3g.spring.eat.First"/>
<aop:config>
<aop:pointcut id="first" expression="execution(* com.lanou3g.spring.eat.EatLemon.*(..))"/>
<aop:aspect ref="aspect">
<!-- 环绕通知可以终止目标方法,环绕通知目标方法前后做事的通知 -->
<aop:around method="look" pointcut-ref="first"/>
</aop:aspect>
</aop:config>
入口类(xml)
/**
* XML
*/
public class App {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Eat el = ctx.getBean(Eat.class);
el.eat();
}
}
控制台结果
Spring 编程式事务和声明式事务的区别
编程式事务处理:所谓编程式事务指的是通过编码方式实现事务,允许用户在代码中精确定义事务的边界。即类似于JDBC编程实现事务管理。管理使用TransactionTemplate或者直接使用底层的PlatformTransactionManager。对于编程式事务管理,spring推荐使用TransactionTemplate。
声明式事务处理:管理建立在AOP之上的。其本质是对方法前后进行拦截,然后在目标方法开始之前创建或者加入一个事务,在执行完目标方法之后根据执行情况提交或者回滚事务。声明式事务最大的优点就是不需要通过编程的方式管理事务,这样就不需要在业务逻辑代码中掺杂事务管理的代码,只需在配置文件中做相关的事务规则声明(或通过基于@Transactional注解的方式),便可以将事务规则应用到业务逻辑中。
简单地说,编程式事务侵入到了业务代码里面,但是提供了更加详细的事务管理;而声明式事务由于基于AOP,所以既能起到事务管理的作用,又可以不影响业务代码的具体实现。
事务的七种传播行为
PROPAGATION_REQUIRED | 支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。 |
---|---|
PROPAGATION_SUPPORTS | 支持当前事务,如果当前没有事务,就以非事务方式执行。 |
PROPAGATION_MANDATORY | 支持当前事务,如果当前没有事务,就抛出异常。 |
PROPAGATION_REQUIRES_NEW | 新建事务,如果当前存在事务,把当前事务挂起。 |
PROPAGATION_NOT_SUPPORTED | 以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。 |
PROPAGATION_NEVER | 以非事务方式执行,如果当前存在事务,则抛出异常。 |
PROPAGATION_NESTED | 如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。 |
备注:常用的两个事务传播属性是1和4,即PROPAGATIONREQUIRED PROPAGATIONREQUIRES_NEW
五种隔离级别
级别 | 说明 |
---|---|
ISOLATION_DEFAULT | 这是一个PlatfromTransactionManager默认的隔离级别,使用数据库默认的事务隔离级别. |
ISOLATIONREADUNCOMMITTED | 这是事务最低的隔离级别,它允许另外一个事务可以看到这个事务未提交的数据。 这种隔离级别会产生脏读,不可重复读和幻像读。 |
ISOLATIONREADCOMMITTED | 保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。 这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。 |
ISOLATIONREPEATABLEREAD | 这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。 它除了保证一个事务不能读取另一个事务未提交的数据外,还保证了避免下面的情况产生(不可重复读)。 |
ISOLATION_SERIALIZABLE | 这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。 除了防止脏读,不可重复读外,还避免了幻像读。 |
关键词:
幻读(虚读)
事务1读取记录时事务2增加了记录并提交,事务1再次读取时可以看到事务2新增的记录;
通俗的说,幻读就是指在一个事务内读取了别的事务插入的数据,导致前后读取不一致(insert)
不可重复读取
事务1读取记录时,事务2更新了记录并提交,事务1再次读取时可以看到事务2修改后的记录;
在一个事务内读取表中的某一行数据,多次读取结果不同.一个事务读取到了另一个事务提交后的数据.
脏读
事务1更新了记录,但没有提交,事务2读取了更新后的行,然后事务T1回滚,现在T2读取无效。
通俗的说,脏读就是指一个事务读取了一个未提交事务的数据
XML方式
配置数据源
数据源依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
<!-- 通过xml方式配置Spring声明式事务 配置start -->
<context:component-scan base-package="com.lanou3g.spring.transaction" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="teacherDao" class="com.lanou3g.spring.transaction.dao.TeacherDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>
<!-- 第一步:配置数据源 -->
<!--引入外部jdbc.properties配置文件-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--引用外部jdbc.properties配置文件里的配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="username" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
<property name="connectionProperties">
<props>
<prop key="characterEncoding">${jdbc.characterEncoding}</prop>
</props>
</property>
</bean>
初始化事务管理器
<!-- 第二步:初始化事务管理器 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
配置事务AOP通知
<!-- 第三步:配置事务AOP通知 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<!--只读 ,回滚 -->
<tx:method name="insert*" rollback-for="ArithmeticException" />
<tx:method name="query*" isolation="READ_COMMITTED" read-only="true" />
</tx:attributes>
</tx:advice>
定义AOP配置(将上面的通知和表达式组装到一起)
<!-- 第四步:定义AOP配置(将上面的通知和表达式组装到一起) -->
<aop:config>
<aop:pointcut id="all_dao_method" expression="execution(* com.lanou3g.spring.transaction.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="all_dao_method" />
</aop:config>
注解
数据源
/**
* 配置数据源
*/
@PropertySource("classpath:jdbc.properties")
@Component
public class MyDataSource extends DriverManagerDataSource{
public MyDataSource(@Value("${jdbc.driver}") String driver, @Value("${jdbc.url}") String url, @Value("${jdbc.user}") String userName, @Value("${jdbc.password}") String password, @Value("${jdbc.characterEncoding}") String characterEncoding) {
super.setDriverClassName(driver);
super.setUrl(url);
super.setUsername(userName);
super.setPassword(password);
Properties conProperties = new Properties();
conProperties.setProperty("characterEncoding", characterEncoding);
super.setConnectionProperties(conProperties);
}
}
// 此注解和@Component作用一样, 只是含有特定的语义(一般用来标注dao层的类)
@Setter
@Getter
@Repository
public class TeacherDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate;
/**
* 查询teacher表所有数据
* @return
*/
// 通过@Transactional 开启事务、设置事务属性
@Transactional(readOnly = true)
public List<Teacher> queryAll() {
//List<Teacher> teachers = jdbcTemplate.query("select * from teacher", new BeanPropertyRowMapper<Teacher>(Teacher.class));
//return teachers;
int rows = jdbcTemplate.update("delete from teacher;");
System.out.println("影响了" + rows + "行.");
return null;
}
/**
* 插入teacher
* @param teacher
* @return 返回影响行数
*/
// 通过@Transactional 开启事务、设置事务属性
@Transactional(rollbackFor = {ArithmeticException.class})
public int insertTeacher(Teacher teacher) {
// 通过伪代码演示如果我们手写事务控制代码的套路
// setAutoCommit(false);
// beginTransaction
// try{
int result = jdbcTemplate.update("insert into teacher (tname) values (?)", teacher.getTname());
// int retVal = 9 / 0;
// commit;
// } catch (Exception e) {
// rollback;
// }
//
return result;
}
}
/**
* 纯注解方式入口类
*/
@Slf4j
@Configuration
@ComponentScan(basePackages = "com.lanou3g.spring.transaction.annotation")
// 开启事务相关注解支持
@EnableTransactionManagement
public class AppByTransactionAnnotation {
/**
* 定义JdbcTemplate对象,Spring给我们封装了所有的JDBC操作
* @param dataSource
* @return
*/
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* 定义事务管理器bean
* @param dataSource
* @return
*/
@Bean
public PlatformTransactionManager transactionManager(DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppByTransactionAnnotation.class);
testQuery(ctx);
// testTransaction(ctx);
}
static void testQuery(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
List<Teacher> teacherList = teacherDao.queryAll();
for(Teacher teacher : teacherList) {
System.out.println("id: " + teacher.getId()+", tname: " + teacher.getTname());
}
}
static void testTransaction(ApplicationContext ctx) {
TeacherDaoImpl teacherDao = ctx.getBean(TeacherDaoImpl.class);
int rowEffect = teacherDao.insertTeacher(new Teacher("周伯通"));
System.out.println("影响了" + rowEffect + "行.");
}
}