applicationContext.xml

<? The 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"> <! - the target object - > < bean id = "target" class = "com. Itheima. Aop. TargetImpl" / > <! Class =" com.itheima.aop.myaspect "/> <! <aop:config> <aop:config> <! <aop:aspect ref="myAspect"> <! - section: Notice + tangent point - - > < aop: before method = "before" pointcut = "execution (a public void com. Itheima. Aop. TargetImpl. The save ())" / > </aop:aspect> </aop:config> </beans>Copy the code

Target classes and interfaces

package com.itheima.aop; public interface Target { void save(); } package com.itheima.aop; public class TargetImpl implements Target{ public void save(){ System.out.println("----save---"); }}Copy the code

section

package com.itheima.aop; Public class MyAspect {void before(){system.out.println (" prepackage "); } void after(){system.out.println (" after "); }}Copy the code

The test class:

package com.itheima.aop;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestAop {
  @Autowired
  private Target target;
  @Test
  public void testSave(){
    target.save();
  }
}
Copy the code