Wednesday, 17 January 2018

Spring Configuration Metadata (XML, Annotation and Java)

        Spring configuration metadata is to tell Spring container how to initiate, configure, wire and assemble the application specific objects. Spring provides three ways of configuration,

  • XML Based Configuration
  • Annotation Based Configuration
  • Java Based Configuration


XML Based Configuration

        All configurations are in one or multiple XML files.  This is the most complicated way of configuration. Large projects require tedious amount of XML which is difficult to manage.

see below examples,

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 introduces annotation-based configuration. We still have to write XML files but just to indicate "component-scan" on the packages of annotated classes. This is the preferred way of Spring Configuration.
           In this, we can enable autowire using <context:annotation-config /> in bean configuration. In bean declaration, you can use @Component and if you need to inject other bean then use @Autowired. Refer this for annotation 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, a pure-Java means of configuring container was provided. We don't need any XML with this method of configuration. Java Config provides a truly object-oriented mechanism for dependency injection, meaning we can take full advantage of re usability, inheritance and polymorphism in the configuration code.  Application developer has complete control over instantiation and dependency injection here.

You need to use two annotations @Configuration and @Bean to achieve this type of configuration.
It's same as Annotation based configuration, except XML file. In this type, XML configuration is not using instead using Java Class and annotating class with @Configuration.

See below Java config class,

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: