AOP详解
AOP理解:
AOP–aspect oriented programing 面向切面编程
将业务进行分离,或者说将可变的部分剥离出来,降低耦合,能够在调用某个对象的方法前、中、后回调监听类的方法。
4.1、添加依赖/引入jar包
jar包:aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar
pom添加依赖:
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
<!-- <scope>runtime</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.3</version>
</dependency>
4.2、使用注解的方式装载bean
步骤:
4.2.1、在applicationContext.xml中配置自动注入的扫描路径
<context:component-scan base-package="pojo"/>
<context:component-scan base-package="aspect"/>
4.2.3、定义需要监听的方法,已经监听响应的动作
Landlord.java (包租婆类,只关心签合同和收租)
package pojo;
import org.springframework.stereotype.Component;
@Component("landlord")
public class Landlord {
public void service(){
System.out.println("签合同");
System.out.println("收房租");
}
}
Broker.java (房屋中介类,需要处理包租婆以外的事情)
package aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("broker")
@Aspect
public class Broker {
//方式一:直接创建界面
/*@Before("execution(* pojo.Landlord.service())")
public void before(){
System.out.println("带租客看房");
System.out.println("谈价格");
}
@After("execution(* pojo.Landlord.service())")
public void after(){
System.out.println("交钥匙");
}*/
//方式二:创建切点
/*@Pointcut("execution(* pojo.Landlord.service())")
public void lServie(){}
@Before("lServie()")
public void before(){
System.out.println("带租客看房");
System.out.println("谈价格");
}
@After("lServie()")
public void after(){
System.out.println("交钥匙");
}*/
//方式三:采用环绕通知
@Around("execution(* pojo.Landlord.service())")
public void around(ProceedingJoinPoint joinPoint){
System.out.println("带租客看房");
System.out.println("谈价格");
try {
joinPoint.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("交钥匙");
}
}
测试类:
@Test
public void testAOP(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
Landlord landlord = context.getBean(Landlord.class);
landlord.service();
}
参考:
解决 “通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明” 问题
解决办法就是复制完整的命名空间
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>