第三种方式:使用 AspectJ 开发 AOP – Annotation
也就是通过注入的方式,可以极大的减少xml的配置
所需要的jar包
需要的Dao类
package com.aop;
public interface UserDao {
public void addUser(String id, String name);
}
需要的实现类:
注入解读:
@Repository("userDao") 表示自动注入UserDaoImpl这个类,功能等同于:UserDao userDao = new UserDao();
package com.aop;
import org.springframework.stereotype.Repository;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Override
public void addUser(String id, String name) {
String uid=null;
uid.charAt(0);
System.out.println("=======addUser============方法逻辑执行");
}
}
切面类(代理类):
web开发,提供3个@Component注解衍生注解(功能一样)取代:
@Repository(“名称”):dao层
@Service(“名称”):service层
@Controller(“名称”):web层
@Autowired:自动根据类型注入
@Qualifier(“名称”):指定自动注入的id名称
@Resource(“名称”)
@ PostConstruct 自定义初始化
@ PreDestroy 自定义销毁
@Component 为通配注入
@Aspect:作用是把当前类标识切面类,告诉xml需要扫描这个类
@Pointcut:切入点,就是需要在哪个类中的方法作为切入点:com.aop.UserDao.addUser(..) ,并且以myPointCut()方法作为切入点的方法。等同于addUser() = myPointCut()
@Around:环绕增强,相当于MethodInterceptor
@AfterReturning:后置增强,相当于AfterReturningAdvice,方法正常退出时执行
@Before:标识一个前置增强方法,相当于BeforeAdvice的功能,相似功能的还有
@AfterThrowing:异常抛出增强,相当于ThrowsAdvice
@After: final增强,不管是抛出异常或者正常退出都会执行
package com.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
//切入点
@Pointcut("execution(* com.aop.UserDao.addUser(..))")
public void myPointCut() {
}
//before
@Before("myPointCut()")
public void myBefore(JoinPoint joinpoint) {
System.out.println("---------------before start");
}
//after
@After("myPointCut()")
public void myAfter(JoinPoint joinpoint) {
System.out.println("---------------after start");
}
//around
@Around("myPointCut()")
public Object myAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("---------------around start");
Object obj = proceedingJoinPoint.proceed();
System.out.println("---------------around end");
return obj;
}
//after returning
@AfterReturning("myPointCut()")
public void myAfterReturning(JoinPoint joinpoint) {
System.out.println("---------------after returning start");
}
//throwing
@AfterThrowing(value="myPointCut()", throwing="e")
public void myAfterThrowing(JoinPoint joinpoint, Throwable e) {
System.out.println("---------------error"+e);
}
}
xml配置文件:
会开启自动切面的代理,通过扫描com包下的所有类:
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com"/>
<!-- 开启切面的自动代理 -->
<aop:aspectj-autoproxy/>
</beans>
测试:
package com.aop;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class TestUserDao {
@Test
void testAddUser() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean("userDao", UserDao.class);
userDao.addUser("zhm", "zqw");
}
}
测试结果: