From d01154c212791d22d91a6c883e4d7ffea9ba9d5b Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 9 Jul 2014 18:15:35 +0200 Subject: [PATCH] Move Spring Social and Spring Mobile configuration This commit uses dedicated Properties classes instead of accessing the raw environment for Spring Social and Spring Mobile. This improves the readability and the discovery of such properties. Fixes gh-1238 --- ...legatingViewResolverAutoConfiguration.java | 28 ++--- ...eviceDelegatingViewResolverProperties.java | 100 ++++++++++++++++++ .../social/FacebookAutoConfiguration.java | 28 +++-- .../social/LinkedInAutoConfiguration.java | 28 +++-- .../social/SocialAutoConfigurerAdapter.java | 24 +---- .../social/SocialProperties.java | 51 +++++++++ .../social/TwitterAutoConfiguration.java | 31 +++--- 7 files changed, 216 insertions(+), 74 deletions(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverProperties.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialProperties.java diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java index af62f95b73e..747fe360527 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java @@ -28,12 +28,11 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.EnvironmentAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; -import org.springframework.core.env.Environment; import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; @@ -57,35 +56,20 @@ public class DeviceDelegatingViewResolverAutoConfiguration { private static Log logger = LogFactory .getLog(DeviceDelegatingViewResolverAutoConfiguration.class); - private static abstract class AbstractDelegateConfiguration implements - EnvironmentAware { + private static abstract class AbstractDelegateConfiguration { - private RelaxedPropertyResolver environment; - - @Override - public void setEnvironment(Environment environment) { - this.environment = new RelaxedPropertyResolver(environment, - "spring.mobile.devicedelegatingviewresolver."); - } + @Autowired + private DeviceDelegatingViewResolverProperties viewResolverProperties; protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver( ViewResolver delegate, int delegateOrder) { LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver( delegate); - resolver.setNormalPrefix(getProperty("normal-prefix", "")); - resolver.setNormalSuffix(getProperty("normal-suffix", "")); - resolver.setMobilePrefix(getProperty("mobile-prefix", "mobile/")); - resolver.setMobileSuffix(getProperty("mobile-suffix", "")); - resolver.setTabletPrefix(getProperty("tablet-prefix", "tablet/")); - resolver.setTabletSuffix(getProperty("tablet-suffix", "")); + viewResolverProperties.apply(resolver); resolver.setOrder(getAdjustedOrder(delegateOrder)); return resolver; } - private String getProperty(String key, String defaultValue) { - return this.environment.getProperty(key, defaultValue); - } - private int getAdjustedOrder(int order) { if (order == Ordered.HIGHEST_PRECEDENCE) { return Ordered.HIGHEST_PRECEDENCE; @@ -98,6 +82,7 @@ public class DeviceDelegatingViewResolverAutoConfiguration { } @Configuration + @EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class) @ConditionalOnMissingBean(name = "deviceDelegatingViewResolver") @ConditionalOnExpression("${spring.mobile.devicedelegatingviewresolver.enabled:false}") protected static class DeviceDelegatingViewResolverConfiguration { @@ -123,6 +108,7 @@ public class DeviceDelegatingViewResolverAutoConfiguration { } @Configuration + @EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class) @ConditionalOnMissingBean(name = "thymeleafViewResolver") @ConditionalOnBean(InternalResourceViewResolver.class) protected static class InternalResourceViewResolverDelegateConfiguration extends diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverProperties.java new file mode 100644 index 00000000000..8a192c30370 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverProperties.java @@ -0,0 +1,100 @@ +/* + * Copyright 2012-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.boot.autoconfigure.mobile; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver; + +/** + * {@link ConfigurationProperties properties} for device view resolver. + * + * @author Stephane Nicoll + * @since 1.2.0 + */ +@ConfigurationProperties("spring.mobile.devicedelegatingviewresolver") +public class DeviceDelegatingViewResolverProperties { + + private String normalPrefix = ""; + + private String normalSuffix = ""; + + private String mobilePrefix = "mobile/"; + + private String mobileSuffix = ""; + + private String tabletPrefix = "tablet/"; + + private String tabletSuffix = ""; + + public String getNormalPrefix() { + return normalPrefix; + } + + public void setNormalPrefix(String normalPrefix) { + this.normalPrefix = normalPrefix; + } + + public String getNormalSuffix() { + return normalSuffix; + } + + public void setNormalSuffix(String normalSuffix) { + this.normalSuffix = normalSuffix; + } + + public String getMobilePrefix() { + return mobilePrefix; + } + + public void setMobilePrefix(String mobilePrefix) { + this.mobilePrefix = mobilePrefix; + } + + public String getMobileSuffix() { + return mobileSuffix; + } + + public void setMobileSuffix(String mobileSuffix) { + this.mobileSuffix = mobileSuffix; + } + + public String getTabletPrefix() { + return tabletPrefix; + } + + public void setTabletPrefix(String tabletPrefix) { + this.tabletPrefix = tabletPrefix; + } + + public String getTabletSuffix() { + return tabletSuffix; + } + + public void setTabletSuffix(String tabletSuffix) { + this.tabletSuffix = tabletSuffix; + } + + public void apply(LiteDeviceDelegatingViewResolver resolver) { + resolver.setNormalPrefix(getNormalPrefix()); + resolver.setNormalSuffix(getNormalSuffix()); + resolver.setMobilePrefix(getMobilePrefix()); + resolver.setMobileSuffix(getMobileSuffix()); + resolver.setTabletPrefix(getTabletPrefix()); + resolver.setTabletSuffix(getTabletSuffix()); + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java index 17653bb8dba..4d63ffef2b2 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.social; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -24,7 +25,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -56,21 +58,17 @@ public class FacebookAutoConfiguration { @Configuration @EnableSocial + @EnableConfigurationProperties(FaceBookProperties.class) @ConditionalOnWebApplication protected static class FacebookAutoConfigurationAdapter extends SocialAutoConfigurerAdapter { - @Override - protected String getPropertyPrefix() { - return "spring.social.facebook."; - } + @Autowired + private FaceBookProperties faceBookProperties; @Override - protected ConnectionFactory createConnectionFactory( - RelaxedPropertyResolver properties) { - return new FacebookConnectionFactory( - properties.getRequiredProperty("app-id"), - properties.getRequiredProperty("app-secret")); + protected SocialProperties getSocialProperties() { + return faceBookProperties; } @Bean @@ -90,4 +88,14 @@ public class FacebookAutoConfiguration { } + @ConfigurationProperties("spring.social.facebook") + public static class FaceBookProperties extends SocialProperties { + + public ConnectionFactory createConnectionFactory() { + return new FacebookConnectionFactory( + getAppId(), getAppSecret()); + } + + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java index 827f0794ba7..17b5858dd2a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.social; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -24,7 +25,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -55,21 +57,17 @@ public class LinkedInAutoConfiguration { @Configuration @EnableSocial + @EnableConfigurationProperties(LinkedInProperties.class) @ConditionalOnWebApplication protected static class LinkedInAutoConfigurationAdapter extends SocialAutoConfigurerAdapter { - @Override - protected String getPropertyPrefix() { - return "spring.social.linkedin."; - } + @Autowired + private LinkedInProperties linkedInProperties; @Override - protected ConnectionFactory createConnectionFactory( - RelaxedPropertyResolver properties) { - return new LinkedInConnectionFactory( - properties.getRequiredProperty("app-id"), - properties.getRequiredProperty("app-secret")); + protected SocialProperties getSocialProperties() { + return linkedInProperties; } @Bean @@ -89,4 +87,14 @@ public class LinkedInAutoConfiguration { } + @ConfigurationProperties("spring.social.linkedin") + public static class LinkedInProperties extends SocialProperties { + + public ConnectionFactory createConnectionFactory() { + return new LinkedInConnectionFactory( + getAppId(), getAppSecret()); + } + + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java index 93050f7aa61..bde673f1bca 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java @@ -16,12 +16,9 @@ package org.springframework.boot.autoconfigure.social; -import org.springframework.boot.bind.RelaxedPropertyResolver; -import org.springframework.context.EnvironmentAware; import org.springframework.core.env.Environment; import org.springframework.social.config.annotation.ConnectionFactoryConfigurer; import org.springframework.social.config.annotation.SocialConfigurerAdapter; -import org.springframework.social.connect.ConnectionFactory; /** * Base class for auto-configured {@link SocialConfigurerAdapter}s. @@ -30,29 +27,14 @@ import org.springframework.social.connect.ConnectionFactory; * @author Phillip Webb * @since 1.1.0 */ -abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter implements - EnvironmentAware { +abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter { - private RelaxedPropertyResolver properties; - - @Override - public void setEnvironment(Environment environment) { - this.properties = new RelaxedPropertyResolver(environment, getPropertyPrefix()); - } - - protected abstract String getPropertyPrefix(); + protected abstract SocialProperties getSocialProperties(); @Override public void addConnectionFactories(ConnectionFactoryConfigurer configurer, Environment environment) { - configurer.addConnectionFactory(createConnectionFactory(this.properties)); + configurer.addConnectionFactory(getSocialProperties().createConnectionFactory()); } - protected final RelaxedPropertyResolver getProperties() { - return this.properties; - } - - protected abstract ConnectionFactory createConnectionFactory( - RelaxedPropertyResolver properties); - } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialProperties.java new file mode 100644 index 00000000000..9e1a4915b13 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialProperties.java @@ -0,0 +1,51 @@ +/* + * Copyright 2012-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.boot.autoconfigure.social; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.social.connect.ConnectionFactory; + +/** + * Base {@link ConfigurationProperties properties} for spring social. + * + * @author Stephane Nicoll + * @since 1.2.0 + */ +abstract class SocialProperties { + + private String appId; + + private String appSecret; + + public String getAppId() { + return appId; + } + + public void setAppId(String appId) { + this.appId = appId; + } + + public String getAppSecret() { + return appSecret; + } + + public void setAppSecret(String appSecret) { + this.appSecret = appSecret; + } + + public abstract ConnectionFactory createConnectionFactory(); +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java index 8da9f1772e2..0accfa99e1d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.boot.autoconfigure.social; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureBefore; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -24,7 +25,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; -import org.springframework.boot.bind.RelaxedPropertyResolver; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; @@ -56,20 +58,17 @@ public class TwitterAutoConfiguration { @Configuration @EnableSocial + @EnableConfigurationProperties(TwitterProperties.class) @ConditionalOnWebApplication protected static class TwitterAutoConfigurationAdapter extends SocialAutoConfigurerAdapter { - @Override - protected String getPropertyPrefix() { - return "spring.social.twitter."; - } + @Autowired + private TwitterProperties twitterProperties; @Override - protected ConnectionFactory createConnectionFactory( - RelaxedPropertyResolver properties) { - return new TwitterConnectionFactory(properties.getRequiredProperty("app-id"), - properties.getRequiredProperty("app-secret")); + protected SocialProperties getSocialProperties() { + return twitterProperties; } @Bean @@ -81,9 +80,7 @@ public class TwitterAutoConfiguration { if (connection != null) { return connection.getApi(); } - String id = getProperties().getRequiredProperty("app-id"); - String secret = getProperties().getRequiredProperty("app-secret"); - return new TwitterTemplate(id, secret); + return new TwitterTemplate(twitterProperties.getAppId(), twitterProperties.getAppSecret()); } @Bean(name = { "connect/twitterConnect", "connect/twitterConnected" }) @@ -94,4 +91,14 @@ public class TwitterAutoConfiguration { } + @ConfigurationProperties("spring.social.twitter") + public static class TwitterProperties extends SocialProperties { + + public ConnectionFactory createConnectionFactory() { + return new TwitterConnectionFactory( + getAppId(), getAppSecret()); + } + + } + }