Spring — Hello World Example | Code Factory

Spring Tutorial Index Page: Link

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create HelloWorld 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
  • * 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 {
public Test() {
System.out.println("Test.Test()");
}
public void hello() {
System.out.println("Hello World...");
}
}
package com.codeFactory.test;import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.codeFactory.beans.Test;/**
* @author code.factory
*
*/
public class Client {
public static void main(String... args) {
/* find xml */
Resource r = new ClassPathResource("com/codeFactory/resources/spring.xml");

/* load xml into container */
BeanFactory factory = new XmlBeanFactory(r);

/* create test class object */
Object o1 = factory.getBean("t");
Object o2 = factory.getBean("t");
Object o3 = factory.getBean("t");

System.out.println(o1 == o2);
System.out.println(o1 == o3);

Test t = (Test) o1;
t.hello();
}
}
Dec 10, 2020 7:50:50 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
Hello World...
  • IOC container will create one singleton object. In above example you can see that instead of calling factory.getBean(“t”) multiple times still we have only single object.
Test.Test()
true
true
Hello World...
  • In <bean></bean> if we don’t sprcify anything then scope is singleton. If we define scope=”prototype” then it will create new object on every request.
Test.Test()
Test.Test()
Test.Test()
false
false
Hello World...

--

--

--

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

Recommended from Medium

Flutter: Don’t Fear the Garbage Collector

How I learned to love my job

Billy’s Coding Bootcamp Success Story

Python Virtual Environment for Data Science

Wordpress app deployment using AWS RDS with Terraform

How to Setup Git-LFS

Why being afraid of Algorithms and Data Structures sucks, and how to get over it

Snowflake Snowsight : A Hype or a real deal ?

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

Singleton Design Pattern in Java

Functional Interfaces in Java

Inheritance and Polymorphism

Comparison Inheritance And Polymorphism in JAVA