1、ProxyFactoryBean 把proxy的创建交给AopProxy去做。
ProxyFactoryBean.javajava 代码
java 代码
- public class ProxyFactoryBean extends AdvisedSupport
-
- implements FactoryBean, BeanFactoryAware, AdvisedSupportListener {
-
- public Object getObject() throws BeansException {
-
-
-
- return this.singleton ? getSingletonInstance() : newPrototypeInstance();
-
- }
-
-
-
- public void setAopProxyFactory(AopProxyFactory apf) {
-
- this.aopProxyFactory = apf;
-
- }
-
-
-
- public AopProxyFactory getAopProxyFactory() {
-
- return this.aopProxyFactory;
-
- }
-
- }
-
2.、那么AopProxy又如何获取?答案是从AopProxyFactory获得。这个AopProxyFactory已在ProxyFactoryBean掌控范围内,如上述代码所示。AopProxyFactory interface如下所示:
AopProxyFactory.java
java 代码
- public interface AopProxyFactory {
-
- AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException;
-
-
-
- }
3、AopProxy是如何introduce Advised interface的?那要先从AopProxyFactory的implementation说起。
AopProxyFactory的实现类目前只有DefaultAopProxyFactory。这个类的核心方法如下:
java 代码
- public AopProxy createAopProxy(AdvisedSupport advisedSupport) throws AopConfigException {
-
- if (advisedSupport.isOptimize() || advisedSupport.isProxyTargetClass() ||
-
- advisedSupport.getProxiedInterfaces().length == 0) {
-
- if (!cglibAvailable) {
-
- throw new AopConfigException(
-
- "Cannot proxy target class because CGLIB2 is not available. " +
-
- "Add CGLIB to the class path or specify proxy interfaces.");
-
- }
-
- return CglibProxyFactory.createCglibProxy(advisedSupport);
-
- } else {
-
- eturn new JdkDynamicAopProxy(advisedSupport);
-
- }
-
- }
-
这个方法主要是依赖CglibProxyFactory 类和JdkDynamicAopProxy类。这两个类是如何代理所有的接口的呢?
这两个类都是重要接口AopProxy的实现类,这个接口的核心方法是
java 代码
- public Object getProxy(ClassLoader classLoader)。
-
- 其中JdkDynamicAopProxy实现该方法用到的获取需要代理的接口的代码如下:
-
- public Object getProxy(ClassLoader classLoader) {
-
- if (logger.isDebugEnabled()) {
-
- Class targetClass = this.advised.getTargetSource().getTargetClass();
-
- logger.debug("Creating JDK dynamic proxy" +
-
- (targetClass != null ? " for [" + targetClass.getName() + "]" : ""));
-
- }
-
- Class[] proxiedInterfaces = AopProxyUtils.completeProxiedInterfaces(this.advised);
-
- return Proxy.newProxyInstance(classLoader, proxiedInterfaces, this);
- }
-
-
而
java 代码
- public Object getProxy(ClassLoader classLoader) {
-
- Enhancer enhancer = new Enhancer();
-
- enhancer.setInterfaces(AopProxyUtils.completeProxiedInterfaces(this.advised));
- }
-
从上面可以看出,这两个AopProxy实现方案在获取接口时都是通过AopProxyUtils.completeProxiedInterfaces(this.advised)获取的。该方法如下:
Advised interface的实现。前提是是否允许代理该interafce,这由isOpaque方法确定,该方法Return whether proxies created by this configuration should be prevented from being cast to Advised.
java 代码
-
-
-
-
-
-
-
-
-
- public static Class[] completeProxiedInterfaces(AdvisedSupport advised) {
-
-
-
- Class[] specifiedInterfaces = advised.getProxiedInterfaces();
-
- Class[] proxiedInterfaces = specifiedInterfaces;
-
- if (!advised.isOpaque() && !advised.isInterfaceProxied(Advised.class)) {
-
-
-
- proxiedInterfaces = new Class[specifiedInterfaces.length + 1];
-
- proxiedInterfaces[0] = Advised.class;
-
- System.arraycopy(specifiedInterfaces, 0, proxiedInterfaces, 1,specifiedInterfaces.length);
-
- }
-
- return proxiedInterfaces;
-
- }
-
从这可以看出,是在这强制加了对
4. 顺便提一下,Advised interface的具体实现又是怎样的,在哪呢?
4.1 在JdkDynamicAopProxy里,
- public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
- if (Advised.class == method.getDeclaringClass()) {
-
- return AopUtils.invokeJoinpointUsingReflection(this.advised, method, args);
- }
- }
注意第一个参数是
而AdisedSupport extends ProxyConfig implements Advised,也就是说Advised的默认实现就是AdisedSupport。
4.2 在Cglib2AopProxy里
- public Object getProxy(ClassLoader classLoader) {
-
- Enhancer enhancer = new Enhancer();
-
- enhancer.setCallbackFilter(new ProxyCallbackFilter(this.advised));
-
- Callback[] callbacks = getCallbacks(rootClass);
-
- }
-
- private Callback[] getCallbacks(Class rootClass) throws Exception {
-
- Callback[] mainCallbacks = new Callback[]{
-
- aopInterceptor,
-
- targetInterceptor,
-
- new SerializableNoOp(),
-
- targetDispatcher, this.advisedDispatcher,
-
- new EqualsInterceptor(this.advised)
-
- };
- }
-
-
-
- private class ProxyCallbackFilter implements CallbackFilter {
-
- public int accept(Method method) {
-
- if (method.getDeclaringClass() == Advised.class) {
-
- if (logger.isDebugEnabled()) {
-
- logger.debug("Method " + method + " is declared on Advised - using DISPATCH_ADVISED");
-
- }
-
- return DISPATCH_ADVISED;
-
- }
-
- }
-
- }
-
-
-
- private final transient AdvisedDispatcher advisedDispatcher = new AdvisedDispatcher();
-
-
-
-
- private class AdvisedDispatcher implements Dispatcher, Serializable {
- public Object loadObject() throws Exception {
- return advised;
- }
-
- }
-
-
注意
Dispatcher extends Callback interface 。
java 代码
this.advised,其实就是ProxyFactoryBean本身,注意在我发表的其天的Spring文章里也曾提到,ProxyFactoryBean extends AdvisedSupport,
java 代码
CglibProxyFactory的getProxy(ClassLoader classLoader)方法如下:
分享到:
相关推荐
class="org.springframework.aop.framework.ProxyFactoryBean"> <!--这里的必须要指定接口 --> <value>org.nitpro.aop.BizInterface <!--业务处理节点名称和拦截器名称必须都要定义, 必须将拦截器的对象...
7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. 使用...
7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. ...
并定义了一个 org.springframework.aop.framework.ProxyFactoryBean对象(messageSender),FactoryBean或ApplicationContext将使用ProxyFactoryBean来建立代理对象,在这里就是messageSenderImpl建立代理对象。...
7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”通知器 7.6. 简化代理定义 7.7. 使用...
7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. ...
<bean id="cacheProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <value>cacheBeforeAdvice <value>cacheAfterAdvice <value>cacheInterceptor</value> ...
7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”通知器 7.6. 简化代理定义 7.7. 使用...
ProxyFactoryBean ProxyMethodInvocation QuartzJobBean QuickTargetSourceCreator RadioButtonTag RdbmsOperation ReaderContext ReaderEventListener RecordCreator RecordExtractor ...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................