AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
提供声明式事务;允许用户自定义切面;
以下名词需要了解下:
SpringAOP中,通过Advice定义横切逻辑,Spring中支持5种类型的Advice:
即 Aop 在 不改变原有代码的情况下 , 去增加新的功能(就是代理模式的功能);
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>StudySpring</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring_009_aop</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
package com.zibo.service;
public interface UserService {
void add();
void delete();
void update();
void select();
}
package com.zibo.service;
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("添加了一个用户");
}
@Override
public void delete() {
System.out.println("删除了一个用户");
}
@Override
public void update() {
System.out.println("修改了一个用户");
}
@Override
public void select() {
System.out.println("查询了一个用户");
}
}
package com.zibo.log;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.lang.Nullable;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
/**
* 方法执行之前执行的方法,也就是传说中的前置通知
* @param method 要执行的方法
* @param args 给要执行的方法所传的参数
* @param target 代理对象
* @throws Throwable 异常
*/
@Override
public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
assert target != null;
System.out.println(target.getClass().getName() + "类的" + method.getName() + "方法要执行了!");
}
}
package com.zibo.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogAfter implements AfterReturningAdvice {
/**
* 方法执行之后执行的方法,也就是传说中的后置通知
* @param returnValue 要执行的方法的返回值
* @param method 要执行的方法
* @param args 给要执行的方法所传的参数
* @param target 代理对象
* @throws Throwable 异常
*/
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + method.getName() + "方法,返回结果为:" + returnValue);
}
}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.zibo.service.UserServiceImpl"/>
<bean id="log" class="com.zibo.log.Log"/>
<bean id="logAfter" class="com.zibo.log.LogAfter"/>
<!--配置AOP,需要导入AOP的约束-->
<aop:config>
<!--切入点:expression表达式,expression(要执行的位置)-->
<aop:pointcut id="pointcut" expression="execution(* com.zibo.service.UserServiceImpl.*(..))"/>
<!--执行环绕-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>
</aop:config>
</beans>
package com.zibo;
import com.zibo.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService service = context.getBean("userService", UserService.class);
service.add();
}
}
com.zibo.service.UserServiceImpl类的add方法要执行了!
添加了一个用户
执行了add方法,返回结果为:null
package com.zibo.diy;
public class DIYPointCut {
public void before(){
System.out.println("======方法执行前======");
}
public void after(){
System.out.println("======方法执行后======");
}
}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.zibo.service.UserServiceImpl"/>
<bean id="log" class="com.zibo.log.Log"/>
<bean id="logAfter" class="com.zibo.log.LogAfter"/>
<!--配置AOP,需要导入AOP的约束-->
<!--方式一:使用原生Spring API接口-->
<!-- <aop:config>-->
<!-- <!–切入点:expression表达式,expression(要执行的位置)–>-->
<!-- <aop:pointcut id="pointcut" expression="execution(* com.zibo.service.UserServiceImpl.*(..))"/>-->
<!-- <!–执行环绕–>-->
<!-- <aop:advisor advice-ref="log" pointcut-ref="pointcut"/>-->
<!-- <aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>-->
<!-- </aop:config>-->
<!--方式二:使用自定义类-->
<bean id="diy" class="com.zibo.diy.DIYPointCut"/>
<aop:config>
<!--自定义切面,ref:要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.zibo.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
======方法执行前======
添加了一个用户
======方法执行后======
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>StudySpring</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>spring_009_aop</artifactId>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<scope>runtime</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
</dependencies>
</project>
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.zibo.service.UserServiceImpl"/>
<bean id="log" class="com.zibo.log.Log"/>
<bean id="logAfter" class="com.zibo.log.LogAfter"/>
<!--配置AOP,需要导入AOP的约束-->
<!--使用注解实现-->
<bean id="diy" class="com.zibo.diy.DIYPointCut2"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
</beans>
package com.zibo.diy;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标准这个类是一个切面
public class DIYPointCut2 {
@Before("execution(* com.zibo.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("======方法执行前======");
}
@After("execution(* com.zibo.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("======方法执行后======");
}
}
======方法执行前======
添加了一个用户
======方法执行后======
package com.zibo.diy;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect //标准这个类是一个切面
public class DIYPointCut2 {
@Before("execution(* com.zibo.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("======方法执行前======");
}
@After("execution(* com.zibo.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("======方法执行后======");
}
@Around("execution(* com.zibo.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable{
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
//执行目标方法proceed
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
环绕前
签名:void com.zibo.service.UserService.add()
======方法执行前======
添加了一个用户
环绕后
null
======方法执行后======
通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring 在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy />隐藏起来了;
<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理;