Spring – J2EE Container | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create IOC-J2EETest project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
  • * You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)

spring.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
	"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
	<bean id="t" class="com.codeFactory.beans.Test" />
</beans>

Test.java

package com.codeFactory.beans;

/**
 * @author code.factory
 *
 */
public class Test {
	private Test() {
		System.out.println("Test.Test()");
	}
}

Client.java

package com.codeFactory.test;

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

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		/* load xml into container */
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		/*
		 * check xml document
		 * it will create instances to singleton beans
		 */
		
		/* create test class object */
		Object o1 = context.getBean("t");
		Object o2 = context.getBean("t");
		Object o3 = context.getBean("t");
		
		System.out.println(o1 == o2);
		System.out.println(o1 == o3);
	}
}

Output:

Dec 11, 2020 2:32:58 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Fri Dec 11 14:32:58 IST 2020]; root of context hierarchy
Dec 11, 2020 2:32:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/codeFactory/resources/spring.xml]
Test.Test()
true
true
  • If scope is singleton then ApplicationContext will create instance at the time of loading. So comment all code in Client.java except ApplicationContext declaration and run Client.java. Now in console you can see that Test.Test() output but we have not called .getBean() method. So what happned here?. Reason is we use ApplicationContext which will create instance while loading xml file.

Client.java

package com.codeFactory.test;

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

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) {
		/* load xml into container */
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
	}
}

Output:

Dec 11, 2020 2:55:31 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4bf558aa: startup date [Fri Dec 11 14:55:30 IST 2020]; root of context hierarchy
Dec 11, 2020 2:55:31 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/codeFactory/resources/spring.xml]
Test.Test()

  • Change scope from singleton to prototype and then run Client.java. It will not display anything in console because instance is not created.
  • Now uncomment commented code and run it and see console.
  • * If class constructor is private then still Spring will create object.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s