简体   繁体   中英

Application failed to start - Required a bean of type that could not be found

I just downloaded the project:

https://github.com/habuma/facebook-security5

I started the project and it works properly. The main functionality of this basic project is authenticate the user with Facebook and get his feed.

Inside this project there is the file:

https://github.com/habuma/facebook-security5/blob/master/src/main/java/sample/SocialConfig.java

which content is the following:

package sample;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.web.context.annotation.RequestScope;

import sample.api.facebook.Facebook;

@Configuration
public class SocialConfig {

    @Bean
    @RequestScope
    public Facebook facebook(OAuth2AuthorizedClientService clientService) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String accessToken = null;
        if (authentication.getClass().isAssignableFrom(OAuth2AuthenticationToken.class)) {
            OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;
            String clientRegistrationId = oauthToken.getAuthorizedClientRegistrationId();
            if (clientRegistrationId.equals("facebook")) {
                OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(clientRegistrationId, oauthToken.getName());
                accessToken = client.getAccessToken().getTokenValue();
            }
        }
        return new Facebook(accessToken);
    }

}

I noticed that there is no references to this class: SocialConfig .

My question is: why when I remove this no referenced class, when I try to run the application, I get the following error?

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in sample.HomeController required a bean of type 'sample.api.facebook.Facebook' that could not be found.


Action:

Consider defining a bean of type 'sample.api.facebook.Facebook' in your configuration.

What implications have the annotations: { @Configuration, @Bean, @RequestScope }

Thanks!

I think you need to read up on annotation-based configuration in Spring:

  • @Configuration annotates a Java class that defines Spring beans to be used by the application - sort of like an application context XML file, but in the form of a Java class.
  • @Bean annotates a method that defines a Spring bean to be used by the application. The value returned by the method will be the new Spring bean, and the method name will be used to derive the name for that bean.

So, when you remove that Java class, you remove the definition for a Spring bean used by the application, and so it fails to start with a "Missing bean" error.

@RequestScope annotation describes something about the lifecycle for the bean - I think this one means Spring will automatically create a new instance of this bean for each HTTP Request it handles.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM