文章目录
前言
1.maven坐标请到上一篇文章提取,地址:Aspectj AOP实现流程(基于XML)
2.在实验前建议先创建项目骨架
一、创建目标接口和目标类(包含切点)
创建TargetInterface接口
public interface TargetInterface {
public void save();
}
创建Target类实现TargetInterface接口
public class Target implements TargetInterface {
public void save() {
System.out.println("save running...");
}
}
二、创建切面类(包含增强方法)
创建MyAspect类
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
}
三、将目标类和切面类的对象创建权交给spring
使用注解标注Target类
@Component("target")
public class Target implements TargetInterface {
public void save() {
System.out.println("save running...");
}
}
使用注解标注MyAspect类
@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
}
四、在切面类中使用注解配置织入关系
创建MyAspect 类
@Component("myAspect")
@Aspect //标注当前类是切面类
public class MyAspect {
//配置切入点
@Before("execution(* com.hbh.anno.*.*(..))")
public void before(){
System.out.println("前置增强....");
}
}
五、在配置文件中开启组件扫描和AOP的自动代理
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
">
<!--组件扫描-->
<context:component-scan base-package="com.hbh.anno"/>
<!--aop自动代理-->
<aop:aspectj-autoproxy/>
</beans>
六、测试代码
创建AopTest 类,测试织入效果
五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
五月 01, 2022 3:39:21 下午 org.springframework.test.context.support.AbstractTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3a03464, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2d3fcdbd, org.springframework.test.context.support.DirtiesContextTestExecutionListener@617c74e5]
五月 01, 2022 3:39:21 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
五月 01, 2022 3:39:21 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@c038203: startup date [Sun May 01 15:39:21 CST 2022]; root of context hierarchy
前置增强....
save running...