Spirng框架一般都是基于AspectJ实现AOP操作
AspectJ不是Spring组成部分,独立于AOP框架,一般把AsPectJ和Spring框架一起使用,进行AOP操作。
package com.spring.spring5.annotaion;
//被增强的类
public class User {
public void add(){
System.out.println("add......");
}
}
在增强类里面,创建不同的方法,让不同的方法代表不同的通知类型
package com.spring.spring5.annotaion;
//增强的类
public class UserProxy {
//前置通知
public void before(){
System.out.println("before......");
}
}
<?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">
<!--开启组件扫描-->
<context:component-scan base-package="com.spring.spring5"></context:component-scan>
</beans>
package com.spring.spring5.annotaion;
import org.springframework.stereotype.Component;
//被增强的类
@Component
public class User {
public void add(){
System.out.println("add......");
}
}
package com.spring.spring5.annotaion;
import org.springframework.stereotype.Component;
//增强的类
@Component
public class UserProxy {
//前置通知
public void before(){
System.out.println("before......");
}
}
package com.spring.spring5.annotaion;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
//增强的类
@Aspect//表示生成代理对象
@Component
public class UserProxy {
//前置通知
public void before(){
System.out.println("before......");
}
}
<?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">
<!--开启组件扫描-->
<context:component-scan base-package="com.spring.spring5"></context:component-scan>
<!--开启AspectJ生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
在增强类里面,在作为通知方法上面添加类型注解,使用切入点表达式配置
切入点表达式语法参考
相同切入点的抽取
package com.spring.spring5.annotaion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
//增强的类
@Aspect//表示生成代理对象
@Component
public class UserProxy {
//前置通知
@Before(value = "execution(* com.spring.spring5.annotaion.User.add(..))")//表示作为前置通知
public void before(){
System.out.println("Before......");
}
//最终通知,不管有没有异常在方法之后都执行
@After(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void after(){
System.out.println("After......");
}
//后置通知(返回通知),方法无异常结束时执行
@AfterReturning(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void afterReturning(){
System.out.println("AfterReturning......");
}
//异常通知,add()有异常时才会执行
@AfterThrowing(value = "execution(* com.spring.spring5.annotaion.User.add(..))")
public void afterThrowing(){
System.out.println("AfterThrowing......");
}
//环绕通知,在方法之前和之后执行
@Around("execution(* com.spring.spring5.annotaion.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("Around之前......");
proceedingJoinPoint.proceed();
System.out.println("Around之后......");
}
}
package com.spring.spring5.annotaion.test;
import com.spring.spring5.annotaion.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnotation {
@Test
public void test(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
User user = context.getBean("user", User.class);
user.add();
/*
Around之前......
Before......
add.......
Around之后......
After......
AfterReturning......
*/
}
}
配置类:
package com.spring.spring5.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration//配置类
@ComponentScan(basePackages = "com.spring.spring5")//开启组件扫描
@EnableAspectJAutoProxy(proxyTargetClass = true)//开启AspectJ生成代理对象
public class Config {
}
测试:
@Test
public void test2(){
ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
User user = context.getBean("user", User.class);
user.add();
/*
Person Before......
UserProxy Before......
add.......
*/
}
Book类:
package com.spring.spring5.xml;
//被增强类
public class Book {
public void buy(){
System.out.println("buy......");
}
}
BookProxy类:
package com.spring.spring5.xml;
//增强类
public class BookProxy {
public void before() {
System.out.println("before......");
}
}
<?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 id="bookProxy" class="com.spring.spring5.xml.BookProxy"></bean>
<bean id="book" class="com.spring.spring5.xml.Book"></bean>
<!--配置AOP的增强-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="p" expression="execution(* com.spring.spring5.xml.Book.buy(..))"></aop:pointcut>
<!--配置切面-->
<aop:aspect ref="bookProxy">
<!--增强作用在具体的方法上-->
<aop:before method="before" pointcut-ref="p"></aop:before>
</aop:aspect>
</aop:config>
</beans>
package com.spring.spring5.xml;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestXml {
@Test
public void test() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-xml.xml");
Book book = context.getBean("book", Book.class);
book.buy();
/*
before......
buy......
*/
}
}