Skip to content

Commit d01154c

Browse files
committed
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 spring-projectsgh-1238
1 parent 249e09d commit d01154c

File tree

7 files changed

+216
-74
lines changed

7 files changed

+216
-74
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mobile/DeviceDelegatingViewResolverAutoConfiguration.java

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@
2828
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2929
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
31-
import org.springframework.boot.bind.RelaxedPropertyResolver;
31+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3232
import org.springframework.context.EnvironmentAware;
3333
import org.springframework.context.annotation.Bean;
3434
import org.springframework.context.annotation.Configuration;
3535
import org.springframework.core.Ordered;
36-
import org.springframework.core.env.Environment;
3736
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
3837
import org.springframework.web.servlet.ViewResolver;
3938
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@@ -57,35 +56,20 @@ public class DeviceDelegatingViewResolverAutoConfiguration {
5756
private static Log logger = LogFactory
5857
.getLog(DeviceDelegatingViewResolverAutoConfiguration.class);
5958

60-
private static abstract class AbstractDelegateConfiguration implements
61-
EnvironmentAware {
59+
private static abstract class AbstractDelegateConfiguration {
6260

63-
private RelaxedPropertyResolver environment;
64-
65-
@Override
66-
public void setEnvironment(Environment environment) {
67-
this.environment = new RelaxedPropertyResolver(environment,
68-
"spring.mobile.devicedelegatingviewresolver.");
69-
}
61+
@Autowired
62+
private DeviceDelegatingViewResolverProperties viewResolverProperties;
7063

7164
protected LiteDeviceDelegatingViewResolver getConfiguredViewResolver(
7265
ViewResolver delegate, int delegateOrder) {
7366
LiteDeviceDelegatingViewResolver resolver = new LiteDeviceDelegatingViewResolver(
7467
delegate);
75-
resolver.setNormalPrefix(getProperty("normal-prefix", ""));
76-
resolver.setNormalSuffix(getProperty("normal-suffix", ""));
77-
resolver.setMobilePrefix(getProperty("mobile-prefix", "mobile/"));
78-
resolver.setMobileSuffix(getProperty("mobile-suffix", ""));
79-
resolver.setTabletPrefix(getProperty("tablet-prefix", "tablet/"));
80-
resolver.setTabletSuffix(getProperty("tablet-suffix", ""));
68+
viewResolverProperties.apply(resolver);
8169
resolver.setOrder(getAdjustedOrder(delegateOrder));
8270
return resolver;
8371
}
8472

85-
private String getProperty(String key, String defaultValue) {
86-
return this.environment.getProperty(key, defaultValue);
87-
}
88-
8973
private int getAdjustedOrder(int order) {
9074
if (order == Ordered.HIGHEST_PRECEDENCE) {
9175
return Ordered.HIGHEST_PRECEDENCE;
@@ -98,6 +82,7 @@ private int getAdjustedOrder(int order) {
9882
}
9983

10084
@Configuration
85+
@EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class)
10186
@ConditionalOnMissingBean(name = "deviceDelegatingViewResolver")
10287
@ConditionalOnExpression("${spring.mobile.devicedelegatingviewresolver.enabled:false}")
10388
protected static class DeviceDelegatingViewResolverConfiguration {
@@ -123,6 +108,7 @@ public LiteDeviceDelegatingViewResolver deviceDelegatingViewResolver() {
123108
}
124109

125110
@Configuration
111+
@EnableConfigurationProperties(DeviceDelegatingViewResolverProperties.class)
126112
@ConditionalOnMissingBean(name = "thymeleafViewResolver")
127113
@ConditionalOnBean(InternalResourceViewResolver.class)
128114
protected static class InternalResourceViewResolverDelegateConfiguration extends
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.mobile;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver;
21+
22+
/**
23+
* {@link ConfigurationProperties properties} for device view resolver.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 1.2.0
27+
*/
28+
@ConfigurationProperties("spring.mobile.devicedelegatingviewresolver")
29+
public class DeviceDelegatingViewResolverProperties {
30+
31+
private String normalPrefix = "";
32+
33+
private String normalSuffix = "";
34+
35+
private String mobilePrefix = "mobile/";
36+
37+
private String mobileSuffix = "";
38+
39+
private String tabletPrefix = "tablet/";
40+
41+
private String tabletSuffix = "";
42+
43+
public String getNormalPrefix() {
44+
return normalPrefix;
45+
}
46+
47+
public void setNormalPrefix(String normalPrefix) {
48+
this.normalPrefix = normalPrefix;
49+
}
50+
51+
public String getNormalSuffix() {
52+
return normalSuffix;
53+
}
54+
55+
public void setNormalSuffix(String normalSuffix) {
56+
this.normalSuffix = normalSuffix;
57+
}
58+
59+
public String getMobilePrefix() {
60+
return mobilePrefix;
61+
}
62+
63+
public void setMobilePrefix(String mobilePrefix) {
64+
this.mobilePrefix = mobilePrefix;
65+
}
66+
67+
public String getMobileSuffix() {
68+
return mobileSuffix;
69+
}
70+
71+
public void setMobileSuffix(String mobileSuffix) {
72+
this.mobileSuffix = mobileSuffix;
73+
}
74+
75+
public String getTabletPrefix() {
76+
return tabletPrefix;
77+
}
78+
79+
public void setTabletPrefix(String tabletPrefix) {
80+
this.tabletPrefix = tabletPrefix;
81+
}
82+
83+
public String getTabletSuffix() {
84+
return tabletSuffix;
85+
}
86+
87+
public void setTabletSuffix(String tabletSuffix) {
88+
this.tabletSuffix = tabletSuffix;
89+
}
90+
91+
public void apply(LiteDeviceDelegatingViewResolver resolver) {
92+
resolver.setNormalPrefix(getNormalPrefix());
93+
resolver.setNormalSuffix(getNormalSuffix());
94+
resolver.setMobilePrefix(getMobilePrefix());
95+
resolver.setMobileSuffix(getMobileSuffix());
96+
resolver.setTabletPrefix(getTabletPrefix());
97+
resolver.setTabletSuffix(getTabletSuffix());
98+
}
99+
100+
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.social;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2021
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -24,7 +25,8 @@
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2627
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
27-
import org.springframework.boot.bind.RelaxedPropertyResolver;
28+
import org.springframework.boot.context.properties.ConfigurationProperties;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2830
import org.springframework.context.annotation.Bean;
2931
import org.springframework.context.annotation.Configuration;
3032
import org.springframework.context.annotation.Scope;
@@ -56,21 +58,17 @@ public class FacebookAutoConfiguration {
5658

5759
@Configuration
5860
@EnableSocial
61+
@EnableConfigurationProperties(FaceBookProperties.class)
5962
@ConditionalOnWebApplication
6063
protected static class FacebookAutoConfigurationAdapter extends
6164
SocialAutoConfigurerAdapter {
6265

63-
@Override
64-
protected String getPropertyPrefix() {
65-
return "spring.social.facebook.";
66-
}
66+
@Autowired
67+
private FaceBookProperties faceBookProperties;
6768

6869
@Override
69-
protected ConnectionFactory<?> createConnectionFactory(
70-
RelaxedPropertyResolver properties) {
71-
return new FacebookConnectionFactory(
72-
properties.getRequiredProperty("app-id"),
73-
properties.getRequiredProperty("app-secret"));
70+
protected SocialProperties getSocialProperties() {
71+
return faceBookProperties;
7472
}
7573

7674
@Bean
@@ -90,4 +88,14 @@ public View facebookConnectView() {
9088

9189
}
9290

91+
@ConfigurationProperties("spring.social.facebook")
92+
public static class FaceBookProperties extends SocialProperties {
93+
94+
public ConnectionFactory<?> createConnectionFactory() {
95+
return new FacebookConnectionFactory(
96+
getAppId(), getAppSecret());
97+
}
98+
99+
}
100+
93101
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.social;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2021
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -24,7 +25,8 @@
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
2627
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
27-
import org.springframework.boot.bind.RelaxedPropertyResolver;
28+
import org.springframework.boot.context.properties.ConfigurationProperties;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2830
import org.springframework.context.annotation.Bean;
2931
import org.springframework.context.annotation.Configuration;
3032
import org.springframework.context.annotation.Scope;
@@ -55,21 +57,17 @@ public class LinkedInAutoConfiguration {
5557

5658
@Configuration
5759
@EnableSocial
60+
@EnableConfigurationProperties(LinkedInProperties.class)
5861
@ConditionalOnWebApplication
5962
protected static class LinkedInAutoConfigurationAdapter extends
6063
SocialAutoConfigurerAdapter {
6164

62-
@Override
63-
protected String getPropertyPrefix() {
64-
return "spring.social.linkedin.";
65-
}
65+
@Autowired
66+
private LinkedInProperties linkedInProperties;
6667

6768
@Override
68-
protected ConnectionFactory<?> createConnectionFactory(
69-
RelaxedPropertyResolver properties) {
70-
return new LinkedInConnectionFactory(
71-
properties.getRequiredProperty("app-id"),
72-
properties.getRequiredProperty("app-secret"));
69+
protected SocialProperties getSocialProperties() {
70+
return linkedInProperties;
7371
}
7472

7573
@Bean
@@ -89,4 +87,14 @@ public View linkedInConnectView() {
8987

9088
}
9189

90+
@ConfigurationProperties("spring.social.linkedin")
91+
public static class LinkedInProperties extends SocialProperties {
92+
93+
public ConnectionFactory<?> createConnectionFactory() {
94+
return new LinkedInConnectionFactory(
95+
getAppId(), getAppSecret());
96+
}
97+
98+
}
99+
92100
}

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,9 @@
1616

1717
package org.springframework.boot.autoconfigure.social;
1818

19-
import org.springframework.boot.bind.RelaxedPropertyResolver;
20-
import org.springframework.context.EnvironmentAware;
2119
import org.springframework.core.env.Environment;
2220
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
2321
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
24-
import org.springframework.social.connect.ConnectionFactory;
2522

2623
/**
2724
* Base class for auto-configured {@link SocialConfigurerAdapter}s.
@@ -30,29 +27,14 @@
3027
* @author Phillip Webb
3128
* @since 1.1.0
3229
*/
33-
abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter implements
34-
EnvironmentAware {
30+
abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter {
3531

36-
private RelaxedPropertyResolver properties;
37-
38-
@Override
39-
public void setEnvironment(Environment environment) {
40-
this.properties = new RelaxedPropertyResolver(environment, getPropertyPrefix());
41-
}
42-
43-
protected abstract String getPropertyPrefix();
32+
protected abstract SocialProperties getSocialProperties();
4433

4534
@Override
4635
public void addConnectionFactories(ConnectionFactoryConfigurer configurer,
4736
Environment environment) {
48-
configurer.addConnectionFactory(createConnectionFactory(this.properties));
37+
configurer.addConnectionFactory(getSocialProperties().createConnectionFactory());
4938
}
5039

51-
protected final RelaxedPropertyResolver getProperties() {
52-
return this.properties;
53-
}
54-
55-
protected abstract ConnectionFactory<?> createConnectionFactory(
56-
RelaxedPropertyResolver properties);
57-
5840
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.social;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
import org.springframework.social.connect.ConnectionFactory;
21+
22+
/**
23+
* Base {@link ConfigurationProperties properties} for spring social.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 1.2.0
27+
*/
28+
abstract class SocialProperties {
29+
30+
private String appId;
31+
32+
private String appSecret;
33+
34+
public String getAppId() {
35+
return appId;
36+
}
37+
38+
public void setAppId(String appId) {
39+
this.appId = appId;
40+
}
41+
42+
public String getAppSecret() {
43+
return appSecret;
44+
}
45+
46+
public void setAppSecret(String appSecret) {
47+
this.appSecret = appSecret;
48+
}
49+
50+
public abstract ConnectionFactory<?> createConnectionFactory();
51+
}

0 commit comments

Comments
 (0)