First step to use Spring in your Java application, is to create Spring configuration metadata, this metadata tells Spring what beans “Java classes” you need to be instantiated in Spring container. You have three options to write or code your configuration metadata “XML, annotation or Java configuration”
Objective
How to prepare Spring configuration metadata using XML, annotation or Java based configuration?
Environment & Tools:
Eclipse
Maven
Libraries:
Spring framework
List of jars
:
.classpath
List of dependencies
:
pom.xml
Here will see how to instantiate a bean “MyBean.java” using XML, annotation and Java configuration.
( 1 ) XML Configuration
This is the preferred way to configure Spring.
Beans:
These are the Java classes “beans” we want to create in Spring container.
src/main/java/com/hmkcode/spring/beans/
MyBean.java
package com.hmkcode.spring.beans;
public class MyBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean [name=" + name + "]";
}
}
src/main/java/com/hmkcode/spring/beans/
AnotherBean.java
package com.hmkcode.spring.beans;
public class AnotherBean {
private MyBean myBean;
public MyBean getMyBean() {
return myBean;
}
public void setMyBean(MyBean myBean) {
this.myBean = myBean;
}
@Override
public String toString() {
return "AnotherBean [myBean=" + myBean + "]";
}
}
Configuration:
src/main/resources/config/
XMLConfig.xml
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">
This equivalent to:
MyBean myBean = new MyBean();
myBean.setName("xmlBean");
AnotherBean anotherBean = new AnotherBean();
anotherBean.setMyBean(mybean);
Run:
package com.hmkcode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.beans.MyBean;
public class App
{
public static void main( String[] args )
{
ApplicationContext ctxXML = new ClassPathXmlApplicationContext("config/XMLConfig.xml");
AnotherBean anotherBean = (AnotherBean) ctxXML.getBean("anotherBean");
System.out.println( anotherBean);
}
}
Output
:
AnotherBean [myBean=MyBean [name=xmlBean]]
( 2 ) Annotation-Based Configuration
Beans + Annotation:
src/main/java/com/hmkcode/spring/beans/
MyBean.java
@Component
(value=
“myBean2″
) : this will enable Spring to detect this bean when scanning and instantiate a bean named “
myBean2
“
@Value
(value=
“AnnotationBean”
) : this equivalent to mybean2.setName(“AnnotationBean”);
package com.hmkcode.spring.beans;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value="myBean2")
public class MyBean {
private String name;
public String getName() {
return name;
}
@Value(value="AnnotationBean")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean [name=" + name + "]";
}
}
src/main/java/com/hmkcode/spring/beans/
AnotherBean.java
@Component
(value=
“anotherBean”
) : this will enable Spring to detect this bean when scanning and instantiate a bean named “anotherBean”
@Autowired
@Qalifier
(
“myBean2″
): this equivalent to anotherBean.setMyBean(mybean2)
package com.hmkcode.spring.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component(value="anotherBean")
public class AnotherBean {
private MyBean myBean;
public MyBean getMyBean() {
return myBean;
}
@Autowired
@Qualifier("myBean2")
public void setMyBean(MyBean myBean) {
this.myBean = myBean;
}
@Override
public String toString() {
return "AnotherBean [myBean=" + myBean + "]";
}
}
XML Configuration:
to enable annotation
We still need the XML just to enable annotation.
src/main/resources/config/
XMLConfig-Annotation.xml
Run:
package com.hmkcode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.hmkcode.spring.beans.AnotherBean;
public class App
{
public static void main( String[] args )
{
ApplicationContext ctxAnnotation = new ClassPathXmlApplicationContext("config/XMLConfig-Annotation.xml");
AnotherBean anotherBean = (AnotherBean) ctxAnnotation.getBean("anotherBean");
System.out.println( anotherBean);
}
}
Output:
AnotherBean [myBean=MyBean [name=AnnotationBean]]
( 3 ) Java-Based Configuration
Beans:
src/main/java/com/hmkcode/spring/beans/
MyBean.java
package com.hmkcode.spring.beans;
import org.springframework.beans.factory.annotation.Value;
public class MyBean {
private String name;
public String getName() {
return name;
}
@Value(value="JavaConfigBean")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "MyBean [name=" + name + "]";
}
}
src/main/java/com/hmkcode/spring/beans/
AnotherBean.java
package com.hmkcode.spring.beans;
import org.springframework.beans.factory.annotation.Autowired;
public class AnotherBean {
private MyBean myBean;
public MyBean getMyBean() {
return myBean;
}
@Autowired
public void setMyBean(MyBean myBean) {
this.myBean = myBean;
}
@Override
public String toString() {
return "AnotherBean [myBean=" + myBean + "]";
}
}
Cofiguration:
package com.hmkcode.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.beans.MyBean;
@Configuration
public class JavaConfig {
@Bean
public MyBean myBean(){
return new MyBean();
}
@Bean(name = "anotherBean2")
public AnotherBean anotherBean(){
return new AnotherBean();
}
}
Run:
package com.hmkcode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.hmkcode.spring.beans.AnotherBean;
import com.hmkcode.spring.config.JavaConfig;
public class App
{
public static void main( String[] args )
{
ApplicationContext ctxJavaConfig = new AnnotationConfigApplicationContext(JavaConfig.class);
AnotherBean anotherBean = (AnotherBean) ctxJavaConfig.getBean("anotherBean2");
System.out.println( anotherBean);
}
}
Output:
AnotherBean [myBean=MyBean [name=JavaConfigBean]]