提供商'twitter'的ConnectionFactory已经注册

时间:2015-07-30 20:03:01

标签: java facebook twitter spring-boot spring-social

我正在覆盖Spring Social Twitter和Facebook的默认Spring Boot配置。运行应用程序时出现以下错误。

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactoryLocator' defined in class path resource [org/springframework/social/config/annotation/SocialConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.social.connect.ConnectionFactoryLocator]: Factory method 'connectionFactoryLocator' threw exception; nested exception is java.lang.IllegalArgumentException: A ConnectionFactory for provider 'twitter' has' already been registered

我觉得这种情况正在发生,因为它与默认配置有些冲突。代码如下,我该如何解决这个问题?

SocialConfig

@Configuration
@EnableSocial
public class SocialConfig implements SocialConfigurer{

protected static final String FACEBOOK_APP_ID = "spring.social.facebook.appId";
protected static final String FACEBOOK_APP_SECRET = "spring.social.facebook.appSecret";

protected static final String TWITTER_APP_ID = "spring.social.twitter.appId";
protected static final String TWITTER_APP_SECRET = "spring.social.twitter.appSecret";
private DataSource dataSource;

@Inject
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
}

@Override
public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer,Environment env) {
    connectionFactoryConfigurer.addConnectionFactory(getFacebookConnectionFactory(env));
    connectionFactoryConfigurer.addConnectionFactory(getTwitterConnectionFactory(env));
}

@Override
public UserIdSource getUserIdSource() {
    return new AuthenticationNameUserIdSource();
}

@Override
public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
    return new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
}


protected final FacebookConnectionFactory getFacebookConnectionFactory(Environment env) {
    String appId = env.getProperty(FACEBOOK_APP_ID);
    String appSecret = env.getProperty(FACEBOOK_APP_SECRET);

    if(appId == null || appId.isEmpty())
        throw new ApplicationPropertyException();

    if (appSecret == null || appSecret.isEmpty())
        throw new ApplicationPropertyException();

    return new FacebookConnectionFactory(appId, appSecret);
}


protected final TwitterConnectionFactory getTwitterConnectionFactory (Environment env){
    String consumerKey = env.getProperty(TWITTER_APP_ID);
    String consumerSecret = env.getProperty(TWITTER_APP_SECRET);

    if(consumerKey == null || consumerKey.isEmpty())
        throw new ApplicationPropertyException();

    if (consumerSecret == null || consumerSecret.isEmpty())
        throw new ApplicationPropertyException();

    return new TwitterConnectionFactory(consumerKey, consumerSecret);
}


protected DataSource getDataSource() {
    return dataSource;
}

}

的AppConfig

@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class AppConfig{

  public static void main(String[] args) {
        SpringApplication.run(AppConfig.class, args); 
   }
 }

1 个答案:

答案 0 :(得分:17)

Spring Boot的Spring Social Twitter和Spring Social Facebook的自动配置将根据application.properties中的配置自动添加连接工厂。这些自动配置的连接工厂与您尝试添加的工厂发生冲突。

您尝试添加的配置与Spring Boot为您创建的配置相同。我建议删除你的配置并让Boot做它自己的事情。更改SocialConfig以扩展SocialConfigurerAdapter而不是实施SocialConfigurer,然后删除您的addConnectionFactories方法。

相关问题