这是我认为相对简单好理解的一种方法

话不多说直接上码

UserInterface接口

package henu.rjxy.myinterface;

public interface UserInterface {
  public void smile();
}

User继承UserInterface接口

package henu.rjxy.bean;

import henu.rjxy.myinterface.UserInterface;

public class User implements UserInterface {

	@Override
	public void smile() {
		// TODO Auto-generated method stub
		System.out.println(\"user smile\");
	}

}

BeforeAdvisor继承MethodBeforeAdvice接口
实现接口MethodBeforeAdvice该拦截器会在调用方法前执行
实现接口AfterReturningAdvice该拦截器会在调用方法后执行
实现接口MethodInterceptor该拦截器会在调用方法前后都执行

package henu.rjxy.aspect;

import java.lang.reflect.Method;

import org.spring work.aop.MethodBeforeAdvice;

public class BeforeAdvisor implements MethodBeforeAdvice  {

	@Override
	public void before(Method method,  [] obj,   args)
			throws Throwable {
		// TODO Auto-generated method stub
		System.out.println(\"Smile......\");
	}

}

中的配置

<?  version=\"1.0\" encoding=\"UTF-8\"?>
<beans  ns=\"http://www.spring work.org/schema/beans\"
     ns:xsi=\"http://www.w3.org/2001/ Schema-instance\"
    xsi:schemaLocation=\"http://www.spring work.org/schema/beans
        http://www.spring work.org/schema/beans/spring-beans.xsd\">
        
  <bean id=\"beforadvisor\" class=\"henu.rjxy.aspect.BeforeAdvisor\"/>
  
  <bean id=\"user\" class=\"henu.rjxy.bean.User\"/>
  
  <bean id=\"userProxy\" class=\"org.spring work.aop. work.ProxyFactoryBean\">
    <!-- 代理的目标对象,即想要在哪个类上增加代理 -->
    <property name=\"target\" ref=\"user\"/>
    <!-- 代理类 -->
    <property name=\"interceptorNames\">
      <value>beforadvisor</value>
    </property>
    <!-- 代理类所需要的接口 -->
    <property name=\"proxyInterfaces\">
      <value>henu.rjxy.myinterface.UserInterface</value>
    </property>
  </bean>
</beans>

JUnit中测试

package henu.rjxy.test;

import static org.junit.Assert.*;
import henu.rjxy.myinterface.UserInterface;

import org.spring work.context.ApplicationContext;
import org.spring work.context.support.ClassPath ApplicationContext;

public class Test {

	@org.junit.Test
	public void test() {
		ApplicationContext act = new ClassPath ApplicationContext(\"application. \");
		UserInterface uProxy = (UserInterface)act.getBean(\"userProxy\");
		uProxy.smile();
	}

}

测试效果
\"在这里插入图片描述\"

收藏 打印