gpt4 book ai didi

java - 提供程序 'twitter' 的 ConnectionFactory 已注册

转载 作者:搜寻专家 更新时间:2023-11-01 04:01:49 27 4
gpt4 key购买 nike

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

我有一种感觉,这是因为与默认配置发生了一些冲突。代码如下,请问如何解决?

社交配置

@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;
}

应用配置

@EnableAutoConfiguration
@ComponentScan
@SpringBootApplication
public class AppConfig{

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

最佳答案

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

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

关于java - 提供程序 'twitter' 的 ConnectionFactory 已注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31733000/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com