版权声明:仅供学习交流使用 https://blog.csdn.net/drxRose/article/details/84845477
ProxyFactoryBean | xml配置中,实现代理工厂的类 |
属性 | 定义 |
target | 代理的目标对象 |
proxyInterfaces | 代理需要实现的接口 |
interceptorNames | 需要weaving的目标对象的bean列表,这些bean列表 org.springframework.cglib.proxy.MethodInterceptor或 org.springframework.aop.Advisor. 调用的先后顺序按配置的先后顺序. |
singleton | 返回的代理实例是否是单例模式的 |
optimize | 是否强制设置cglib代理模式 |
proxyTargetClass | 是否对类进行代理 |
目标对象类
package siye;
public class TargetObj
{
public void work()
{
System.out.println("work");
}
public void throwOwn()
{
try
{
throw new RuntimeException();
} catch (Exception e)
{
}
}
}
目标对象新增特性的接口
package siye;
public interface NewFeature
{
void eat();
}
前置通知实现类
package siye;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MethodBeforeAdviceImpl implements MethodBeforeAdvice
{
@Override
public void before(Method method, Object[] args, Object target)
throws Throwable
{
System.out.println("方法即将被调用.");
}
}
返回通知实现类
package siye;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterReturningAdviceImpl implements AfterReturningAdvice
{
@Override
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable
{
System.out.println("方法已返回,返回值是," + returnValue);
}
}
环绕通知实现类
package siye;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MethodInterceptorImpl implements MethodInterceptor
{
@Override
public Object invoke(MethodInvocation invocation) throws Throwable
{
System.out.println("环绕拦截开始....");
Object obj = invocation.proceed();
System.out.println("环绕拦截结束");
return obj;
}
}
异常通知实现类
package siye;
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class ThrowsAdviceImpl implements ThrowsAdvice
{
public void afterThrowing(Method method, Object[] args,
Object target, Exception ex) throws Throwable
{
System.out.println("方法抛出异常,异常类型是," + ex.toString());
}
}
引入实现类
package siye;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;
public class IntroductionInterceptorImpl extends
DelegatingIntroductionInterceptor implements NewFeature
{
private static final long serialVersionUID = 1401264437483157075L;
@Override
public Object invoke(MethodInvocation mi) throws Throwable
{
return super.invoke(mi);
}
@Override
public void eat()
{
System.out.println("introduction__eat....");
}
}
xml配置文件,文件名`config.xml`
<bean id="adviceBefore" class="siye.MethodBeforeAdviceImpl" />
<bean id="adviceAfterReturning"
class="siye.AfterReturningAdviceImpl" />
<bean id="adviceAround" class="siye.MethodInterceptorImpl" />
<bean id="adviceThrows" class="siye.ThrowsAdviceImpl" />
<bean id="introductionCustom"
class="siye.IntroductionInterceptorImpl" />
<bean id="target" class="siye.TargetObj" />
<bean id="proxyFactory"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interfaces" value="siye.NewFeature" />
<property name="interceptorNames">
<list>
<idref bean="adviceBefore" />
<idref bean="adviceAfterReturning" />
<idref bean="adviceAround" />
<idref bean="adviceThrows" />
<idref bean="introductionCustom" />
</list>
</property>
<property name="target" ref="target" />
<property name="proxyTargetClass" value="true" />
</bean>
测试类
package siye;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class UnitTest
{
public static void main(String[] args)
{
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext(
"classpath:/siye/config.xml");
TargetObj targetObj =
(TargetObj) context.getBean("proxyFactory");
targetObj.work();
System.out.println();
try
{
targetObj.throwOwn();
} catch (Exception e)
{
}
System.out.println();
NewFeature obj = (NewFeature) targetObj;
obj.eat();
context.close();
}
}