Browse Source

Fix social property binding

Update Spring Social auto-configurations to read properties using
the `dashed` notation and with the appropriate prefixes. This allows
properties to be specified in any of the relaxed forms.

Also minor refactor to extract common logic to a new
SocialAutoConfigurerAdapter base class.

See gh-941
pull/1016/head
Phillip Webb 12 years ago
parent
commit
7fd26a556d
  1. 2
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java
  2. 30
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/FacebookAutoConfiguration.java
  3. 30
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/LinkedInAutoConfiguration.java
  4. 58
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java
  5. 6
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java
  6. 37
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/TwitterAutoConfiguration.java

2
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java

@ -26,7 +26,7 @@ import org.springframework.core.env.Environment; @@ -26,7 +26,7 @@ import org.springframework.core.env.Environment;
/**
* {@link Conditional} that only matches when the specified properties are defined in
* {@link Environment}
* {@link Environment} and not "false".
*
* @author Maciej Walkowiak
* @since 1.1.0

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

@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social; @@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.GenericConnectionStatusView;
import org.springframework.social.facebook.api.Facebook;
@ -57,24 +54,19 @@ public class FacebookAutoConfiguration { @@ -57,24 +54,19 @@ public class FacebookAutoConfiguration {
@EnableSocial
@ConditionalOnWebApplication
protected static class FacebookAutoConfigurationAdapter extends
SocialConfigurerAdapter implements EnvironmentAware {
private String appId;
private String appSecret;
SocialAutoConfigurerAdapter {
@Override
public void setEnvironment(Environment env) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env,
"spring.social.");
this.appId = propertyResolver.getRequiredProperty("facebook.appId");
this.appSecret = propertyResolver.getRequiredProperty("facebook.appSecret");
protected String getPropertyPrefix() {
return "spring.social.facebook.";
}
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer config,
Environment env) {
config.addConnectionFactory(new FacebookConnectionFactory(this.appId,
this.appSecret));
protected ConnectionFactory<?> createConnectionFactory(
RelaxedPropertyResolver properties) {
return new FacebookConnectionFactory(
properties.getRequiredProperty("app-id"),
properties.getRequiredProperty("app-secret"));
}
@Bean
@ -87,7 +79,7 @@ public class FacebookAutoConfiguration { @@ -87,7 +79,7 @@ public class FacebookAutoConfiguration {
}
@Bean(name = { "connect/facebookConnect", "connect/facebookConnected" })
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View facebookConnectView() {
return new GenericConnectionStatusView("facebook", "Facebook");
}

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

@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social; @@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.GenericConnectionStatusView;
import org.springframework.social.linkedin.api.LinkedIn;
@ -56,24 +53,19 @@ public class LinkedInAutoConfiguration { @@ -56,24 +53,19 @@ public class LinkedInAutoConfiguration {
@EnableSocial
@ConditionalOnWebApplication
protected static class LinkedInAutoConfigurationAdapter extends
SocialConfigurerAdapter implements EnvironmentAware {
private String appId;
private String appSecret;
SocialAutoConfigurerAdapter {
@Override
public void setEnvironment(Environment env) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env,
"spring.social.");
this.appId = propertyResolver.getRequiredProperty("linkedin.appId");
this.appSecret = propertyResolver.getRequiredProperty("linkedin.appSecret");
protected String getPropertyPrefix() {
return "spring.social.linkedin.";
}
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig,
Environment env) {
cfConfig.addConnectionFactory(new LinkedInConnectionFactory(this.appId,
this.appSecret));
protected ConnectionFactory<?> createConnectionFactory(
RelaxedPropertyResolver properties) {
return new LinkedInConnectionFactory(
properties.getRequiredProperty("app-id"),
properties.getRequiredProperty("app-secret"));
}
@Bean
@ -86,7 +78,7 @@ public class LinkedInAutoConfiguration { @@ -86,7 +78,7 @@ public class LinkedInAutoConfiguration {
}
@Bean(name = { "connect/linkedinConnect", "connect/linkedinConnected" })
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View linkedInConnectView() {
return new GenericConnectionStatusView("linkedin", "LinkedIn");
}

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

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
/*
* 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.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.
*
* @author Phillip Webb
* @author Craig Walls
* @since 1.1.0
*/
abstract class SocialAutoConfigurerAdapter extends SocialConfigurerAdapter implements
EnvironmentAware {
private RelaxedPropertyResolver properties;
@Override
public void setEnvironment(Environment environment) {
this.properties = new RelaxedPropertyResolver(environment, getPropertyPrefix());
}
protected abstract String getPropertyPrefix();
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer configurer,
Environment environment) {
configurer.addConnectionFactory(createConnectionFactory(this.properties));
}
protected final RelaxedPropertyResolver getProperties() {
return this.properties;
}
protected abstract ConnectionFactory<?> createConnectionFactory(
RelaxedPropertyResolver properties);
}

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

