diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java index 04a35d9abf..0063297876 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionalOnProperty.java @@ -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 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 c9e9719971..86d1838ea9 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 @@ -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; @@ -44,7 +41,7 @@ import org.springframework.web.servlet.View; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity with * Facebook. - * + * * @author Craig Walls * @since 1.1.0 */ @@ -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 { } @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"); } 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 da95ee8cbc..7d6cd5299a 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 @@ -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; @@ -43,7 +40,7 @@ import org.springframework.web.servlet.View; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity with * LinkedIn. - * + * * @author Craig Walls * @since 1.1.0 */ @@ -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 { } @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"); } 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 new file mode 100644 index 0000000000..fc55904924 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialAutoConfigurerAdapter.java @@ -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); + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java index c98998e0ca..b055c8f474 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/social/SocialWebAutoConfiguration.java @@ -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; @@ -37,7 +37,7 @@ import org.springframework.web.servlet.view.BeanNameViewResolver; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Social's web connection * support. - * + * * @author Craig Walls * @since 1.1.0 */ @@ -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 { @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 { } }; } + } } 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 a1dbadff2e..fd2dcae0d0 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 @@ -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; @@ -44,7 +41,7 @@ import org.springframework.web.servlet.View; /** * {@link EnableAutoConfiguration Auto-configuration} for Spring Social connectivity with * Twitter. - * + * * @author Craig Walls * @since 1.1.0 */ @@ -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 { public Twitter twitter(ConnectionRepository repository) { Connection 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"); } diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialFacebookCompilerAutoConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialFacebookCompilerAutoConfiguration.java index 37fe98393c..7535e68d05 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialFacebookCompilerAutoConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialFacebookCompilerAutoConfiguration.java @@ -25,7 +25,7 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer; /** * {@link CompilerAutoConfiguration} for Spring Social Facebook. - * + * * @author Craig Walls * @since 1.1.0 */ diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialLinkedInCompilerAutoConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialLinkedInCompilerAutoConfiguration.java index 1f2763bd6b..e2192df835 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialLinkedInCompilerAutoConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialLinkedInCompilerAutoConfiguration.java @@ -25,7 +25,7 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer; /** * {@link CompilerAutoConfiguration} for Spring Social LinkedIn. - * + * * @author Craig Walls * @since 1.1.0 */ diff --git a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialTwitterCompilerAutoConfiguration.java b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialTwitterCompilerAutoConfiguration.java index 84a73538a6..7ec113086d 100644 --- a/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialTwitterCompilerAutoConfiguration.java +++ b/spring-boot-cli/src/main/java/org/springframework/boot/cli/compiler/autoconfigure/SpringSocialTwitterCompilerAutoConfiguration.java @@ -25,7 +25,7 @@ import org.springframework.boot.cli.compiler.DependencyCustomizer; /** * {@link CompilerAutoConfiguration} for Spring Social Twitter. - * + * * @author Craig Walls * @since 1.1.0 */ @@ -42,7 +42,7 @@ public class SpringSocialTwitterCompilerAutoConfiguration extends throws CompilationFailedException { dependencies .ifAnyMissingClasses("org.springframework.social.twitter.api.Twitter") - .add("spring-boot-starter-social-twitter"); + .add("spring-boot-starter-social-twitter"); } @Override