May 14, 2021 Spring
This is the simplest container, and its main function is to support dependency injection (DI), which is defined in org.springframework.beans.factory.beanFactory. BeanFactory and related interfaces, such as BeanFactoryAware, DisposableBean, and InitializingBean, remain in Spring, primarily for backward compatibility with existing third-party frameworks that spring integrates with.
In Spring, there are a number of implementations of the BeanFactory interface. O f these, the XmlBeanFactory class is most commonly used. This container reads configuration metadata from an XML file to generate a configured system or application.
BeanFactory is preferred for resource-rich mobile devices or applet-based applications. Otherwise, applicationContext is generally used unless you have a better reason to choose BeanFactory.
Let's say we've installed eclipse IDE, and following the steps below, we can create a Spring application.
Steps | Describe |
---|---|
1 | Create a project called SpringExample and create a new one under the src folder called com.tutorialspoint. |
2 | Right-click to select the Add External JARs option to import Spring's library files, as we mentioned in the Spring Hello World Example section. |
3 | Create helloworld files and MainApp files .java the com.tutorialspoint .java and MainApp. |
4 | Create beans, the profile of the bean under the src .xml |
5 | The final step is to create the contents of all Java files and bean profiles and run the application as follows. |
Here's what the .java HelloWorld does:
package com.tutorialspoint;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message = message;
}
public void getMessage(){
System.out.println("Your Message : " + message);
}
}
Here's what the .java MainApp reads:
package com.tutorialspoint;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class MainApp {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory
(new ClassPathResource("Beans.xml"));
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();
}
}
In the main program, we need to pay attention to the following two points:
The first step is to use the XmlBeanFactory() API provided by the framework to generate factory beans and the ClassPathResource() API to load the bean profiles available under path CLASSPATH. ry() The API is responsible for creating and initializing all objects, that is, the beans mentioned in the configuration file.
Here's what'.xml in the profile Beans:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="helloWorld" class="com.tutorialspoint.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
</beans>
If you've finished the above, next, let's run this application. If the program is error-correct, you will see the following information from the console:
Your Message : Hello World!