Walid
Walid

Reputation: 142

Spring security 4 - configure(AuthenticationManagerBuilder auth) works without @Autowired

When configuring Spring Security :

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    DataSource dataSource;
    //@Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

when running, with or without @Autowired, it works. where AuthenticationManagerBuilder come from if it's not Autowired ?

Upvotes: 0

Views: 156

Answers (2)

CryptoFool
CryptoFool

Reputation: 23119

There is no "injection" going on there. 'configure' is just a method that takes a AuthenticationManagerBuilder object.

Your SecurityConfig object implements WebSecurityConfigurerAdapter, and is a Spring Bean because of the annotations on it. You also enable security behavior via an annotation. All of this will cause Spring to be looking for beans of type WebSecurityConfigurerAdapter to serve a purpose in the set up of security. It finds your bean because it is one of these objects. Spring knows what this type of bean is supposed to do, so it just calls the appropriate methods on that bean.

Because you have overloaded one of the methods of WebSecurityConfigurerAdapter, your version of that method will be called.

@Autowired is only for member variables that reference beans.

Upvotes: 1

vavasthi
vavasthi

Reputation: 952

It is injected automatically in the method by the caller. Anyway @Autowiring has no role to play in a method argument.

Upvotes: 0

Related Questions