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
This commit is contained in:
Stephane Nicoll 2014-07-09 18:15:35 +02:00
parent 249e09d9bc
commit d01154c212
7 changed files with 216 additions and 74 deletions

View File

@ -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

View File

@ -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());
}
}

View File

@ -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());
}
}
}

View File

@ -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());
}
}
}

View File

@ -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);
}

View File

@ -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();
}

View File

@ -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());
}
}
}