当前位置: 代码迷 >> java >> 提供商'twitter'的ConnectionFactory已经注册
  详细解决方案

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

热度:84   发布时间:2023-07-25 19:02:30.0

我正在覆盖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); 
   }
 }

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

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

  相关解决方案