使用 Spring 实现 AOP
使用 Spring 实现 AOP
- 方式一:使用Spring的API接口 [主要SpringAPI接口实现]
- 方式二:自定义来实现AOP [主要是切面定义]
- 方式三:使用注解实现
1、使用AOP织入,需要首先导入一个依赖包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
2、代码测试
向原有业务中添加日志功能
1、编写UserService接口:
public interface UserService{
public void add();
public void delete();
public void update();
public void query();
}
2、编写UserServiceImpl接口实现类
//真实对象
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 query(){
System.out.println("查询了一个用户!");
}
}
3、编写Log日志类
添加方式一:使用Spring的API接口
public class BeforeLog implements MethodBeforeAdvice {
//method:要执行的切入方法
//args:参数
//target:目标对象
public void before(Method method, object[] args, object target) throws Throwable {
System.out.print1n(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
public class AfterLog implements AfterReturningAdvice {
//returnValue:返问值
public void afterReturning(object returnValue, Method method, Object[] args, object target) throws Throwable {
System.out.print1n("执行了"+method.getName ()+ "返回结果为: " +returnValue);
}
}
添加方式二:使用自定义diy类来实现aop
public class DiyPointCut {
public void before(){
System.out.printn("=======方法执行前=======");
}
public void after(){
System.out.println("======方法执行后=======");
}
}
添加方式三:使用注解实现
@Aspect //标注这个类是一个切面
public class AnnotationPointCut {
@Before("execution(* com.qilu.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("====方法执行前=====");
}
@After("execution(* com.qilu.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("====方法执行后=====");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.qilu.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp){
System.out.println("执行前");
//执行方法
Object proceed = jp.proceed();
System.out.println("执行后");
}
}
4、向applicationContext.xml容器中注入bean,并配置aop
方式一:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userService" class="com.qilu.service.UserServiceImpl" />
<bean id="beforelog" class="com.qilu.log.BeforeLog" />
<bean id="afterlog" class="com.qilu.log.AfterLog" />
<!-- 方式一:使用原生Spring API接口 -->
<!-- 配置aop:需要导入aop约束 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.qilu.service.UserServiceImpl.*(..))" />
<!-- 执行环绕增强 -->
<aop:advisor advice-ref="beforelog" pointcut-ref="pointcut" />
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut" />
</aop:config>
</beans>
方式二:
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userService" class="com.qilu.service.UserServiceImpl" />
<!-- 方式二:自定义类 -->
<bean id ="diy" class="com.qilu.DiyPointCut" />
<aop:config>
<!-- 自定义切面,ref 要引用的类 -->
<aop:aspect ref="diy">
<!-- 切入点 -->
<aop:pointcut id="pointcut" expression="execution(* com.qilu.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"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 注册bean -->
<bean id="userService" class="com.qilu.service.UserServiceImpl" />
<!-- 方式三:使用注解 -->
<bean id="annotationPointCut" class="com.qilu.log.annotationPointCut" />
<!--开启注解支持! JDK(默认 proxy-target-class="false") cglib(proxy-target-class="true") -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
5、编写测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXml1App1icationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
使用 Spring 实现 AOP
原文地址:https://www.cnblogs.com/abc991314/p/13416061.html