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

Spring Configurations


What is Spring Configurations?

  • In Spring; configurations refers to the "instructions" and "setup" that are provided to the Spring Container.
  • Some basic spring configurations are :-
    • What beans should be created (eg. service classes, repositories)
    • How beans should be wired or connected (dependency injection)
    • Scope of beans (eg. singleton, prototype)
    • Life cycle methods (initialization and destruction methods)
    • (and many more configurations which are explained below)
  • According to the provided configurations, Spring Container performs the particular task.

Important Configurations in Spring :

Below are some commonly used configurations in spring :-

  1. Bean Configurations : Bean Configurations defines the beans (objects) that Spring will manage, including their properties, dependencies and lifecycle methods. It specify how beans are instantiated and injected into each other.
  2. Autowiring Configurations : Autowiring Configurations manages dependency injection automatically by wiring beans based on type, name, or explicitly with annotations like @Autowired, @Qualifier, or @Inject.
  3. AOP Configurations : AOP Configurations configures aspects and advice for handling cross-cutting concerns such as logging, transactions, and security. This can include defining aspects, pointcuts, and advice types.
  4. Database Configurations : Database Configurations configures the data sources, connection pools and Hibernate/JPA settings for interacting with databases. This often includes setting up properties like database URL, username, password, and Hibernate dialect.
  5. Transaction Management Configurations : Transaction Management Configurations configures transactional behavior for methods interacting with databases, using @Transactional annotations or XML configurations. Specifies when transactions begin, commit, or roll back.
  6. MVC Configurations : MVC Configurations configures settings related to the Spring MVC framework, including view resolvers, handler mappings, interceptors, and static resource handling.
  7. Security Configurations : Security Configurations Configures application security settings, including authentication, authorization, and access control. Security configurations may specify user roles, authentication providers, and access restrictions.
  8. Profile Configurations : Profile Configurations allows configurations based on different environments (e.g. development, testing, production). By setting profiles, Spring loads specific beans or properties depending on the active environment.
  9. Caching Configurations : Caching Configurations enables caching in the application using @EnableCaching. This allows caching results of methods or components to improve performance.
  10. Scheduling Configurations : Configures tasks to run at scheduled intervals using @EnableScheduling and @Scheduled annotations.

These are only some common configurations that we provide in spring applications. As you keep reading further and creating more programs, you will understand these configurations more deeply...!!


How to Provide Configurations ?

Spring provides three main ways for configurations :-
   1. XML-based Configuration
   2. Java-based Configuration
   3. Annotations-based Configuration

  • 1. XML-based Configuration
    • This is an older approach where configurations are provided in XML files (typically applicationContext.xml).
    • For Example :
      <bean class="in.sp.beans.Student" id="stdId">
          <property name="name" value="Deepak" />
          <property name="rollno" value="101" />
          <property name="emailid" value="deepak@gmail.com" />
      </bean>
      In this configuration, we are creating one Student Bean Object and setting the values in properties.
    • Advantages: XML Based Configurations allows a clear separation of configuration from code, which is useful for complex applications or when working with legacy codebases.
    • Disadvantages: XML Based Configurations are very long and can get hard to manage as it grows. Also it doesn't support helpful code suggestions or catch errors easily in the IDE.
    • Click Here for Spring XML Based Configuration Program
  • 2. Java-based Configuration
    • This is somewhat new approach where configurations are provided in Java Class using @Configuration and @Bean annotations.
    • For Example :
      @Configuration
      public class AppConfig
      {
          @Bean
          public Student stdId()
          {
              return new Student("Deepak");
          }
      }
      
      In this configuration, we create creating one Student Bean Object and setting the values using constructor.
    • Advantages: Java Based Configurations provides Type safety, better readability and IDE support. It allows configuration to stay within the code, making it easier to understand for developers familiar with the Java language.
    • Disadvantages: Java Based Configurations can mix the configuration with business logic, potentially cluttering code.
    • Click Here for Spring Java Based Configuration Program
  • 3. Annotations-based Configuration
    • This is modern approach where configurations are provided directly on components and classes using annotations i.e. @Component, @Controller, @Service, @Repository etc
    • For Example :
      @Component
      public class Student
      {
          private String name = "Deepak";
      }
      
      //-----------
      
      @Autowired
      private Student student;
      
    • Advantages: Annotations Based Configurations requires minimal configurations, making it suitable for simpler applications or services. It promotes cleaner code by removing external configuration files.
    • Disadvantages: Annotations Based Configurations have limited flexibility for complex bean wiring. Annotations are less explicit, which may be confusing in large projects with numerous dependencies.
    • Click Here for Spring Annotations Based Configuration Program