一·AOP面向切面编程
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。底层是动态代理,意思就是横向开发,在一般的纵向开发中,为某些方法进行横向处理,添加前后日志等。
二·springAPI实现AOP
导入jar包:
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
编写两个日志类,分别实现spring的两个接口:
@Component
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"方法开始执行了");
}
}
@Component
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getSimpleName()+"的"+method.getName()+"执行完毕,返回值为"+returnValue);
}
}
编写一个业务中的UserService接口,以及实现类:
public interface UserService {
void add();
void delete();
void update();
void query();
}
@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 query() {
System.out.println("查询了一个用户");
}
}
现在用将日志前后切入到这业务的四个方法中:
先添加注解context的约束,和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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
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:
<context:component-scan base-package="com.he"/>
添加切入点,以及切入方法:
<!-- 方式一:通过springAPI接口来实现切入-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.he.service.UserService.*(..))"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userservice = context.getBean("userServiceImpl", UserService.class);
userservice.add();
}
结果:
三·自定义切面
@Component
public class DiyAspect {
public void before(){
System.out.println("=======执行方法前=======");
}
public void after(){
System.out.println("=======执行方法后=======");
}
}
<!-- 方式二:通过自定义切面来实现切入-->
<aop:config>
<aop:aspect ref="diyAspect">
<aop:pointcut id="pointcut" expression="execution(* com.he.service.UserService.*(..))"/>
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
注意:自定义的日志类都要装入到容器里面!
四·注解实现
编写一个AnnotationDiy类:
用一个空方法,上面自定义一个切入点:
@Pointcut("execution(* com.he.service.UserService.*(..))")
public void pointcut(){}
后面真正要切入的方法,方法上加@Before或@After直接引用方法名即可。
@Before("pointcut()")
public void before(){
System.out.println("=======方法执行前=======");
}
@After("pointcut()")
public void after(){
System.out.println("=======方法执行后=======");
}
也可以抽取成finnal常量,实现复用,必须要加finnal关键字!
final String pc = "execution(* com.he.service.UserService.*(..))";
@Aspect
@Component
public class AnnotationDiy {
@Pointcut("execution(* com.he.service.UserService.*(..))")
public void pointcut(){}
//final String pc = "execution(* com.he.service.UserService.*(..))";
@Before("pointcut()")
public void before(){
System.out.println("=======方法执行前=======");
}
@After("pointcut()")
public void after(){
System.out.println("=======方法执行后=======");
}
}
最后,在xml文件中开启自动代理:
<!-- 方式三:注解实现-->
<aop:aspectj-autoproxy/>
测试:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userservice = context.getBean("userServiceImpl", UserService.class);
userservice.add();
}
结果:
注意:动态代理代理的是接口!