Spring实现Aop的方式
前言
为什么要用Spring实现AOP?
我们不希望增加新功能的同时改变了原有的代码,因此我们使用Aop来保持原有的代码同时可以新增功能。
方式一:使用原生Spring API接口
使用
这里我们先建立几个实体类和测试类
项目目录如下所示:
AfterLog
package com.cjh.log;
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
// returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
Log
package com.cjh.log;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
AfterLog和Log属于我们增加的功能,在原有的基础上面增加的。
下面是主类
UserService
package com.cjh.service;
public interface UserService {
public void add();
public void delate();
public void update();
public void select();
}
UserServicelmpl
package com.cjh.service;
public class UserServicelmpl implements UserService{
public void add(){
System.out.println("增加了一名用户!");
}
public void delate(){
System.out.println("删除了一名用户");
}
public void update(){
System.out.println("修改了一名用户");
}
public void select(){
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-->
<bean id="userService" class="com.cjh.service.UserServicelmpl"/>
<bean id="log" class="com.cjh.log.Log"/>
<bean id="afterlog" class="com.cjh.log.AfterLog"/>
<!--方式一:使用原生Spring API接口-->
<!--配置aop:需要导入aop的约束-->
<aop:config>
<!--切入点:我们需要在哪里执行spring expression表达式
execution(要执行的位置!)
-->
<aop:pointcut id="pointcut" expression="execution(* com.cjh.service.UserServicelmpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
测试类
MyTest
import com.cjh.service.UserService;
import com.cjh.service.UserServicelmpl;
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 userService = (UserService) context.getBean("userService");
userService.add();
}
}
这里要注意的是,动态代理代理的是接口,而不是一个类,如果写一个类进去代理,会报错
方式二:自定义类实现AOP
创建一个自定义的类
我们通过一个切入面
DiyPointCut
package com.cjh.diy;
public class DiyPointCut {
public void before(){
System.out.println("========方法执行前========");
}
public void after(){
System.out.println("========方法执行后========");
}
}
applicationContext.xml
<!--方式二:自定义类来实现aop-->
<bean id="diy" class="com.cjh.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--切入点-->
<aop:pointcut id="point" expression="execution(* com.cjh.service.UserServicelmpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
测试
import com.cjh.service.UserService;
import com.cjh.service.UserServicelmpl;
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 userService = (UserService) context.getBean("userService");
userService.add();
}
}
结果
这种方法相对来说,比较简单
方式三:注解的方法
创建一个类
AnnotationPointCut
package com.cjh.diy;
//方式三:使用注解方式来实现aop
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 AnnotationPointCut {
//注解实现切入点
@Before("execution(* com.cjh.service.UserServicelmpl.*(..))")
public void before(){
System.out.println("======方法执行前======");
}
@After("execution(* com.cjh.service.UserServicelmpl.*(..))")
public void after(){
System.out.println("======方法执行后======");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.cjh.service.UserServicelmpl.*(..))")
public void around(){
}
}
applicationContext.xml
<!--方式三 使用注解实现-->
<bean id="annotationPointCut" class="com.cjh.diy.AnnotationPointCut"/>
<!--开启注解支持 jdk(proxy-target-class="false"(默认) cglib(proxy-target-class="true")-->
<aop:aspectj-autoproxy/>
小结
用注解来实现的话代码比较简单也很明了,但是容错率比较低。
总结
AOP就是在不影响原有代码的基础上实现动态的增强