Spring — J2EE Container | Code Factory

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)
<!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>
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Test {
private Test() {
System.out.println("Test.Test()");
}
}
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);
}
}
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.
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");
}
}
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.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Terraform — Deploy AWS Inspector A cloud-based Vulnerability Assessment Tools

Top 10 websites for learning programming language

GSoC 2021 SCoRe Lab: Week 10

TRON SR RANKING June 4,2018

Should You Use DynamoDB or MongoDB?

Creating Android Archive (AAR) file

With #RPA the operating cost can be gradually reduced, permitting businesses to streamline revenue…

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Spring Cloud Stream Kafka errors to Dead Letter Queue (DLQ)

Visitor Design Pattern in Java

Functional Interfaces in Java

Reactive Programming : Project-Reactor / Rx-Java / Java 9 Reactive Flow