How to expose a method as a bean to use the spring dependency injection

5

I need to expose a method as a bean in Spring% with% to use it in the injection of an attribute, which has more than one implementation. What I did was the following:

  • I added the method that will create my object using the ApplicationContext annotation:

    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public CsrfTokenRepository csrfTokenRepository() {
        return CookieCsrfTokenRepository.withHttpOnlyFalse();
    }
    
    [...]
    
    }
    
  • I added @Bean to inject the attribute that has the type of interface @Autowired that is returned by the method I exposed as Spring bean:

    public final class RestClient<T> {
    private Class<T> type;
    @Autowired
    private CsrfTokenRepository csrfTokenRepository;
    
    [...]
    
    }
    
  • When I try to use the attribute, NPE occurs because it was not injected by Spring:

    public final class RestClient<T> {
    private Class<T> type;
    @Autowired
    private CsrfTokenRepository csrfTokenRepository;
    
    public HttpHeaders csrfHeaders() throws IOException {
        CsrfToken csrfToken = csrfTokenRepository.generateToken(null); //Aqui ocorre o NPE!
        HttpHeaders headers = createHeaders();
        headers.add(csrfToken.getHeaderName(), csrfToken.getToken());
        headers.add("Cookie", "XSRF-TOKEN=" + csrfToken.getToken());
        return headers;
    }
    }
    
  •   

    java.lang.NullPointerException       at br.com.restclientjersey.RestClient.csrfHeaders (RestClient.java:61)       at br.com.restclientjersey.RestClient.postCall (RestClient.java:47)       at br.com.restclientjersey.RestClientTest.testPostCallStatus200 (RestClientTest.java:83)

    The attribute is still not injected. I even tried to add CsrfTokenRepository but it still does not work. What is missing to be able to inject this attribute by calling the Qualifier method?

        
    asked by anonymous 28.05.2018 / 17:12

    1 answer

    0

    Your error shows that the RestClient class was unable to get the Spring Bean to inject the dependency of csrfTokenRepository .

    Since you have not shown how you are using the RestClient class and it is not marked with any Spring stereotypes (% with%,% with% or% with%), then the problem may be in the way you are instantiating it .

    For Spring to do dependency injection of @Component , the class you are requesting this injection also needs to be controlled by Spring. That is, it also needs to be injected into the class in which it is being used.

    Imagining that you have a @Service class, this would be the correct way to use @Repository , injecting it with csrfTokenRepository :

    @Service
    class CrsftTokenService {
        @Autowired
        private RestClient restClient;
    
        public void call() {
            restClient.csrfHeaders();
        }
    }
    

    And this would be the incorrect form, the dependency injection will not work:

    @Service
    class CrsftTokenService {
        public void call() {
            RestClient restClient = new RestClient();
            restClient.csrfHeaders();
        }
    }
    

    And remembering again that your CrsftTokenService class must have some Spring stereotype to be injected into other classes.

        
    20.09.2018 / 13:26