34 Spring自动代理创建者实例
2017-08-24 本文已影响24人
笑Skr人啊
在上几篇介绍 Spring AOP的文章中, 必须手动创建一个代理bean(ProxyFactryBean),对每个Bean需要AOP支持。
这不是一种有效的方式,例如,如果想在客户模块,所有的DAO类实现SQL日志支持(提醒)的AOP功能,那么必须手动创建很多代理工厂bean,因此在 bean配置文件可能会泛滥代理类。
幸运的是,Spring有两个自动代理创建者来自动创建代理bean。
1. BeanNameAutoProxyCreator示例
在此之前,必须手动创建一个代理bean(ProxyFactryBean)。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.Point.customer.services.CustomerService">
<property name="name" value="Point Mook Kim" />
<property name="url" value="http://www.Point.com" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.Point.aop.HijackAroundMethod" />
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="customerService" />
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
</beans>
使用代理名称“customerServiceProxy”来获得 bean。
CustomerService cust = (CustomerService)appContext.getBean("customerServiceProxy");
在自动代理机制,只需要创建一个的 BeanNameAutoProxyCreator,并包含所有你的bean(通过bean的名字,或正则表达式名)和“advisor” 作为一个单位。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.aspectJ.CustomerService">
<property name="name" value="Point Mook Kim" />
<property name="url" value="http://www.Point.com" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.gp6.aop.aspectJ.HijackAroundMethod" />
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<value>*Service</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>customerAdvisor</value>
</list>
</property>
</bean>
</beans>
package com.gp6.aop.aspectJ;
public class CustomerService {
private String name;
private String url;
public void setName(String name) {
this.name = name;
}
public void setUrl(String url) {
this.url = url;
}
public void printName() {
System.out.println("Customer name : " + this.name);
}
public void printURL() {
System.out.println("Customer website : " + this.url);
}
public void printThrowException() {
throw new IllegalArgumentException();
}
}
package com.gp6.aop.aspectJ;
import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class HijackAroundMethod implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method name : "
+ methodInvocation.getMethod().getName());
System.out.println("Method arguments : "
+ Arrays.toString(methodInvocation.getArguments()));
// same with MethodBeforeAdvice
System.out.println("HijackAroundMethod : Before method hijacked!");
try {
// proceed to original method call
Object result = methodInvocation.proceed();
// same with AfterReturningAdvice
System.out.println("HijackAroundMethod : Before after hijacked!");
return result;
} catch (IllegalArgumentException e) {
// same with ThrowsAdvice
System.out.println("HijackAroundMethod : Throw exception hijacked!");
throw e;
}
}
}
- 现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。
package com.gp6.aop.aspectJ;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext("com/gp6/aop/aspectJ/applicationContext.xml");
//现在,可以通过“CustomerService”的原始名称获取bean, 如果知道这个bean已经代理。
CustomerService cust = (CustomerService)appContext.getBean("customerService");
System.out.println("*************************");
cust.printName();
System.out.println("*************************");
cust.printURL();
System.out.println("*************************");
try {
cust.printThrowException();
} catch (Exception e) {
}
}
}
2. DefaultAdvisorAutoProxyCreator示例
这个 DefaultAdvisorAutoProxyCreator 是非常强大的,如果有 bean 相关连,Spring会自动创建一个代理。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="customerService" class="com.gp6.aop.aspectJ.CustomerService">
<property name="name" value="Point Mook Kim" />
<property name="url" value="http://www.Point.com" />
</bean>
<bean id="hijackAroundMethodBeanAdvice" class="com.gp6.aop.aspectJ.HijackAroundMethod" />
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="mappedName" value="printName" />
<property name="advice" ref="hijackAroundMethodBeanAdvice" />
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
</beans>