AOP实现中,可以看到三个主要的步骤,一个是代理对象的生成,然后是拦截器的作用,然后是Aspect编织的实现。

 1.ProxyFactoryBean生成AopProxy

ProxyFactoryBean生成AOP proxy

 1     /**
 2      * Return a proxy. Invoked when clients obtain beans from this factory bean.
 3      * Create an instance of the AOP proxy to be returned by this factory.
 4      * The instance will be cached for a singleton, and create on each call to
 5      * <code>getObject()</code> for a proxy.
 6      * @return a fresh AOP proxy reflecting the current state of this factory
 7      */
 8     public Object getObject() throws BeansException {
 9         initializeAdvisorChain();
10         if (isSingleton()) {
11             return getSingletonInstance();
12         }
13         else {
14             if (this.targetName == null) {
15                 logger.warn("Using non-singleton proxies with singleton targets is often undesirable. " +
16                         "Enable prototype proxies by setting the 'targetName' property.");
17             }
18             return newPrototypeInstance();
19         }
20     }

初始化Advisor chain

 1     /**
 2      * Create the advisor (interceptor) chain. Aadvisors that are sourced
 3      * from a BeanFactory will be refreshed each time a new prototype instance
 4      * is added. Interceptors added programmatically through the factory API
 5      * are unaffected by such changes.
 6      */
 7     private synchronized void initializeAdvisorChain() throws AopConfigException, BeansException {
 8         if (this.advisorChainInitialized) {
 9             return;
10         }
11 
12         if (!ObjectUtils.isEmpty(this.interceptorNames)) {
13             if (this.beanFactory == null) {
14                 throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
15                         "- cannot resolve interceptor names " + Arrays.asList(this.interceptorNames));
16             }
17 
18             // Globals can't be last unless we specified a targetSource using the property...
19             if (this.interceptorNames[this.interceptorNames.length - 1].endsWith(GLOBAL_SUFFIX) &&
20                     this.targetName == null && this.targetSource == EMPTY_TARGET_SOURCE) {
21                 throw new AopConfigException("Target required after globals");
22             }
23 
24             // Materialize interceptor chain from bean names.
25             for (int i = 0; i < this.interceptorNames.length; i++) {
26                 String name = this.interceptorNames[i];
27                 if (logger.isTraceEnabled()) {
28                     logger.trace("Configuring advisor or advice '" + name + "'");
29                 }
30 
31                 if (name.endsWith(GLOBAL_SUFFIX)) {
32                     if (!(this.beanFactory instanceof ListableBeanFactory)) {
33                         throw new AopConfigException(
34                                 "Can only use global advisors or interceptors with a ListableBeanFactory");
35                     }
36                     addGlobalAdvisor((ListableBeanFactory) this.beanFactory,
37                             name.substring(0, name.length() - GLOBAL_SUFFIX.length()));
38                 }
39 
40                 else {
41                     // If we get here, we need to add a named interceptor.
42                     // We must check if it's a singleton or prototype.
43                     Object advice = null;
44                     if (this.singleton || this.beanFactory.isSingleton(this.interceptorNames[i])) {
45                         // Add the real Advisor/Advice to the chain.
46                         advice = this.beanFactory.getBean(this.interceptorNames[i]);
47                     }
48                     else {
49                         // It's a prototype Advice or Advisor: replace with a prototype.
50                         // Avoid unnecessary creation of prototype bean just for advisor chain initialization.
51                         advice = new PrototypePlaceholderAdvisor(this.interceptorNames[i]);
52                     }
53                     addAdvisorOnChainCreation(advice, this.interceptorNames[i]);
54                 }
55             }
56         }
57 
58         this.advisorChainInitialized = true;
59     }

增加advisor chain(AdvisedSupport.java)

 1     private void addAdvisorInternal(int pos, Advisor advisor) throws AopConfigException {
 2         Assert.notNull(advisor, "Advisor must not be null");
 3         if (isFrozen()) {
 4             throw new AopConfigException("Cannot add advisor: Configuration is frozen.");
 5         }
 6         if (pos > this.advisors.size()) {
 7             throw new IllegalArgumentException(
 8                     "Illegal position " + pos + " in advisor list with size " + this.advisors.size());
 9         }
10         this.advisors.add(pos, advisor);
11         updateAdvisorArray();
12         adviceChanged();
13     }

 

posted on 2013-03-18 11:17  一天不进步,就是退步  阅读(3002)  评论(0编辑  收藏  举报