目录
5、在applicationContext.xml中配置植入关系
1、导入坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
2、创建目标类和目标方法
public void save() {
System.out.println("save run....");
}
3、创建切面类(内部有增强方法)
public void before(){
System.out.println("前置增强");
}
4、将目标类和切面类的对象创建圈交给spring
<!-- 目标对象-->
<bean id="target" class="com.itheima.aop.Target"></bean>
<!-- 切面-->
<bean id="myAspect" class="com.itheima.aop.MyAspect"></bean>
5、在applicationContext.xml中配置植入关系
<!-- 配置织入,告诉spring框架,哪些方法需要增加那些增强-->
<aop:config>
<!-- 声明切面-->
<aop:aspect ref="myAspect">
<!-- 切面。切点+通知-->
<aop:before method="before" pointcut="execution(public void com.itheima.aop.Target.save())"></aop:before>
</aop:aspect>
</aop:config>
6、测试代码(Spring测试)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}