##一、 方式二:Spring自定义类来实现Aop
依然完成我们之前的demo,实现userServiceImpl日志功能
- 写我们自己的一个切入类
public class DiyPointCut {
public void before(){
System.out.println("======方法执行前======");
}
public void after(){
System.out.println("======方法执行后======");
}
}
- 去spring中配置
切面—对应一个类,之前方式一的配置是切入点,这里是切面
<?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-->
<bean id="userService" class="com.lding.service.UserServiceImpl"></bean>
<bean id="log" class="com.lding.log.Log"></bean>
<bean id="afterlog" class="com.lding.log.AfterLog"></bean>
<!-- 方式二:自定义类实现-->
<bean id="diy" class="com.lding.diy.DiyPointCut"></bean>
<aop:config>
<!-- 自定义切面,ref为要引用的类 -->
<aop:aspect ref="diy">
<!-- 切入点-->
<aop:pointcut id="point" expression="execution(* com.lding.service.UserServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="point"></aop:before>
<aop:after method="after" pointcut-ref="point"></aop:after>
</aop:aspect>
</aop:config>
</beans>
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//动态代理代理的是接口
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
}
测试结果
二、利用注解方式实现AOP
- 编写一个注解实现的增强类
//方式三:使用注解方式实现aop
@Aspect//标注这个类是一个切面
public class AnnotationPointcut {
@Before("execution(* com.lding.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("-----方法执行前-------");
}
@After("execution(* com.lding.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("-----方法执行后-------");
}
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点;
@Around("execution(* com.lding.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前:");
Signature signature = joinPoint.getSignature();//获得签名
System.out.println("signature:"+signature);
//执行方法
Object proceed = joinPoint.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
}
- 第二步:在Spring配置文件中,注册bean,并增加支持注解的配置
<!--方式三-->
<bean id="annotationPointCut" class="com.lding.config.AnnotationPointcut"></bean>
<!-- 开启注解支持!-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
aop:aspectj-autoproxy:说明
通过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动态代理。