注入 Google 和 Facebook 连接工厂

Inject Google and Facebook connection factory

我创建的 SocialConfig 看起来像:

@Configuration
@EnableSocial
public class SocialConfig {

  //enviroments

    @Bean
    public ConnectionFactoryLocator connectionFactoryLocator() {
        ConnectionFactoryRegistry connectionFactoryRegistry = new ConnectionFactoryRegistry();
        connectionFactoryRegistry.addConnectionFactory(new FacebookConnectionFactory(facebookAppId, facebookAppSecret));
        connectionFactoryRegistry.addConnectionFactory(new GoogleConnectionFactory(googleAppId, googleAppSecret));
        return connectionFactoryRegistry;
    }
}

现在我想在某些服务中注入 fb 和 google 连接工厂,但我得到的信息是没有 bean。我也试过没有 @EnableSocial 但根本没有帮助。我的目标是获取有关用户的信息,而无需在服务中设置应用程序 ID 和密码。 要清楚我是如何注入它们的:

   private FacebookConnectionFactory facebookConnectionFactory;
    private GoogleConnectionFactory googleConnectionFactory;
    private UserRepository userRepository;

    public SocialServiceImpl(FacebookConnectionFactory facebookConnectionFactory, GoogleConnectionFactory googleConnectionFactory, UserRepository userRepository) {
        this.facebookConnectionFactory = facebookConnectionFactory;
        this.googleConnectionFactory = googleConnectionFactory;
        this.userRepository = userRepository;
    }

但出现以下错误,

Parameter 0 of constructor in ...SocialServiceImpl required a bean of type '>org.springframework.social.facebook.connect.FacebookConnectionFactory' that could not be found.

操作:

考虑在您的配置中定义类型为 'org.springframework.social.facebook.connect.FacebookConnectionFactory' 的 bean。

您没有创建 bean,您应该像那样创建两个 bean,然后 Spring 可以将这些 bean 注入应用程序。此外,您可以查看与 bean 创建和配置相关的 spring documentation

@Bean
public FacebookConnectionFactory facebookConnectionFactory() {
    return new FacebookConnectionFactory(facebookAppId, facebookAppSecret);
}

@Bean
public GoogleConnectionFactory googleConnectionFactory() {
    return new GoogleConnectionFactory(googleAppId, googleAppSecret);
}

@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
    ConnectionFactoryRegistry connectionFactoryRegistry = new ConnectionFactoryRegistry();
    connectionFactoryRegistry.addConnectionFactory(facebookConnectionFactory());
    connectionFactoryRegistry.addConnectionFactory(googleConnectionFactory());
    return connectionFactoryRegistry;
}