6. Spring Configuration Metadata

 

- The Spring IoC (Inversion of Control) container is responsible for managing and controlling the lifecycle of Java objects, also known as beans. 

- Configuration metadata is essential for the Spring IoC container to understand how to create, configure, and manage these beans. 


There are three main ways to provide this configuration metadata:


1. XML-based Configuration:

   - In XML-based configuration, you use XML files to define the configuration metadata for your Spring application.

   - The configuration typically includes information about beans, their dependencies, and how they should be wired together.

   - An example of XML-based configuration:


     

     <!-- applicationContext.xml -->

     <beans xmlns="http://www.springframework.org/schema/beans"

            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

            xsi:schemaLocation="http://www.springframework.org/schema/beans

                                http://www.springframework.org/schema/beans/spring-beans.xsd">


         <bean id="userService" class="com.example.UserService">

             <property name="VariableName" ref="reftoAnotherBean"/>

         </bean>


         <bean id="reftoAnotherBean" class="com.example.UserRepository"/>

     </beans>



2. Annotation-based Configuration:

   - In annotation-based configuration, you use Java annotations to provide the configuration metadata.

   - Annotations like `@Component`, `@Service`, `@Repository`, and `@Autowired` are commonly used.

   - Spring scans the classpath for annotated classes and automatically registers them as beans in the context.

   - Example of annotation-based configuration:


     

     // UserService.java

     @Service

     public class UserService {

         @Autowired

         private UserRepository userRepository;

         // ...

     }


     // UserRepository.java

     @Repository

     public class UserRepository {

         // ...

     }



3. Java-based Configuration:

   - In Java-based configuration, you use Java classes and methods to provide configuration metadata.

   - Typically, a configuration class is annotated with `@Configuration`, and methods within the class annotated with `@Bean` define beans.

- Using @Configuration annotation indicates that Spring IoC container can use it as a source of Beans definitions. 

- Using the @Bean tells Spring that method will return an object which should be registered as a bean in Spring application context.

   - Example of Java-based configuration:



     // AppConfig.java

     @Configuration

     public class AppConfig {

         @Bean

         public UserService userService() {

             UserService userService = new UserService();

             userService.setUserRepository(userRepository());

             return userService;

         }


         @Bean

         public UserRepository userRepository() {

             return new UserRepository();

         }

     }

     



   - In this example, the `@Configuration` annotation indicates that the class contains Spring configuration. The `@Bean` annotation on methods indicates that the methods will return a bean managed by the Spring IoC container.



Comments

Popular posts from this blog

11. Spring MVC @RequestMapping

2. Spring Framework Module (Architecture)

10. Spring - MVC Framework