Spring Configuration (XML, Annotation & Java)

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”

springsource

Objective

How to prepare Spring configuration metadata using XML, annotation or Java based configuration?

Environment & Tools:

  • Eclipse
  • Maven

Libraries:

  • Spring framework

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
<?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="myBean" class="com.hmkcode.spring.beans.MyBean">
		<property name="name" value="xmlBean" />
	</bean>

	<bean id="anotherBean" class="com.hmkcode.spring.beans.AnotherBean">
                <property name="myBean" ref="myBean" />
        </bean>
</beans>

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
<?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.hmkcode.spring.beans"/>
      <!-- The use of <context:component-scan> implicitly enables the functionality of <context:annotation-config> --> 
</beans>

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]]

2 thoughts on “Spring Configuration (XML, Annotation & Java)

  1. Srishti Kapoor

    Hello,

    I am B.Tech student and looking for Advance level of java i.e spring struts hibernate and many more.
    So Wanna learn these from basics to make a powerful value in IT world.
    Please suggest me some platforms to learn these no only on theoretical basics but on practicals too.
    After spending much time and analysis I got one but don’t know are they offering me good stuff at that price.
    I am attaching the link for that too as to get the proper idea or any other platform.
    http://www.wiziq.com/course/25894-the-6-week-complete-java-primer

    Thanks in advance

    Regards,
    Srishti

    1. hani.hmk Post author

      Hi,

      Thank you for visiting my blog.

      I suggest to visit mkyong.com, it is a good place to start in the tech you mentioned.

      Always remember “Code First!” :)

Comments are closed.