Wednesday, 17 January 2018

Spring Configuration Metadata: XML, Annotation, and Java-Based Configuration

        Spring configuration metadata tells the Spring container how to create, configure, wire, and assemble application objects. It provides the information required by the Spring IoC container to initialize and manage Spring beans.

Spring supports the following three types of configuration:

  • XML-based Configuration

  • Annotation-based Configuration

  • Java-based Configuration



XML Based Configuration

All configurations are defined in one or more XML files. This was the traditional way of configuring Spring applications. However, in large projects, maintaining a large amount of XML configuration can become tedious and difficult to manage.

See the example below:

Address.java

package com.test;
 
public class Address {
 
    private String address;
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
}

Employee.java,
package com.test;
 
public class Employee{
 
    private Address address;
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
}

Configuration file, beans.xml as follows,
<?xml version="1.0" encoding="UTF-8"?>
<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="address" class="com.test.Address">
        <property name="address" value="XYZZ" />
    </bean>
 
    <bean id="employee" class="com.test.Employee">
         <property name="address" ref="address" />
    </bean>
</beans>



Annotation Based Configuration

           Spring 2.5 introduced annotation-based configuration. With this approach, we still need to define some XML configuration, mainly to enable component scanning for packages containing annotated classes. However, most of the bean configuration is handled using annotations, making it easier to maintain and manage.

In this approach, we can enable annotation-based dependency injection using <context:annotation-config /> in the Spring configuration file. We can use the @Component annotation to declare a class as a Spring bean, and the @Autowired annotation to inject dependencies into the bean.

Refer to this article for more details about Spring Annotations.

Address.java

package com.test;
import org.springframework.stereotype.Component;

@Component
public class Address {
 
    private String address;
 
    public String getAddress() {
        return address;
    }
 
    public void setAddress(String address) {
        this.address = address;
    }
 
}

Employee.java,
package com.test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component 
public class Employee{
 
    @Autowired
    private Address address;
 
    public Address getAddress() {
        return address;
    }
 
    public void setAddress(Address address) {
        this.address = address;
    }
 
}

Configuration file(beans.xml),

<?xml version="1.0" encoding="UTF-8"?> 
 
<beans xmlns = "http://www.springframework.org/schema/beans"      
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      
        xmlns:context="http://www.springframework.org/schema/context"    
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
      
     <context:component-scan base-package="com.test"/>
      
     <context:annotation-config> -->

</beans>



Java Based Configuration 

          Starting with Spring 3.0, Spring introduced a pure Java-based approach for configuring the application context. With this approach, we do not need XML configuration files. Java-based configuration provides a fully object-oriented mechanism for dependency injection, allowing developers to take advantage of features such as reusability, inheritance, and polymorphism while defining the configuration.

The application developer has complete control over bean creation and dependency injection using Java configuration.

To achieve this type of configuration, we mainly use two annotations:

  • @Configuration

  • @Bean

Java-based configuration is similar to annotation-based configuration, except that it does not rely on XML configuration files. Instead, we create Java classes and annotate them with @Configuration to define the Spring beans and their dependencies.

See the Java configuration class below:

package com.test;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
import com.test.Employee;
 
@Configuration
public class JavaConfig {
 
    @Bean(name="employee")
    public Employee getEmployee(){
        return new Employee();
    }
}

Thank you for visiting blog.

Related Posts:--
1) Spring MVC with Hibernate CRUD Example
2) What is IOC Container in Spring? Difference between BeanFactory and ApplicationContext
3) Spring Annotations and its usage
4) What is Autowiring in Spring ? Explain Autowiring modes and limitations with examples
5) Spring @Qualifier Annotation with example
6) What are different Spring Bean Scopes?

1 comment: