5
I need to expose a method like a bean on ApplicationContext
of Spring 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 annotation
@Bean
:@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Bean public CsrfTokenRepository csrfTokenRepository() { return CookieCsrfTokenRepository.withHttpOnlyFalse(); } [...] }
I added the
@Autowired
to inject the attribute that has the interface typeCsrfTokenRepository
which is returned by the method I have expounded as Spring bean:public final class RestClient<T> { private Class<T> type; @Autowired private CsrfTokenRepository csrfTokenRepository; [...] }
When I try to use the attribute, it occurs NPE 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)
Still the attribute is not injected. I even tried to add a Qualifier
but it still doesn’t work. What remains to be done to inject this attribute by calling the method csrfTokenRepository
?
Some error or Exception?
– StatelessDev
Nullpointerexception when trying to use the injected attribute, I will add in question.
– Giuliana Bezerra
You have tried creating a separate class, using some bean annotation (@Repository etc.) to see if it works or not is an option?
– StatelessDev
The problem is replicating the implementation by adding another level of inheritance. That’s why I wanted something more elegant, like injecting a method as a constructor. So I avoid creating a "wrapper" class just for this.
– Giuliana Bezerra
Your class
CsrfTokenRepository
is annotated to be a bean (@Component, @Repository etc.) as well?– StatelessDev
This Csrftokenrepository is a Spring interface, so there it declares.
– Giuliana Bezerra
As the instance of
RestClient
is created? The packagebr.com.restclientjersey
is "scanned" by Spring? Note: there is no "method as bean", the bean is the return of the method, this is what is managed by Spring as bean.– Bruno César
I found the problem, @Brunocésar. What happened was that in the tests Restclient was created with new and not injected. When I injected him into the test he was able to carry the dependency! Thank you.
– Giuliana Bezerra
Cool, it was what I had imagined even when I questioned how the instance was created ;)
– Bruno César
@Brunocésar, as you helped, I suggest you include your answer (discovered in the comments) to the question :)
– Dherik