Spring-----Aop
https://snailclimb.gitee.io/javaguide/#/
AOP(Aspect Oriented Programming) 意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
横切关注点: 跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 …
切面(ASPECT): 横切关注点 被模块化 的特殊对象。即,它是一个类。
通知(Advice): 切面必须要完成的工作。即,它是类中的一个方法。
目标(Target): 被通知对象。
代理(Proxy): 向目标对象应用通知之后创建的对象。
切入点(PointCut): 切面通知 执行的 “地点”的定义。
连接点(JointPoint): 与切入点匹配的执行点。
- Spring中Aop的实现
实现前导入织入包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
1. 使用SpringAPI进行实现
- 编写增强类
//前置增强类
public class BeforeLog implements MethodBeforeAdvice {
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println( o.getClass().getName() + "的" + method.getName() + "方法被执行了");
}
}
//后置增强类
public class AfterLog implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了" + target.getClass().getName()
+"的"+method.getName()+"方法,"
+"返回值:"+returnValue);
}
}
- service接口
public interface UserService {
public void add();
public void delete();
public void update();
public void search();
}
- service接口实现类
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加方法");
}
public void delete() {
System.out.println("删除方法");
}
public void update() {
System.out.println("更新方法");
}
public void search() {
System.out.println("查询方法");
}
}
- applicatiomContext.xml
<?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 id="userService" class="com.lyb.Service.Impl.UserServiceImpl"/>
<bean class="com.lyb.log.AfterLog" id="afterLog"/>
<bean class="com.lyb.log.BeforeLog" id="beforeLog"/>
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.lyb.Service.Impl.UserServiceImpl.*(..))"/>
<!--配置切面 切面执行方法 切入点-->
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 测试
public class SpringTest {
@Test
public void Test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
2. 使用自定义类实现
- 编写自定义类
public class MyPointcut {
public void before(){
System.out.println("-----方法执行前-----");
}
public void after(){
System.out.println("-----方法执行后-----");
}
}
- applicationContext.xml
<?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 id="userService" class="com.lyb.Service.Impl.UserServiceImpl"/>
<bean id="myPointcut" class="com.lyb.MyPointcut"/>
<aop:config>
<!--切面 引用的类-->
<aop:aspect ref="myPointcut">
<!--切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.lyb.Service.Impl.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
3.使用注解进行实现
- 编写一个类AnnotationPointcut
@Aspect //切面
public class AnnotationPointcut {
@After("execution(* com.lyb.Service.Impl.UserServiceImpl.*(..))")
public void before(){
System.out.println("=========方法执行前");
}
@Before("execution(* com.lyb.Service.Impl.UserServiceImpl.*(..))")
public void after(){
System.out.println("=========方法执行后");
}
@Around("execution(* com.lyb.Service.Impl.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);
}
}
- applicationContext.xml
<?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 id="userService" class="com.lyb.Service.Impl.UserServiceImpl"/>
<bean class="com.lyb.Service.AnnotationPointcut" id="annotationPointcut"/>
<!--开启aop注解支持-->
<aop:aspectj-autoproxy/>
</beans>
注意:<aop:aspectj-autoproxy/>
开启aop注解支持