Nishada Liyanage
Nishada Liyanage

Reputation: 69

Spring @configure annotation usage without bean definitions

I have some experience with spring dependency injection and transaction management but I am new to spring security. When i was reading an article related to spring security, I found that @Configuration annotation is used in an example but there were no bean definitions to be found.

According to my understanding, @Configuration annotation is used in classes which contain bean definitions. I need to know that what does the @Configuration annotation do in this example.

@Configuration
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

  @Autowired
  DataSource dataSource;

   ... // web stuff here

  @Override
  public configure(AuthenticationManagerBuilder builder) {
    builder.jdbcAuthentication().dataSource(dataSource).withUser("dave")
      .password("secret").roles("USER");
  }

}

Thank you

Upvotes: 6

Views: 3434

Answers (1)

hovanessyan
hovanessyan

Reputation: 31483

It's not mandatory to have Bean definitions in Spring managed classes.

In this case @Configuration (which wraps @Component) is used to indicate to Spring that this class should be instantiated and all it's dependencies should be injected - in this case that's DataSource and AuthenticationManagerBuilder. This is an example of Inversion of Control principle.

Spring also provides these ConfigurerAdapter hook points, where you can tweak the default configuration of an already instantiated component. This is exactly what is happening in your Configuration class.

Upvotes: 5

Related Questions