From b3e3c5312f73194121e566e75ade58108925a15c Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 26 Jun 2014 11:43:03 +0200 Subject: [PATCH] Introduced AnnotationConfigRegistry as common interface for AnnotationConfig(Web)ApplicationContext Issue: SPR-11814 --- .../AnnotationConfigApplicationContext.java | 16 +++---- .../annotation/AnnotationConfigRegistry.java | 43 +++++++++++++++++++ ...AnnotationConfigWebApplicationContext.java | 16 +++---- 3 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java index 300f1d6ff2..98ab303e29 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -46,7 +46,7 @@ import org.springframework.util.Assert; * @see ClassPathBeanDefinitionScanner * @see org.springframework.context.support.GenericXmlApplicationContext */ -public class AnnotationConfigApplicationContext extends GenericApplicationContext { +public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry { private final AnnotatedBeanDefinitionReader reader; @@ -135,12 +135,11 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex this.scanner.setScopeMetadataResolver(scopeMetadataResolver); } + /** * Register one or more annotated classes to be processed. - * Note that {@link #refresh()} must be called in order for the context - * to fully process the new class. - *

Calls to {@code register} are idempotent; adding the same - * annotated class more than once has no additional effect. + *

Note that {@link #refresh()} must be called in order for the context + * to fully process the new classes. * @param annotatedClasses one or more annotated classes, * e.g. {@link Configuration @Configuration} classes * @see #scan(String...) @@ -153,8 +152,8 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex /** * Perform a scan within the specified base packages. - * Note that {@link #refresh()} must be called in order for the context to - * fully process the new class. + *

Note that {@link #refresh()} must be called in order for the context + * to fully process the new classes. * @param basePackages the packages to check for annotated classes * @see #register(Class...) * @see #refresh() @@ -164,6 +163,7 @@ public class AnnotationConfigApplicationContext extends GenericApplicationContex this.scanner.scan(basePackages); } + @Override protected void prepareRefresh() { this.scanner.clearCache(); diff --git a/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java new file mode 100644 index 0000000000..d1fe2f1def --- /dev/null +++ b/spring-context/src/main/java/org/springframework/context/annotation/AnnotationConfigRegistry.java @@ -0,0 +1,43 @@ +/* + * Copyright 2002-2014 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.context.annotation; + +/** + * Common interface for annotation config application contexts, + * defining {@link #register} and {@link #scan} methods. + * + * @author Juergen Hoeller + * @since 4.1 + */ +public interface AnnotationConfigRegistry { + + /** + * Register one or more annotated classes to be processed. + *

Calls to {@code register} are idempotent; adding the same + * annotated class more than once has no additional effect. + * @param annotatedClasses one or more annotated classes, + * e.g. {@link Configuration @Configuration} classes + */ + void register(Class... annotatedClasses); + + /** + * Perform a scan within the specified base packages. + * @param basePackages the packages to check for annotated classes + */ + void scan(String... basePackages); + +} diff --git a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java index c4c1ec3d34..3a37fea1b7 100644 --- a/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java +++ b/spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2014 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. @@ -23,6 +23,7 @@ import java.util.Set; import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.context.annotation.AnnotatedBeanDefinitionReader; +import org.springframework.context.annotation.AnnotationConfigRegistry; import org.springframework.context.annotation.AnnotationConfigUtils; import org.springframework.context.annotation.ClassPathBeanDefinitionScanner; import org.springframework.context.annotation.ScopeMetadataResolver; @@ -78,7 +79,8 @@ import org.springframework.web.context.ContextLoader; * @since 3.0 * @see org.springframework.context.annotation.AnnotationConfigApplicationContext */ -public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWebApplicationContext { +public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWebApplicationContext + implements AnnotationConfigRegistry { private BeanNameGenerator beanNameGenerator; @@ -130,10 +132,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe /** * Register one or more annotated classes to be processed. - * Note that {@link #refresh()} must be called in order for the context - * to fully process the new class. - *

Calls to {@code register} are idempotent; adding the same - * annotated class more than once has no additional effect. + *

Note that {@link #refresh()} must be called in order for the context + * to fully process the new classes. * @param annotatedClasses one or more annotated classes, * e.g. {@link org.springframework.context.annotation.Configuration @Configuration} classes * @see #scan(String...) @@ -148,8 +148,8 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe /** * Perform a scan within the specified base packages. - * Note that {@link #refresh()} must be called in order for the context to - * fully process the new class. + *

Note that {@link #refresh()} must be called in order for the context + * to fully process the new classes. * @param basePackages the packages to check for annotated classes * @see #loadBeanDefinitions(DefaultListableBeanFactory) * @see #register(Class...)