From d35620511e8304421929e89ed4d13d82d5f40219 Mon Sep 17 00:00:00 2001 From: Chris Beams Date: Mon, 28 Nov 2011 06:57:17 +0000 Subject: [PATCH] Introduce @EnableSpringConfigured Equivalent to . Also update @EnableLoadTimeWeaving Javadoc and spring-configured XSD documentation to reflect. Issue: SPR-7888 --- org.springframework.aspects/ivy.xml | 1 + .../aspectj/EnableSpringConfigured.java | 47 ++++++++++++++++++ .../SpringConfiguredConfiguration.java | 49 +++++++++++++++++++ .../AnnotationBeanConfigurerTests.java | 43 ++++++++++++++++ .../annotation/EnableLoadTimeWeaving.java | 4 +- .../context/config/spring-context-3.1.xsd | 4 ++ 6 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/EnableSpringConfigured.java create mode 100644 org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/SpringConfiguredConfiguration.java create mode 100644 org.springframework.aspects/src/test/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerTests.java diff --git a/org.springframework.aspects/ivy.xml b/org.springframework.aspects/ivy.xml index 34abe6a86d6..be0cfa1b60c 100644 --- a/org.springframework.aspects/ivy.xml +++ b/org.springframework.aspects/ivy.xml @@ -27,6 +27,7 @@ + diff --git a/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/EnableSpringConfigured.java b/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/EnableSpringConfigured.java new file mode 100644 index 00000000000..68b7aab4a77 --- /dev/null +++ b/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/EnableSpringConfigured.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.aspectj; + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.context.annotation.Import; + +/** + * Signals the current application context to apply dependency injection to non-managed + * classes that are instantiated outside of the Spring bean factory (typically classes + * annotated with the @{@link org.springframework.beans.factory.annotation.Configurable + * Configurable} annotation). + * + *

Similar to functionality found in Spring's {@code } XML + * element. Often used in conjunction with {@link + * org.springframework.context.annotation.EnableLoadTimeWeaving @EnableLoadTimeWeaving}. + * + * @author Chris Beams + * @since 3.1 + * @see org.springframework.context.annotation.EnableLoadTimeWeaving + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Import(SpringConfiguredConfiguration.class) +public @interface EnableSpringConfigured { + +} diff --git a/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/SpringConfiguredConfiguration.java b/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/SpringConfiguredConfiguration.java new file mode 100644 index 00000000000..2c34e11ab2a --- /dev/null +++ b/org.springframework.aspects/src/main/java/org/springframework/beans/factory/aspectj/SpringConfiguredConfiguration.java @@ -0,0 +1,49 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.aspectj; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Role; + +/** + * {@code @Configuration} class that registers an {@link AnnotationBeanConfigurerAspect} + * capable of performing dependency injection services for non-Spring managed objects + * annotated with @{@link org.springframework.beans.factory.annotation.Configurable + * Configurable}. + * + *

This configuration class is automatically imported when using the @{@link + * EnableSpringConfigured} annotation. See {@code @EnableSpringConfigured} Javadoc for + * complete usage details. + * + * @author Chris Beams + * @since 3.1 + * @see EnableSpringConfigured + */ +@Configuration +public class SpringConfiguredConfiguration { + + public static final String BEAN_CONFIGURER_ASPECT_BEAN_NAME = + "org.springframework.context.config.internalBeanConfigurerAspect"; + + @Bean(name=BEAN_CONFIGURER_ASPECT_BEAN_NAME) + @Role(BeanDefinition.ROLE_INFRASTRUCTURE) + public AnnotationBeanConfigurerAspect beanConfigurerAspect() { + return AnnotationBeanConfigurerAspect.aspectOf(); + } +} diff --git a/org.springframework.aspects/src/test/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerTests.java b/org.springframework.aspects/src/test/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerTests.java new file mode 100644 index 00000000000..26e4ac0a9b1 --- /dev/null +++ b/org.springframework.aspects/src/test/java/org/springframework/beans/factory/aspectj/AnnotationBeanConfigurerTests.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.beans.factory.aspectj; + +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; + +/** + * Tests that @EnableSpringConfigured properly registers an + * {@link AnnotationBeanConfigurerAspect}, just as does {@code } + * + * @author Chris Beams + * @since 3.1 + */ +public class AnnotationBeanConfigurerTests extends AbstractBeanConfigurerTests { + + @Override + protected ConfigurableApplicationContext createContext() { + return new AnnotationConfigApplicationContext(Config.class); + } + + @Configuration + @ImportResource("org/springframework/beans/factory/aspectj/beanConfigurerTests-beans.xml") + @EnableSpringConfigured + static class Config { + } +} diff --git a/org.springframework.context/src/main/java/org/springframework/context/annotation/EnableLoadTimeWeaving.java b/org.springframework.context/src/main/java/org/springframework/context/annotation/EnableLoadTimeWeaving.java index bb20e205868..bed3cec76ac 100644 --- a/org.springframework.context/src/main/java/org/springframework/context/annotation/EnableLoadTimeWeaving.java +++ b/org.springframework.context/src/main/java/org/springframework/context/annotation/EnableLoadTimeWeaving.java @@ -117,8 +117,8 @@ import org.springframework.instrument.classloading.LoadTimeWeaver; *

The two examples are equivalent with one significant exception: in the XML case, * the functionality of {@code } is implicitly enabled when * {@code aspectj-weaving} is "on". This does not occur when using - * {@code @EnableLoadTimeWeaving(aspectjWeaving=ENABLED)}, although this may change in - * future revisions. + * {@code @EnableLoadTimeWeaving(aspectjWeaving=ENABLED)}. Instead you must explicitly add + * {@code @EnableSpringConfigured} (included in the {@code spring-aspects} module) * * @author Chris Beams * @since 3.1 diff --git a/org.springframework.context/src/main/resources/org/springframework/context/config/spring-context-3.1.xsd b/org.springframework.context/src/main/resources/org/springframework/context/config/spring-context-3.1.xsd index 465ef354de0..c672df54f7c 100644 --- a/org.springframework.context/src/main/resources/org/springframework/context/config/spring-context-3.1.xsd +++ b/org.springframework.context/src/main/resources/org/springframework/context/config/spring-context-3.1.xsd @@ -360,6 +360,10 @@ Signals the current application context to apply dependency injection to non-managed classes that are instantiated outside of the Spring bean factory (typically classes annotated with the @Configurable annotation). + + See Javadoc for org.springframework.context.annotation.EnableSpringConfigured in the + spring-aspects module for information on code-based alternatives to bootstrapping + this functionality. ]]>