使用aspectj实现Spring的aop操作
AOP是Spring框架中的一个重要内容,我们今天通过aspectj框架来实现Spring的aop操作。
方法一:使用表达式配置切入点。
1.准备相关的jar包
除了spring基础的包以外我们还要准备相关的aspectj包和aop包
2.编写类
在com.itheima.aop包中创建两个类,一个book类,一个myBook类
book.java(原业务)
package com.itheima.aop;
public class book {
public void add() {
System.out.println("add........");
}
}
myBook.java(添加的业务)
package com.itheima.aop;
public class myBook {
public void before() {
System.out.println("before........");
}
}
3.编写Spring配置文件
在src包中创建bean.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">
<!-- 为book、myBook类创建对象 -->
<bean id="book" class="com.itheima.aop.book"/>
<bean id="myBook" class="com.itheima.aop.myBook"/>
<!-- 配置aop操作 -->
<aop:config>
<!--使用表达式配置切入点
execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)-->
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.itheima.aop.book.*(..))" id="pointcut1"/>
<!-- 配置切面 -->
<aop:aspect ref="myBook">
<!-- 配置增强类型
method:增强类里面使用哪个方法作为前置 -->
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
<!-- bean definitions here -->
</beans>
4.编写测试类
在com.itheima.aop包中创建TestAop类
package com.itheima.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
book book=(book) applicationContext.getBean("book");
book.add();
}
}
运行
myBook类中的before()方法已经加载到了book的add()方法中,而且是前置通知。
方法二:基于aspecj的注解aop操作。
1.将上述中的myBook类中添加aop注释
package com.itheima.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class myBook {
//aop前置通知
@Before(value = "execution(* com.itheima.aop.book.*(..))")
public void before() {
System.out.println("before........");
}
//aop后置通知
@After(value = "execution(* com.itheima.aop.book.*(..))")
public void after() {
System.out.println("after........");
}
}
2.修改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: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="book" class="com.itheima.aop.book"/>
<bean id="myBook" class="com.itheima.aop.myBook"/>
<!-- 开启aop操作 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--
<aop:config>
<aop:pointcut expression="execution(* com.itheima.aop.book.*(..))" id="pointcut1"/>
<aop:aspect ref="myBook">
<aop:before method="before" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
-->
<!-- bean definitions here -->
</beans>
运行
可见前置通知和后置通知都加载完成。