001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.context.annotation;
018:
019: import java.util.LinkedHashSet;
020: import java.util.Set;
021:
022: import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
023: import org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor;
024: import org.springframework.beans.factory.config.BeanDefinition;
025: import org.springframework.beans.factory.config.BeanDefinitionHolder;
026: import org.springframework.beans.factory.support.BeanDefinitionRegistry;
027: import org.springframework.beans.factory.support.RootBeanDefinition;
028: import org.springframework.util.ClassUtils;
029:
030: /**
031: * Utility class that allows for convenient registration of common
032: * {@link org.springframework.beans.factory.config.BeanPostProcessor}
033: * definitions for annotation-based configuration.
034: *
035: * @author Mark Fisher
036: * @author Juergen Hoeller
037: * @since 2.5
038: * @see org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
039: * @see org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor
040: * @see CommonAnnotationBeanPostProcessor
041: * @see org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor
042: */
043: public class AnnotationConfigUtils {
044:
045: /**
046: * The bean name of the internally managed Required annotation processor.
047: */
048: public static final String REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalRequiredAnnotationProcessor";
049:
050: /**
051: * The bean name of the internally managed Autowired annotation processor.
052: */
053: public static final String AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalAutowiredAnnotationProcessor";
054:
055: /**
056: * The bean name of the internally managed JSR-250 annotation processor.
057: */
058: public static final String COMMON_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalCommonAnnotationProcessor";
059:
060: /**
061: * The bean name of the internally managed JPA annotation processor.
062: */
063: public static final String PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME = "org.springframework.context.annotation.internalPersistenceAnnotationProcessor";
064:
065: private static final String PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME = "org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor";
066:
067: private static final boolean jsr250Present = ClassUtils.isPresent(
068: "javax.annotation.Resource", AnnotationConfigUtils.class
069: .getClassLoader());
070:
071: private static final boolean jpaPresent = ClassUtils.isPresent(
072: "javax.persistence.EntityManagerFactory",
073: AnnotationConfigUtils.class.getClassLoader())
074: && ClassUtils.isPresent(
075: PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
076: AnnotationConfigUtils.class.getClassLoader());
077:
078: /**
079: * Register all relevant annotation post processors in the given registry.
080: * @param registry the registry to operate on
081: */
082: public static void registerAnnotationConfigProcessors(
083: BeanDefinitionRegistry registry) {
084: registerAnnotationConfigProcessors(registry, null);
085: }
086:
087: /**
088: * Register all relevant annotation post processors in the given registry.
089: * @param registry the registry to operate on
090: * @param source the configuration source element (already extracted)
091: * that this registration was triggered from. May be <code>null</code>.
092: * @return a Set of BeanDefinitionHolders, containing all bean definitions
093: * that have actually been registered by this call
094: */
095: public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
096: BeanDefinitionRegistry registry, Object source) {
097:
098: Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<BeanDefinitionHolder>(
099: 4);
100:
101: if (!registry
102: .containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
103: RootBeanDefinition def = new RootBeanDefinition(
104: RequiredAnnotationBeanPostProcessor.class);
105: def.setSource(source);
106: beanDefinitions.add(registerBeanPostProcessor(registry,
107: def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
108: }
109:
110: if (!registry
111: .containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
112: RootBeanDefinition def = new RootBeanDefinition(
113: AutowiredAnnotationBeanPostProcessor.class);
114: def.setSource(source);
115: beanDefinitions.add(registerBeanPostProcessor(registry,
116: def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
117: }
118:
119: // Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
120: if (jsr250Present
121: && !registry
122: .containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
123: RootBeanDefinition def = new RootBeanDefinition(
124: CommonAnnotationBeanPostProcessor.class);
125: def.setSource(source);
126: beanDefinitions.add(registerBeanPostProcessor(registry,
127: def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
128: }
129:
130: // Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
131: if (jpaPresent
132: && !registry
133: .containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
134: RootBeanDefinition def = new RootBeanDefinition();
135: def
136: .setBeanClassName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME);
137: def.setSource(source);
138: beanDefinitions.add(registerBeanPostProcessor(registry,
139: def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
140: }
141:
142: return beanDefinitions;
143: }
144:
145: private static BeanDefinitionHolder registerBeanPostProcessor(
146: BeanDefinitionRegistry registry,
147: RootBeanDefinition definition, String beanName) {
148:
149: definition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
150: registry.registerBeanDefinition(beanName, definition);
151: return new BeanDefinitionHolder(definition, beanName);
152: }
153:
154: }
|