๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Spring @Configuration Annotation


Introduction

  • The @Configuration annotation is used to declare that the class serves as a Spring configuration class in which we can provide different configurations like bean configurations, database configurations, MVC configurations etc.

  • Using the @Configuration annotation, we create a Java-based configuration class, which serves as an alternative to XML-based configuration in Spring.

  • @Configuration annotation is used at class level in Spring. It cannot be used with methods or fields.
  • Syntax :
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AppConfig
    {
        /*
            Here we provide the different configurations for example....
            Bean Configurations, Database Configurations, MVC Configurations, Security Configurations etc
        */
    }

  • Click Here for @Configuration Annotation Program

Different Configurations within "@Configuration Class"

  • We can provide different types of configurations in Spring Configuration File which as as follows :-
    1. Bean Configurations
    2. Database Configurations
    3. MVC Configurations
    4. Security Configurations
      etc...

  • Basic definition and its syntax are as below :-

1. Bean Configuration:
  • One of the most common configurations that we provide in Java Configuration Class is Bean Configurations.
  • Within Spring Configuration Class annotated with @Configuration annotation, we can provide methods annotated with @Bean to define and configure beans.
  • Each @Bean method defines the instantiation, initialization, and dependency injection logic for a specific bean.
  • Syntax :
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class BeanConfig
    {
        // Below @Bean annotation is used to create Student class Bean
        @Bean
        public Student stdId() // Here method name is the bean-id / bean-name
        {
            // It will return the Student object
            return new Student();
        }
    }
  • NOTE : When we use @Bean annotated methods, @Configuration ensures that a single instance of each bean is created (singleton), even if methods are called multiple times within the class. This is managed internally by Spring through CGLIB proxying.
2. Database Configuration:
  • Database configurations are also one of the most common configurations when we use databases with spring application.
  • In this, we set up data sources, transaction management, and JPA/Hibernate configurations.
  • Syntax :
    //package and import statements
    
    @Configuration
    public class DatabaseConfig
    {
        @Bean
        public DataSource dataSource()
        {
            DriverManagerDataSource dataSource = new DriverManagerDataSource();
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            dataSource.setUrl("jdbc:mysql://localhost:3306/mydb");
            dataSource.setUsername("database-username");
            dataSource.setPassword("database-password");
            return dataSource;
        }
    }
  • NOTE : These types of configuration programs will be covered in Spring with JDBC (Database) Module.
3. MVC Configuration:
  • MVC Configurations sets-up the MVC-related configurations like view resolvers, message converters, CORS settings, etc
  • Syntax :
    //package and import statements
                                        
    @Configuration
    public class WebConfig implements WebMvcConfigurer
    {
        @Override
        public void addViewControllers(ViewControllerRegistry registry)
        {
            registry.addViewController("/home").setViewName("home");
        }
    
        @Override
        public void addCorsMappings(CorsRegistry registry)
        {
            registry.addMapping("/**").allowedOrigins("https://example.com");
        }
    }
  • NOTE : These types of configuration programs will be covered in Spring MVC Module.
4. Security Configuration:
  • Security Configurations configure the applications security, often using Spring Security.
  • Syntax :
    //package and import statements
                                        
    @Configuration
    @EnableWebSecurity
    public class SecurityConfig extends WebSecurityConfigurerAdapter
    {
        @Override
        protected void configure(HttpSecurity http) throws Exception
        {
            http
                .authorizeRequests()
                    .antMatchers("/public/**").permitAll()
                    .anyRequest().authenticated()
                .and()
                .formLogin();
        }
    }
  • NOTE : These types of configuration programs will be covered in Spring Security Module.