javaspring-bootspring-social

Inject Google and Facebook connection factory


I created SocialConfig which looks like:

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

Now I would like to inject fb and google connection factory in some services but I getting information that there is not beans. I also tried without @EnableSocial but not helping at all. My goal is to get information about user without set app id and secret in services. To be clear how I inject them:

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

but getting following error,

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

Action:

Consider defining a bean of type 'org.springframework.social.facebook.connect.FacebookConnectionFactory' in your configuration.


Solution

  • You did not create beans and you should create two beans like that and then Spring can inject these beans to application. Also, you can check spring documentation related with bean creation and configuration.

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