1: aop: Oriented Programming
aop term:
Joinpoint connection point: The method of the original object
Pointcut entry point: Agent has been enhanced method
Advice notice Enhancements: Enhanced Code
Target Audience: The proxy object, the original object
Waving weaving: a notification process to the entry point of the
proxy agent: weaving a target object, the proxy object is formed
section aspect: the Key + notification. Usually cut class
Notification object:
Before advice before: added before the target method code
Rear notification After: added after the target method code. Whether or not an exception will call appear. Corresponds to exception python try block is finally
Rear return notification AfterRetruning: target code following method. An exception is not called. Corresponds to exception python try block of else
Around advice AroundReturning: call before and after the target method
Abnormal notice After: After an exception occurs in the target method invocation
2: the bottom of a proxy mode
Proxy mode See previous article: Proxy mode https://blog.csdn.net/xinyuebaihe/article/details/104202951
3: Use:
Divided into configuration mode and notes
Example directory structure
Directory Structure
lib
1) configuration
section
package com.csdn.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component
public class MyAdvice {
public void before() throws Throwable {
System.out.println("这是前置通知");
}
public void after(){
System.out.println("这是后置通知");
}
public void afterReturning(){
System.out.println("这是没有异常的后置通知,相当于python中的else");
}
public void exception(){
System.out.println("这是发生异常后的通知");
}
/**
* 增强版的环绕通知。强大无比,可以替代其它通知
* @param jp
* @throws Throwable
*/
public void around2(ProceedingJoinPoint jp) throws Throwable {
System.out.println("这是环绕通知前");
try{
//前置通知
this.before();
jp.proceed();
}catch (Exception e){
//异常通知
this.exception();
}finally {
//后置通知
this.after();
}
//无异常后置通知
this.afterReturning();
System.out.println("这是环绕通知后");
}
}
Profiles
<?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.csdn.*"/>
<!--切面类,通知对象的bean-->
<bean id = "myAdvice" class="com.csdn.advice.MyAdvice"/>
<!--切面配置-->
<aop:config>
<!--切入点定义,目标方法,要增强哪些方法-->
<!--第一个星号:返回值。第二个星号:匹配的实现类。第三个星号:任意方法。两个点:方法的属性-->
<aop:pointcut id="pointcut" expression="execution(* com.csdn.service.impl.*ServiceImpl.*(..))"/>
<!--切面引入-->
<aop:aspect ref = "myAdvice">
<!--不同类型的通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
<aop:after-returning method="afterReturning" pointcut-ref="pointcut"/>
<aop:after-throwing method="exception" pointcut-ref="pointcut"/>
<aop:around method="around2" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<context:property-placeholder location="classpath*:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
</beans>
Test category
package com.csdn.test;
import com.csdn.pojo.Student;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.sql.SQLException;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class Test {
@Autowired
private StudentService studentService;
@org.junit.Test
public void test01(){
// List<Student> all = null;
// try {
// all = studentService.findAll();
//
// } catch (SQLException e) {
// e.printStackTrace();
// }
// System.out.println(all);
studentService.add();
}
}
2) by way of comment
section
<?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">
<!--包扫描。会生成bean-->
<context:component-scan base-package="com.csdn.*"/>
<!--配置自动切面生成。一定要有,要不然只有切面类注解也无效-->
<aop:aspectj-autoproxy/>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="dataSource"/>
</bean>
<context:property-placeholder location="classpath*:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${db.driver}"/>
<property name="jdbcUrl" value="${db.url}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
</beans>
Profiles
package com.csdn.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class MyAdvice2 {
@Pointcut("execution(* com.csdn.service.impl.*ServiceImpl2.*(..))")
public void pointcut(){
}
@Before("MyAdvice2.pointcut()")
public void before() throws Throwable {
System.out.println("这是前置通知");
}
@After("MyAdvice2.pointcut()")
public void after(){
System.out.println("这是后置通知");
}
@AfterReturning("MyAdvice2.pointcut()")
public void afterReturning(){
System.out.println("这是没有异常的后置通知,相当于python中的else");
}
@AfterThrowing("MyAdvice2.pointcut()")
public void exception(){
System.out.println("这是发生异常后的通知");
}
/**
* 增强版的环绕通知。强大无比,可以替代其它通知
* @param jp
* @throws Throwable
*/
@Around("MyAdvice2.pointcut()")
public void around2(ProceedingJoinPoint jp) throws Throwable {
System.out.println("这是环绕通知前");
try{
//前置通知
this.before();
jp.proceed();
}catch (Exception e){
//异常通知
this.exception();
}finally {
//后置通知
this.after();
}
//无异常后置通知
this.afterReturning();
System.out.println("这是环绕通知后");
}
}
Test category
package com.csdn.test;
import com.csdn.service.StudentService;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring2.xml")
public class Test2 {
@Autowired()
private StudentService studentService2;
@org.junit.Test
public void test01(){
// List<Student> all = null;
// try {
// all = studentService.findAll();
//
// } catch (SQLException e) {
// e.printStackTrace();
// }
// System.out.println(all);
studentService2.add();
}
}