Home » Spring » Spring Bean Reference Example Using Maven and Eclipse

Spring Bean Reference Example Using Maven and Eclipse

In this article we are going to develop Spring Bean Reference example by using eclipse and maven .


Tools and Technologies used in this article :

  • Spring 3.1
  • Maven
  • Java
  • Eclipse

1. Create a java project using maven’s archetype

In the command prompt execute the following command to generate Maven compatible Java project named as 'BeanReferenceExample '.

mvn archetype:generate -DgroupId=com.javawebtutor.spring -DartifactId=BeanReferenceExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Layout of the project’s directory

Spring Bean Reference  example in Eclipse


Maven will generate all the Java’s standard folders structure for you (besides resources folder, which you need to create it manually).

2. Convert to Eclipse project

To convert Maven project to support Eclipse IDE, in terminal, navigate to "BeanReferenceExample " project, and issue this command :

mvn eclipse:eclipse

This command generates those files required by the Eclipse IDE, in order for the project to be recognized as a valid Apache Maven project.

Now Import the project into Eclipse IDE.Project directory is given below.


Spring Bean Reference  example in Eclipse


3. Create a resources folder

Create a resources folder inside "src/main" folder. We will create Spring’s bean xml configuration file in that folder. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

4. Add Spring dependency

Add Spring dependency in Maven’s pom.xml file.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javawebtutor.spring</groupId>
	<artifactId>BeanReferenceExample</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>BeanReferenceExample</name>
	<url>http://maven.apache.org</url>
	<properties>
		<spring.version>3.0.5.RELEASE</spring.version>
	</properties>
	<dependencies>
		<!-- Spring 3 dependencies -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	</dependencies>
</project>

Issue "mvn eclipse:eclipse" again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse “.classpath” for dependency purpose.

In the following example, there is a Address bean to store address of a person and we will pass the reference of Address bean to Person bean using Spring IoC Setter Bean Ref Injection.

5. Write Spring bean

Create a normal Java class Employee.java at “src/main/java/com/javawebtutor/spring" package. This class is a normal java bean class.

Employee.java

package com.javawebtutor.spring;

public class Employee {
	private String name;
	private int age;
	private Address addrs;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Address getAddrs() {
		return addrs;
	}
	public void setAddrs(Address addrs) {
		this.addrs = addrs;
	}
}

Address.java

package com.javawebtutor.spring;

public class Address {
	private String doorNo;
	private String street;
	private String area;
	public String getDoorNo() {
		return doorNo;
	}
	public void setDoorNo(String doorNo) {
		this.doorNo = doorNo;
	}
	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public String getArea() {
		return area;
	}
	public void setArea(String area) {
		this.area = area;
	}
}

5. Create Spring bean configuration file

Create an xml file (spring-config.xml) at “src/main/resources/Spring-Module.xml“. This is the Spring’s bean configuration file, which declares all the available Spring beans.

spring-config.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-3.0.xsd">
        <bean id="employee" class="com.javawebtutor.spring.Employee">
                <property name="name" value="Ravi" />
                <property name="age" value="28" />
                <property name="address" ref="address" />
        </bean>
        <bean id="address" class="com.javawebtutor.spring.Address">
                <property name="doorNo" value="36B" />
                <property name="street" value="2nd Street" />
                <property name="area" value="Bharti Nager, OMR Road" />
        </bean>
</beans>

: Here “ref” attribute says to Spring that Spring has to create the instance of address bean and set to the property of employee bean.

Final Project Structure

Review it and make sure the folder structure as follows.


Spring Bean Reference  example in Eclipse


Let us create the Test class having the main() method. This class uses the ApplicationContext container provided by Spring.


6. Run It

Create a java class Test inside com.javawebtutor.spring package and add following code into this class.This class will load the Spring bean configuration file (spring-Module.xml) and retrieve the Spring bean via getBean() method.

Test.java

package com.javawebtutor.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String as[]) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
		Employee employee = (Employee)context.getBean("employee");
		Address address = (Address)context.getBean("address");
		System.out.println("Employee Name : " +employee.getName());
		System.out.println("Employee Age : " +employee.getAge());
		System.out.println("Addres :" +employee.getAddress().getDoorNo());
		System.out.println("--" + address.getStreet());
        System.out.println("--" + address.getArea());
	}
}

8. Output

Employee Name : Ravi
Employee Age : 28
Addres :36B
--2nd Street
--Bharti Nager, OMR Road

You can download the source code of the example by clicking on the Download link below.

Download


Previous Next Article
comments powered by Disqus