Jan 11, 2021

2 min read

Spring — Core Container | Code Factory

Spring Tutorial Index Page: Link

Create Java Project

<!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">
</bean>
</beans>
package com.codeFactory.beans;/**
* @author code.factory
*
*/
public class Test {

public Test() {
System.out.println("Test.Test()");
}
}
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;
/**
* @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);
}
}
Dec 11, 2020 2:09:26 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