Getting Started

Spring is providing IOC container.

  1. Container creates object of a simple or complex java class and manages object life cycle.

  2. Objects are created and managed by spring IOC container.

  3. An object managed by container is called bean ( Java Bean ).

  4. IOC Container is the factory of all beans.

Lets configure and create instance of a simple User POJO class. POJO is stand for plain old java object. POJO contains private attributes and its setter and getter methods.

Lets follow given steps to create instance of User class by spring container.

Step 1.

Create Eclipse Maven java project

Click here to setup a maven java project.

Step 2:

Add following dependencies in pom.xml

<properties>

<springframework.version>5.1.4.RELEASE</springframework.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-core</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>${springframework.version}</version>

</dependency>

<dependency>

<groupId>javax.annotation</groupId>

<artifactId>javax.annotation-api</artifactId>

<version>1.3.2</version>

</dependency>

</dependencies>

  1. Dependency spring-core includes spring core jars. Spring core library jars contain org.springframework.beans and org.springframework.core packages. These packages contains BeanFactory and other core dependencies.

  2. Dependency spring-context includes jars for ApplicationContext IOC container.

See: pom.xml

Step 3

Create User.java POJO class that contains private attributes and their setter and getter methods.

public class User {

private String firstName = null;

private String lastName = null;

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

}

Step 4

Create applicationContext.xml file and configure bean

<bean id="userBean" class="com.sunilos.spring.bean.User">

<property name="firstName" value="SunilOS" />

</bean>

Step 5

Create Test program, instantiate container and get the User bean.

public class TestBeanFactory {

public static void main(String[] args) {

// Create container

BeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

// get User bean

// User bean = (User) context.getBean("userBean");

User bean = factory.getBean("userBean", User.class);

System.out.println("Bean contains " + factory.containsBean("userBean"));

System.out.println("Type of bean " + factory.getType("userBean"));

System.out.println("is Singlton " + factory.isSingleton("userBean"));

System.out.println(bean.getFirstName());

}

}

Congratulations your first bean managed by spring IOC container.

See: TestBeanFactory

FAQ

  1. Which maven dependencies are required for spring core?

  2. Why do you include jsr250-api dependency?

  3. What is the location of applicationContext.xml?

  4. Which aversion of Spring do you use ?

    1. Ans: Spring 5.0