使用Spring实现Aop
首先,使用AOP前需要导入依赖包
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.6</version>
</dependency>
接下来,先准备
1.
public interface UserService {
public void add();
public void delete();
public void update();
public void select();
}
public class UserServiceImpl implements UserService{
public void add() {
System.out.println("增加一个用户");
}
public void delete() {
System.out.println("删除一个用户");
}
public void update() {
System.out.println("修改一个用户");
}
public void select() {
System.out.println("查询一个用户");
}
}
在此之后,就该添加我们的需求了,我们来给这些基本功能添加一些前置和后置日志,
- Log
public class Log implements MethodBeforeAdvice {
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
4.AfterLog
public class AfterLog implements AfterReturningAdvice {
//returnValue:返回值
//method:要执行的目标对象的方法
//args:参数
//target:目标对象
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"方法,返回值:"+returnValue);
}
}
好了,这些都准备好之后,便要进行spring配置了
5.applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.qcp.service.UserServiceImpl"/>
<bean id="log" class="com.qcp.Log.Log"/>
<bean id="afterLog" class="com.qcp.Log.AfterLog"/>
<!--(xmlns:aop="http://www.springframework.org/schema/aop")
(http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd)-->
<!--方法一:使用原生Spring API接口-->
<!--配置aop.在此之前,需要导入aop的约束↑-->
<aop:config>
<!--首先,需要一个切入点————切入点:在什么地方执行目标对象的方法.expression=“execution(* * * * *)”,用以判断切入位置-->
<aop:pointcut id="pointcut" expression="execution(* com.qcp.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加:将某个类,放到某个切入点-->
<aop:advisor advice-ref="log" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config>
</beans>
6.applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--注册bean-->
<bean id="userService" class="com.qcp.service.UserServiceImpl"/>
<bean id="log" class="com.qcp.Log.Log"/>
<bean id="afterLog" class="com.qcp.Log.AfterLog"/>
<!--(xmlns:aop="http://www.springframework.org/schema/aop")
(http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd)-->
<!--配置aop.在此之前,需要导入aop的约束↑-->
<!--方法二:自定义类-->
<bean id="diy" class="com.qcp.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref 要引用的类-->
<aop:aspect ref="diy">
<!--PointCut 切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.qcp.service.UserServiceImpl.*(..))"/>
<!--Advice 通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
</beans>
7.测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.add();
userService.delete();
userService.update();
userService.select();
}
}