@ -19,8 +19,8 @@ package org.springframework.boot.autoconfigure.social; @@ -19,8 +19,8 @@ package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.context.annotation.Bean;
@ -50,6 +50,7 @@ public class SocialWebAutoConfiguration { @@ -50,6 +50,7 @@ public class SocialWebAutoConfiguration {
@EnableSocial
@ConditionalOnWebApplication
protected static class SocialAutoConfigurationAdapter extends SocialConfigurerAdapter {
@Bean
@ConditionalOnMissingBean(ConnectController.class)
public ConnectController connectController(
@ -60,7 +61,7 @@ public class SocialWebAutoConfiguration { @@ -60,7 +61,7 @@ public class SocialWebAutoConfiguration {
@Bean
@ConditionalOnMissingBean(BeanNameViewResolver.class)
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public ViewResolver beanNameViewResolver() {
BeanNameViewResolver bnvr = new BeanNameViewResolver();
bnvr.setOrder(Integer.MIN_VALUE);
@ -76,6 +77,7 @@ public class SocialWebAutoConfiguration { @@ -76,6 +77,7 @@ public class SocialWebAutoConfiguration {
}
};
}
}
}

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

@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social; @@ -19,21 +19,18 @@ package org.springframework.boot.autoconfigure.social;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.core.env.Environment;
import org.springframework.social.config.annotation.ConnectionFactoryConfigurer;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionFactory;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.connect.web.GenericConnectionStatusView;
import org.springframework.social.twitter.api.Twitter;
@ -57,24 +54,18 @@ public class TwitterAutoConfiguration { @@ -57,24 +54,18 @@ public class TwitterAutoConfiguration {
@EnableSocial
@ConditionalOnWebApplication
protected static class TwitterAutoConfigurationAdapter extends
SocialConfigurerAdapter implements EnvironmentAware {
private String appId;
private String appSecret;
SocialAutoConfigurerAdapter {
@Override
public void setEnvironment(Environment env) {
RelaxedPropertyResolver propertyResolver = new RelaxedPropertyResolver(env,
"spring.social.");
this.appId = propertyResolver.getRequiredProperty("twitter.appId");
this.appSecret = propertyResolver.getRequiredProperty("twitter.appSecret");
protected String getPropertyPrefix() {
return "spring.social.twitter.";
}
@Override
public void addConnectionFactories(ConnectionFactoryConfigurer cfConfig,
Environment env) {
cfConfig.addConnectionFactory(new TwitterConnectionFactory(this.appId,
this.appSecret));
protected ConnectionFactory<?> createConnectionFactory(
RelaxedPropertyResolver properties) {
return new TwitterConnectionFactory(properties.getRequiredProperty("app-id"),
properties.getRequiredProperty("app-secret"));
}
@Bean
@ -83,12 +74,16 @@ public class TwitterAutoConfiguration { @@ -83,12 +74,16 @@ public class TwitterAutoConfiguration {
public Twitter twitter(ConnectionRepository repository) {
Connection<Twitter> connection = repository
.findPrimaryConnection(Twitter.class);
return connection != null ? connection.getApi() : new TwitterTemplate(
this.appId, this.appSecret);
if (connection != null) {
return connection.getApi();
}
String id = getProperties().getRequiredProperty("app-id");
String secret = getProperties().getRequiredProperty("app-secret");
return new TwitterTemplate(id, secret);
}
@Bean(name = { "connect/twitterConnect", "connect/twitterConnected" })
@ConditionalOnExpression("${spring.social.auto_connection_views:false}")
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View twitterConnectView() {
return new GenericConnectionStatusView("twitter", "Twitter");
}

Loading…
Cancel
Save