要引入包:aopalliance-1.0.jar
package com.spring.jdkTest;
//接口
public interface Iuser {
void printOne();
void printTwo();
}
package com.spring.jdkTest;
//目标类
public class UserImpl implements Iuser {
@Override
//连接点
public void printOne() {
System.out.println("One");
}
@Override
//连接点
public void printTwo() {
System.out.println("Two");
}
}
package com.spring.advice;
//通知类
import java.lang.invoke.MethodHandleInfo;
import java.lang.reflect.Method;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
public class Advice implements org.aopalliance.intercept.MethodInterceptor{
@Override
public Object invoke(MethodInvocation pass) throws Throwable {
System.out.println("前");
Object proceed =pass.proceed();//执行目标方法
System.out.println("后");
return proceed;
}
}
//xml
<?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"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--设置目标类对象 -->
<bean id="targetObject" class="com.spring.jdkTest.UserImpl"></bean>
<!--增强通知类的对象 -->
<bean id="myAdvice" class="com.spring.advice.Advice"></bean>
<bean id="targetProxyobj" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--获得接口 -->
<property name="interfaces" value="com.spring.jdkTest.Iuser"></property>
<!-- 获得目标类,名字一定要用target -->
<property name="target" ref="targetObject"></property>
<!-- 获得通知类,名字一定要用interceptorNames,注意是用value 而不是ref -->
<property name="interceptorNames" value="myAdvice"></property>
<!--加了name="optimize" value="true" 底层为Cglib代理,否则默认为jdk -->
<property name="optimize" value="true"></property>
</bean>
</beans>
package com.spring.jdkTest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AutomaticTest{
@Test
public void test() {
// TODO 自动生成的方法存根
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//传递工厂对象
Iuser user = context.getBean("targetProxyobj",Iuser.class);
user.printOne();
user.printTwo();
}
}