|
| 1 | +/* |
| 2 | + * Copyright 2002-2008 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.beans.factory.aspectj; |
| 18 | + |
| 19 | +import org.aspectj.lang.annotation.SuppressAjWarnings; |
| 20 | + |
| 21 | +/** |
| 22 | + * Abstract base aspect that can perform Dependency |
| 23 | + * Injection on objects, however they may be created. |
| 24 | + * |
| 25 | + * @author Ramnivas Laddad |
| 26 | + * @since 2.5.2 |
| 27 | + */ |
| 28 | +public abstract aspect AbstractDependencyInjectionAspect { |
| 29 | + /** |
| 30 | + * Select construction join points for objects to inject dependencies |
| 31 | + */ |
| 32 | + public abstract pointcut beanConstruction(Object bean); |
| 33 | + |
| 34 | + /** |
| 35 | + * Select deserialization join points for objects to inject dependencies |
| 36 | + */ |
| 37 | + public abstract pointcut beanDeserialization(Object bean); |
| 38 | + |
| 39 | + /** |
| 40 | + * Select join points in a configurable bean |
| 41 | + */ |
| 42 | + public abstract pointcut inConfigurableBean(); |
| 43 | + |
| 44 | + /** |
| 45 | + * Select join points in beans to be configured prior to construction? |
| 46 | + * By default, use post-construction injection matching the default in the Configurable annotation. |
| 47 | + */ |
| 48 | + public pointcut preConstructionConfiguration() : if(false); |
| 49 | + |
| 50 | + /** |
| 51 | + * Select the most-specific initialization join point |
| 52 | + * (most concrete class) for the initialization of an instance. |
| 53 | + */ |
| 54 | + public pointcut mostSpecificSubTypeConstruction() : |
| 55 | + if(thisJoinPoint.getSignature().getDeclaringType() == thisJoinPoint.getThis().getClass()); |
| 56 | + |
| 57 | + /** |
| 58 | + * Select least specific super type that is marked for DI (so that injection occurs only once with pre-construction inejection |
| 59 | + */ |
| 60 | + public abstract pointcut leastSpecificSuperTypeConstruction(); |
| 61 | + |
| 62 | + /** |
| 63 | + * Configure the bean |
| 64 | + */ |
| 65 | + public abstract void configureBean(Object bean); |
| 66 | + |
| 67 | + |
| 68 | + private pointcut preConstructionCondition() : |
| 69 | + leastSpecificSuperTypeConstruction() && preConstructionConfiguration(); |
| 70 | + |
| 71 | + private pointcut postConstructionCondition() : |
| 72 | + mostSpecificSubTypeConstruction() && !preConstructionConfiguration(); |
| 73 | + |
| 74 | + /** |
| 75 | + * Pre-construction configuration. |
| 76 | + */ |
| 77 | + @SuppressAjWarnings("adviceDidNotMatch") |
| 78 | + before(Object bean) : |
| 79 | + beanConstruction(bean) && preConstructionCondition() && inConfigurableBean() { |
| 80 | + configureBean(bean); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Post-construction configuration. |
| 85 | + */ |
| 86 | + @SuppressAjWarnings("adviceDidNotMatch") |
| 87 | + after(Object bean) returning : |
| 88 | + beanConstruction(bean) && postConstructionCondition() && inConfigurableBean() { |
| 89 | + configureBean(bean); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Post-deserialization configuration. |
| 94 | + */ |
| 95 | + @SuppressAjWarnings("adviceDidNotMatch") |
| 96 | + after(Object bean) returning : |
| 97 | + beanDeserialization(bean) && inConfigurableBean() { |
| 98 | + configureBean(bean); |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments