Spring — Core Container | Code Factory

Spring Tutorial Index Page: Link

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Other… -> Java Project
  • Create IOC-CoreTest 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">
</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
  • In spring.xml if you open any tag and does not close it then you get parser exception. Example: open <bean> tag and don’t close it, now run Client.java
    — In console you can see org.xml.sax.SAXParseException exception
  • If you define any unformed tag then also you get parser exception. Example: lets define <codefactory></codefactory>, now run Client.java
    — You can open and close tag then still you will get org.xml.sax.SAXParseException exception because it is not defined in dtd file.
  • We not define any scope in bean spring.xml so it will create only single instance. BeanFactory will create instance on first request.

--

--

--

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

Recommended from Medium

Get a Sandbox, Domain Name, and TLS Cert for Testing K8s Apps in Under 5 Minutes

STACK in PYTHON(With Description)

pBFT— Understanding the Algorithm

Collecting multiple shield charges

3 Useful Things About Scala’s Either Class That You Want To Know

Metro Bike Sharing Exploratory Data Analysis

Ethereum Sharding Biweekly Development Update #6 - Prysmatic Labs

Unity C#: Static

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

Visitor Design Pattern in Java

Functional Interfaces in Java

Creational Design Patterns in Java

Communication Between Microservices | Spring WebClient