Quantcast
Channel: Java tutorial and Java Interview Questions and Answers
Viewing all articles
Browse latest Browse all 154

Spring Configuration Metadata (XML, Annotation and Java)

$
0
0
        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;

publicclassAddress {

private String address;

public String getAddress() {
return address;
}

publicvoidsetAddress(String address) {
this.address = address;
}

}

Employee.java,


package com.test;

publicclassEmployee{

private Address address;

public Address getAddress() {
return address;
}

publicvoidsetAddress(Address address) {
this.address = address;
}

}

Configuration file, beans.xml as follows,


<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="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">

<beanid="address"class="com.test.Address">
<propertyname="address"value="XYZZ"/>
</bean>

<beanid="employee"class="com.test.Employee">
<propertyname="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;
importorg.springframework.stereotype.Component;

@Component
publicclassAddress {

private String address;

public String getAddress() {
return address;
}

publicvoidsetAddress(String address) {
this.address = address;
}

}

Employee.java,


package com.test;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Component;

@Component
publicclassEmployee{

@Autowired
private Address address;

public Address getAddress() {
return address;
}

publicvoidsetAddress(Address address) {
this.address = address;
}

}

Configuration file(beans.xml),


<?xml version="1.0" encoding="UTF-8"?>

<beansxmlns ="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-scanbase-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;

importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;

importcom.test.Employee;

@Configuration
publicclassJavaConfig {

@Bean(name="employee")
public Employee getEmployee(){
returnnewEmployee();
}
}

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?

Viewing all articles
Browse latest Browse all 154

Trending Articles