An analysis of application of IOC container container in Java Spring Framework _java

Source: Internet
Author: User
Tags getmessage java spring framework

The spring container is the core of the spring framework. The container creates objects that are connected, configured, and managed throughout their lifecycle from creation to destruction. The spring container uses Dependency injection (DI) to manage the components that make up the application. These objects are called Spring Beans.
The container obtains which objects on it are instantiated, configured, and assembled by reading the description of the provided configuration metadata. Configuration metadata can be represented by Xml,java annotations or Java code. The following figure is a high-level diagram of how spring works. The Spring IOC container is a system or application that leverages Java's Pojo class and configures metadata to produce fully configured and executable.

Spring provides two different types of containers for beanfactory and ApplicationContext:

Beanfactory container
This is the simplest container that provides the basic support for DI and is defined by the Org.springframework.beans.factory.BeanFactory interface. Beanfactory or related interfaces, such as implementing Beanfactoryaware,initializingbean,disposablebean, still exist for spring backward compatibility with a large number of spring integration third-party frameworks.

There is a significant number of interfaces that provide the implementation of spring's Beanfactory interface, which is used as a direct access. The most commonly used beanfactory to achieve is xmlbeanfactoryclass. This container reads the configuration metadata from the XML file and uses it to create a fully configured system or application.

Beanfactory are usually the preferred resource, such as mobile devices or applets based applications that are limited. So use a applicationcontext unless you have a good reason not to do so.

For example:
Let's use the Eclipse IDE, and then follow these steps to create a spring application:

Here are the contents of the Helloworld.java file:

Package Com.yiibai;

public class HelloWorld {
 private String message;

 public void Setmessage (String message) {
 this.message = message;
 }

 public void GetMessage () {
 System.out.println (' Your message: ' + message);
 }
}

The following is the contents of the second file Mainapp.java:

Package Com.yiibai;

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 ();
 }



   

The following two key points need to be noted in the main program:

The first step is to create the factory object, we use the Framework API Xmlbeanfactory () to create the factory bean, and use the Classpathresource () API to load the bean configuration file that is available in classpath. The API requires Xmlbeanfactory () to create and initialize all of the objects. The Bean class mentioned in the configuration file.

The second step is to use the Getbean () method of the created Bean Factory object to obtain the required bean. This method returns with the Bean's ID and can eventually be constructed into a generic object of the actual object. Once you have an object, you can use this object to invoke any class method.

The following is the contents of the Bean configuration file Beans.xml

<?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.yiibai.HelloWorld" > <property name= "message
 " value= "Hello world!" />
 </bean>

</beans>

Once you have created the source and bean configuration files to complete, let's run the application. If everything goes well with your application, this will print the following information:

Your Message:hello world!


ApplicationContext Container
the application context (application) is a more advanced container for spring. With its beanfactory, the bean definition can be loaded and the bean allocated as required. In addition, it adds more enterprise-specific functionality, such as the ability to parse text messages from a property file, and to publish event listeners interested in application events. This container is defined by the Org.springframework.context.ApplicationContext interface.

ApplicationContext includes all the beanfactory features, so it is usually recommended in beanfactory. Beanfactory can still be used in lightweight applications, such as mobile devices or applications based on small applications.

The most common ApplicationContext implementations are:

Filesystemxmlapplicationcontext: This container loads the definition of a bean from an XML file. Here, you need to provide the full path to the XML bean configuration file in the constructor.

Classpathxmlapplicationcontext This container loads the definition of the bean from an XML file. Here, you do not need to provide the full path to the XML file, but you need to set the classpath correctly, because this container will look at the XML configuration file of the bean in Classpath.

Webxmlapplicationcontext: This container loads all the XML files defined by the bean from the Web application.

We've seen examples of classpathxmlapplicationcontext containers in spring's Hello World example, and we'll talk more about Xmlwebapplicationcontext in a separate chapter when We'll talk about the web based spring application. So let's see an example of Filesystemxmlapplicationcontext.

Example:
We use the Eclipse IDE, and then follow these steps to create a Spring application:

Here are the contents of the Helloworld.java file:

Package Com.yiibai;

public class HelloWorld {
 private String message;

 public void Setmessage (String message) {
 this.message = message;
 }

 public void GetMessage () {
 System.out.println (' Your message: ' + message);
 }
}

The following is the contents of the second file Mainapp.java:

Package Com.yiibai;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;

public class Mainapp {public
 static void Main (string[] args) {

 ApplicationContext context = new FILESYSTEMXMLAPPL Icationcontext
  ("C:/users/zara/workspace/hellospring/src/beans.xml");

 HelloWorld obj = (HelloWorld) context.getbean ("HelloWorld");
 Obj.getmessage ();
 }


The following two key points need to be noted in the main program:

The first step is to create a factory object that we use to create a factory bean after loading the bean configuration file from a given path, using the Filesystemxmlapplicationcontext of the framework API. The Filesystemxmlapplicationcontext () of the API requires the creation and initialization of all objects. The Bean class mentioned in the XML bean configuration file.

The second step is to use the Getbean () method of the created context to get the desired bean. This method returns with the Bean's ID and can eventually be constructed into a generic object of the actual object. Once we have the object, we can use this object to invoke any class method.

The following is the contents of the Bean configuration file Beans.xml

<?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.yiibai.HelloWorld" > <property name= "message
 " value= "Hello world!" />
 </bean>

</beans>

Once you have created the source code and the bean configuration file is complete, let's run the application. If all goes well, this will print the following information:

Your Message:hello world!

Summarize

The ApplicationContext container includes all the functions of the Beanfactory container, so it is usually recommended in beanfactory. Beanfactory can still be used in lightweight applications, such as mobile devices or small application based applications where the volume and speed of data is significant.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.