文章目录
一、AOP
使用AOP的织入,需要导入一个依赖包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.7</version>
</dependency>
业务接口UserService:
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
实现类UserServiceImpl :
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 select() {
System.out.println("查询成功");
}
}
1、通过 Spring API 实现
注册bean且实现aop切入实现
BeforeLog:
public class BeforeLog implements MethodBeforeAdvice {
@Override
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
AfterLog:
public class AfterLog implements AfterReturningAdvice {
@Override
//returnValue:返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回结果为:"+returnValue);
}
}
配置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.Akatsuki.service.UserServiceImpl"/>
<bean id="log" class="com.Akatsuki.log.Log"/>
<bean id="afterLog" class="com.Akatsuki.log.AfterLog"/>
<!--aop的配置-->
<aop:config>
<!--切入点 expression:表达式匹配要执行的方法-->
<aop:pointcut id="pointcut" expression="execution(* com.Akatsuki.service.UserServiceImpl.*(..))"/>
<!--执行环绕 advice-ref执行方法 . pointcut-ref切入点-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 动态代理的是接口
// UserService userService = context.getBean("userService", UserService.class);
UserService userService = (UserService) context.getBean("userService");
userService.add();
}
结果:
2、通过自定义类实现
自定义类:
public class DiyPointCut {
public void before(){
System.out.println("方法执行前");
}
public void after(){
System.out.println("方法执行后");
}
}
配置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.Akatsuki.service.UserServiceImpl"/>
<bean id="log" class="com.Akatsuki.log.Log"/>
<bean id="afterLog" class="com.Akatsuki.log.AfterLog"/>
<!--aop的配置-->
<bean id="diy" class="com.Akatsuki.diy.DiyPointCut"/>
<aop:config>
<aop:aspect ref="diy">
<aop:pointcut id="diys" expression="execution(* com.Akatsuki.service.UserServiceImpl.*(..))"/>
<!--pointcut-ref不能与上面的aspect ref同名-->
<aop:before method="before" pointcut-ref="diys"/>
<aop:after method="after" pointcut-ref="diys"/>
</aop:aspect>
</aop:config>
</beans>
结果
3、通过注解实现
注解类:
@Before("execution(* com.Akatsuki.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("方法执行前");
}
@After("execution(* com.Akatsuki.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("方法执行后");
}
@Around("execution(* com.Akatsuki.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
System.out.println("签名:"+jp.getSignature());
//执行目标方法proceed
Object proceed = jp.proceed();
System.out.println("环绕后");
System.out.println(proceed);
}
配置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.Akatsuki.service.UserServiceImpl"/>
<bean id="log" class="com.Akatsuki.log.Log"/>
<bean id="afterLog" class="com.Akatsuki.log.AfterLog"/>
<!-- aop的配置-->
<!-- 方式三:-->
<bean id="annocation" class="com.Akatsuki.Annotation.AnnotationPointcut"/>
<aop:aspectj-autoproxy/>
</beans>