(四) 基于XML声明式AOP,实现"攻击"前后的扩展
实现思路
- 需要
spring-aop-4.3.6.RELEASE.jar
和aspectjweaver-1.8.10.jar
这两个支持的jar包 - 创建增强类(切面)
MyAspect
,无需实现任何接口 applicationContext.xml
中引入aop的命名空间- 配置
applicationContext.xml
中aspect信息 - 测试效果
1.需要spring-aop-4.3.6.RELEASE.jar
这个aop支持的jar包,里面包含了aspect信息
2. 创建增强类(切面)MyAspect
,无需实现任何接口
package com.dacangshu.ext;
/**
* @author dacangshu
* @date 2021/5/11 15:15
*/
public class MyAspect {
public void before(){
System.out.println("之前做点什么....");
}
public void after(){
System.out.println("之后做点什么....");
}
}
3.applicationContext.xml
中引入aop的命名空间
- xml头部增加如下内容:
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
- 完成版本
<?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="user" class="com.dacangshu.pojo.User">
<property name="name" value="老谢"/>
<property name="age" value="18"/>
</bean>
<!-- 增强 -->
<bean id="myAspect" class="com.dacangshu.ext.MyAspect"/>
</beans>
4. 配置applicationContext.xml
中aspect信息
- 增加如下内容
<aop:config>
<aop:aspect id="aspect" ref="myAspect">
<!-- 切入点 -->
<aop:pointcut id="userPointcut" expression="execution(* com.dacangshu.pojo.*.*(..))"/>
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="userPointcut"/>
<!-- 后置通知 -->
<aop:after method="after" pointcut-ref="userPointcut"/>
</aop:aspect>
</aop:config>
- 完整版
<?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="user" class="com.dacangshu.pojo.User">
<property name="name" value="老谢"/>
<property name="age" value="18"/>
</bean>
<!-- 增强 -->
<bean id="myAspect" class="com.dacangshu.ext.MyAspect"/>
<aop:config>
<aop:aspect id="aspect" ref="myAspect">
<!-- 切入点 -->
<aop:pointcut id="userPointcut" expression="execution(* com.dacangshu.pojo.*.*(..))"/>
<!-- 前置通知 -->
<aop:before method="before" pointcut-ref="userPointcut"/>
<!-- 后置通知 -->
<aop:after method="after" pointcut-ref="userPointcut"/>
</aop:aspect>
</aop:config>
</beans>
5.测试效果
package com.dacangshu;
import com.dacangshu.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author dacangshu
* @date 2021/5/11 13:34
*/
public class BootStrap {
public static void main(String[] args) {
// 创建上下文context对象,使用ClassPathXmlApplicationContext
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 从容器中获取id为iAttack的接口
IAttack iAttack = context.getBean("user", IAttack.class);
// 调用接口中的attack
iAttack.attack();
}
}
输出结果
之前做点什么....
name = 老谢, 发动一次攻击
之后做点什么....
注意: 仍然要使用接口
6. 其它JoinPoint
可以传入
package com.dacangshu.ext;
import org.aspectj.lang.JoinPoint;
/**
* @author dacangshu
* @date 2021/5/11 15:15
*/
public class MyAspect {
public void before(JoinPoint joinpoint) {
System.out.println("之前做点什么....");
}
public void after(JoinPoint joinpoint) {
System.out.println("之后做点什么....");
}
}