Polish and Fixup
Polish and fixup: - Ordered auto-configuration - @ConditionalOnBean default on @Bean methods - Improved separation of auto-configure classes - Consistent naming - Javadoc, code formatting and testspull/2/merge
parent
2f84df66b6
commit
dd69d0f660
@ -1,89 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.autoconfigure.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.ListableBeanFactory;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnClass;
|
||||
import org.springframework.bootstrap.context.annotation.EnableAutoConfiguration;
|
||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for
|
||||
* {@link EmbeddedServletContainerCustomizer}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnClass({ Servlet.class, EmbeddedServletContainerCustomizer.class })
|
||||
public class EmbeddedContainerCustomizerConfiguration {
|
||||
|
||||
@Bean
|
||||
public BeanPostProcessor embeddedContainerCustomizerBeanPostProcessor(
|
||||
ListableBeanFactory beanFactory) {
|
||||
// Look these up, not autowired because we don't want the ones from the parent
|
||||
// context
|
||||
Collection<EmbeddedServletContainerCustomizer> customizers = beanFactory
|
||||
.getBeansOfType(EmbeddedServletContainerCustomizer.class).values();
|
||||
return new EmbeddedContainerCustomizerBeanPostProcessor(customizers);
|
||||
}
|
||||
|
||||
private static final class EmbeddedContainerCustomizerBeanPostProcessor implements
|
||||
BeanPostProcessor {
|
||||
|
||||
private List<EmbeddedServletContainerCustomizer> customizers;
|
||||
|
||||
public EmbeddedContainerCustomizerBeanPostProcessor(
|
||||
Collection<EmbeddedServletContainerCustomizer> customizers) {
|
||||
final List<EmbeddedServletContainerCustomizer> list = new ArrayList<EmbeddedServletContainerCustomizer>(
|
||||
customizers);
|
||||
Collections.sort(list, new AnnotationAwareOrderComparator());
|
||||
this.customizers = list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainerFactory) {
|
||||
ConfigurableEmbeddedServletContainerFactory factory = (ConfigurableEmbeddedServletContainerFactory) bean;
|
||||
for (EmbeddedServletContainerCustomizer customizer : this.customizers) {
|
||||
customizer.customize(factory);
|
||||
}
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.config;
|
||||
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
/**
|
||||
* Factory to create a {@link JsonParser}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @see JacksonJsonParser
|
||||
* @see YamlJsonParser
|
||||
* @see SimpleJsonParser
|
||||
*/
|
||||
public class JsonParserFactory {
|
||||
|
||||
/**
|
||||
* Static factory for the "best" JSON parser available on the classpath. Tries Jackson
|
||||
* (2), then Snake YAML, and then falls back to the {@link SimpleJsonParser}.
|
||||
*
|
||||
* @return a {@link JsonParser}
|
||||
*/
|
||||
public static JsonParser getJsonParser() {
|
||||
if (ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", null)) {
|
||||
return new JacksonJsonParser();
|
||||
}
|
||||
if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) {
|
||||
return new YamlJsonParser();
|
||||
}
|
||||
return new SimpleJsonParser();
|
||||
}
|
||||
}
|
@ -1,64 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.context.annotation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
|
||||
/**
|
||||
* Convenience class for storing base packages during component scan, for reference later
|
||||
* (e.g. by JPA entity scanner).
|
||||
*
|
||||
* @author Phil Webb
|
||||
* @author Dave Syer
|
||||
*
|
||||
*/
|
||||
public abstract class AutoConfigurationUtils {
|
||||
|
||||
private static String BASE_PACKAGES_BEAN = AutoConfigurationUtils.class.getName()
|
||||
+ ".basePackages";
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getBasePackages(BeanFactory beanFactory) {
|
||||
try {
|
||||
return beanFactory.getBean(BASE_PACKAGES_BEAN, List.class);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public static void storeBasePackages(ConfigurableListableBeanFactory beanFactory,
|
||||
List<String> basePackages) {
|
||||
if (!beanFactory.containsBean(BASE_PACKAGES_BEAN)) {
|
||||
beanFactory.registerSingleton(BASE_PACKAGES_BEAN, new ArrayList<String>(
|
||||
basePackages));
|
||||
} else {
|
||||
List<String> packages = getBasePackages(beanFactory);
|
||||
for (String pkg : basePackages) {
|
||||
if (packages.contains(pkg)) {
|
||||
packages.add(pkg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.context.annotation;
|
||||
|
||||
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.apache.catalina.core.ApplicationContext;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
|
||||
/**
|
||||
* {@link Conditional} that only matches specific {@link ApplicationContext}s and that can
|
||||
* optionally create them.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@Target({ ElementType.TYPE, ElementType.METHOD })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Conditional(OnApplicationContextCondition.class)
|
||||
public @interface ConditionalOnApplicationContext {
|
||||
|
||||
// FIXME complete of delete this
|
||||
|
||||
/**
|
||||
* The ID of the application context.
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
// FIXME Strategy Interface Class, eg ApplicationContextCondition
|
||||
// condition=SomethingSpecific.class
|
||||
|
||||
boolean createIfMissing() default false;
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.context.annotation;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Condition;
|
||||
import org.springframework.context.annotation.ConditionContext;
|
||||
import org.springframework.core.annotation.AnnotationAttributes;
|
||||
import org.springframework.core.type.AnnotatedTypeMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class OnApplicationContextCondition implements Condition {
|
||||
|
||||
// FIXME complete or delete
|
||||
|
||||
@Override
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
|
||||
AnnotationAttributes attributes = AnnotationAttributes
|
||||
.fromMap(metadata
|
||||
.getAnnotationAttributes(ConditionalOnApplicationContext.class
|
||||
.getName()));
|
||||
String id = (String) attributes.get("value");
|
||||
boolean createIfMissing = attributes.getBoolean("createIfMissing");
|
||||
ApplicationContext applicationContext = context.getApplicationContext();
|
||||
|
||||
if (applicationContext != null) {
|
||||
if (StringUtils.hasLength(id) && applicationContext.getId().equals(id)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (createIfMissing) {
|
||||
registerCreate(applicationContext, metadata);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param applicationContext
|
||||
* @param metadata
|
||||
*/
|
||||
private void registerCreate(ApplicationContext applicationContext,
|
||||
AnnotatedTypeMetadata metadata) {
|
||||
Assert.notNull(applicationContext,
|
||||
"Unable to create ApplicationContext from @ConditionalOnApplicationContext");
|
||||
}
|
||||
}
|
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.context.embedded;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
|
||||
/**
|
||||
* {@link BeanPostProcessor} that apply all {@link EmbeddedServletContainerCustomizer}s
|
||||
* from the bean factory to {@link ConfigurableEmbeddedServletContainerFactory} beans.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
public class EmbeddedServletContainerCustomizerBeanPostProcessor implements
|
||||
BeanPostProcessor, ApplicationContextAware {
|
||||
|
||||
// FIXME should we register this by default, Javadoc in
|
||||
// EmbeddedServletContainerCustomizer suggests so
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private List<EmbeddedServletContainerCustomizer> customizers;
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainerFactory) {
|
||||
postProcessBeforeInitialization((ConfigurableEmbeddedServletContainerFactory) bean);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
private void postProcessBeforeInitialization(
|
||||
ConfigurableEmbeddedServletContainerFactory bean) {
|
||||
for (EmbeddedServletContainerCustomizer customizer : getCustomizers()) {
|
||||
customizer.customize(bean);
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<EmbeddedServletContainerCustomizer> getCustomizers() {
|
||||
if (this.customizers == null) {
|
||||
// Look up does not include the parent context
|
||||
this.customizers = new ArrayList<EmbeddedServletContainerCustomizer>(
|
||||
this.applicationContext.getBeansOfType(
|
||||
EmbeddedServletContainerCustomizer.class).values());
|
||||
Collections.sort(this.customizers, AnnotationAwareOrderComparator.INSTANCE);
|
||||
this.customizers = Collections.unmodifiableList(this.customizers);
|
||||
}
|
||||
return this.customizers;
|
||||
}
|
||||
|
||||
}
|
@ -1,18 +1,21 @@
|
||||
# Auto Configure
|
||||
org.springframework.bootstrap.context.annotation.EnableAutoConfiguration=\
|
||||
org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.MessageSourceAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.PropertyPlaceholderAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.data.JpaRepositoriesAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.batch.BatchAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.thymeleaf.ThymeleafAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedContainerCustomizerConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.EmbeddedServletContainerAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.ServerPropertiesAutoConfiguration,\
|
||||
org.springframework.bootstrap.autoconfigure.web.WebMvcAutoConfiguration
|
||||
|
||||
# Application Context Initializers
|
||||
org.springframework.context.ApplicationContextInitializer=\
|
||||
org.springframework.bootstrap.context.initializer.ConfigFileApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.LoggingApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.VcapApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.ContextIdApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.EnvironmentDelegateApplicationContextInitializer
|
||||
org.springframework.bootstrap.context.initializer.EnvironmentDelegateApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.LoggingApplicationContextInitializer,\
|
||||
org.springframework.bootstrap.context.initializer.VcapApplicationContextInitializer
|
||||
|
@ -0,0 +1,125 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.autoconfigure.web;
|
||||
|
||||
import javax.servlet.Servlet;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.bootstrap.context.annotation.ConditionalOnExpression;
|
||||
import org.springframework.bootstrap.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.bootstrap.context.embedded.ConfigurableEmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerCustomizer;
|
||||
import org.springframework.bootstrap.context.embedded.EmbeddedServletContainerFactory;
|
||||
import org.springframework.bootstrap.context.embedded.MockEmbeddedServletContainerFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
/**
|
||||
* Tests for {@link EmbeddedServletContainerAutoConfiguration}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class EmbeddedServletContainerAutoConfigurationTests {
|
||||
|
||||
private AnnotationConfigEmbeddedWebApplicationContext context;
|
||||
|
||||
@Test
|
||||
public void createFromConfigClass() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class);
|
||||
verifyContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void containerHasNoServletContext() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
EnsureContainerHasNoServletContext.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class);
|
||||
verifyContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizeContainerThroughCallback() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
|
||||
EmbeddedContainerConfiguration.class,
|
||||
CallbackEmbeddedContainerCustomizer.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class);
|
||||
verifyContext();
|
||||
assertEquals(9000, getContainerFactory().getPort());
|
||||
}
|
||||
|
||||
private void verifyContext() {
|
||||
MockEmbeddedServletContainerFactory containerFactory = getContainerFactory();
|
||||
Servlet servlet = this.context.getBean(Servlet.class);
|
||||
verify(containerFactory.getServletContext()).addServlet("dispatcherServlet",
|
||||
servlet);
|
||||
}
|
||||
|
||||
private MockEmbeddedServletContainerFactory getContainerFactory() {
|
||||
return this.context.getBean(MockEmbeddedServletContainerFactory.class);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnExpression("true")
|
||||
public static class EmbeddedContainerConfiguration {
|
||||
|
||||
@Bean
|
||||
public EmbeddedServletContainerFactory containerFactory() {
|
||||
return new MockEmbeddedServletContainerFactory();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
public static class EnsureContainerHasNoServletContext implements BeanPostProcessor {
|
||||
|
||||
@Override
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName)
|
||||
throws BeansException {
|
||||
if (bean instanceof ConfigurableEmbeddedServletContainerFactory) {
|
||||
MockEmbeddedServletContainerFactory containerFactory = (MockEmbeddedServletContainerFactory) bean;
|
||||
assertNull(containerFactory.getServletContext());
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) {
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Component
|
||||
public static class CallbackEmbeddedContainerCustomizer implements
|
||||
EmbeddedServletContainerCustomizer {
|
||||
@Override
|
||||
public void customize(ConfigurableEmbeddedServletContainerFactory factory) {
|
||||
factory.setPort(9000);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,124 @@
|
||||
/*
|
||||
* Copyright 2012-2013 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.bootstrap.context.annotation;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link OnApplicationContextCondition}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
*/
|
||||
@SuppressWarnings("resource")
|
||||
public class OnApplicationContextConditionTest {
|
||||
|
||||
@Test
|
||||
public void forContextById() throws Exception {
|
||||
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
|
||||
parent.setId("parent");
|
||||
parent.register(ForContextByIdConf.class);
|
||||
|
||||
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||
child.setId("child");
|
||||
child.setParent(parent);
|
||||
child.register(ForContextByIdConf.class);
|
||||
|
||||
parent.refresh();
|
||||
child.refresh();
|
||||
|
||||
assertThat(parent.containsLocalBean("inParent"), equalTo(true));
|
||||
assertThat(parent.containsLocalBean("inChild"), equalTo(false));
|
||||
|
||||
assertThat(child.containsLocalBean("inParent"), equalTo(false));
|
||||
assertThat(child.containsLocalBean("inChild"), equalTo(true));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore
|
||||
public void createContext() throws Exception {
|
||||
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
|
||||
ApplicationContextCollector collector = new ApplicationContextCollector();
|
||||
parent.addApplicationListener(collector);
|
||||
parent.register(CreateContext.class);
|
||||
parent.refresh();
|
||||
assertThat(collector.get("child").containsLocalBean("inChild"), equalTo(true));
|
||||
}
|
||||
|
||||
// FIXME
|
||||
// createContextOnBeanMethod
|
||||
// createContextComponent
|
||||
|
||||
private static class ApplicationContextCollector implements
|
||||
ApplicationListener<ContextRefreshedEvent> {
|
||||
|
||||
private Set<ApplicationContext> contexts = new HashSet<ApplicationContext>();
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(ContextRefreshedEvent event) {
|
||||
this.contexts.add(event.getApplicationContext());
|
||||
}
|
||||
|
||||
public ApplicationContext get(String id) {
|
||||
for (ApplicationContext context : this.contexts) {
|
||||
if (ObjectUtils.nullSafeEquals(context.getId(), id)) {
|
||||
return context;
|
||||
}
|
||||
}
|
||||
throw new IllegalArgumentException("No such ID " + id);
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
public static class ForContextByIdConf {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnApplicationContext("parent")
|
||||
public String inParent() {
|
||||
return "inParent";
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnApplicationContext("child")
|
||||
public String inChild() {
|
||||
return "inChild";
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnApplicationContext(value = "child", createIfMissing = true)
|
||||
public static class CreateContext {
|
||||
|
||||
@Bean
|
||||
public String inChild() {
|
||||
return "inChild";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue