How spring's IOC container loads initialize

Source: Internet
Author: User

Introduction

We know the core of spring in the IOC container, but if we are going to rely on the IOC container to manage our beans, then we need to tell IOC what beans it is easy for him to manage and what the beans are asking for, which is to tell spring through the configuration file IOC container. After our completion of these configuration files, if IOC is easy to implement the management of these beans, in addition to the location of resources there is an important step is to complete the IOC load initialization, that is, the configuration file loading process. The way to complete the IOC container load initialization is as long as there are three, the first is to load through file files, the second is through CLASSPATH (relative path) loading, there is a way to introduce through the web. Let's make a brief introduction to each other.

  

File load:

We'll start by writing a few simple test cases before we introduce a file-mode load.

A: Project Layout

    

B: Test the classes and interfaces that are used primarily:

Interface Hello.java

  

Package service; Public interface Hello {public    void SayHello ();    public void Saygoodbye ();    }

Interface implementation: Helloimpl.java

  

Package Service.impl;import Service. Hello; public class Helloimpl implements Hello {    @Override public    void SayHello () {        System.out.println ("This is From  the spring,i want to  say  hello!! ");    }    @Override public    void Saygoodbye () {        System.out.println ("This is from the  spring, I want to say goodbye!!");    }}

C:spring related profiles (Spring's default profile is in Applicationcontext.xml in the Web-inf folder)

  

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:tx= "Http://www.springframework.org/schema/tx"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-3.2.xsd Http://www.springframework.org/schema/context Http://www.springframework.org/schema/con Text/spring-context-3.2.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/ Mvc/spring-mvc.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-3.2.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-3.2.xsd ">    <BeanID= "Hello"class= "Service.impl.HelloImpl"/>    <BeanID= "Springutil"class= "service. Springutil "/></Beans>

D: Load via Filesystemxmlapplicationcontext

  

  /*filesystemxmlapplicationcontext Load Mode */       filesystemxmlapplicationcontext filesystemxmlapplicationcontext = new Filesystemxmlapplicationcontext ("Classpath:applicationContext.xml");        Hello Hello2 = (hello) context.getbean ("Hello");        Hello2.sayhello ();        Hello2.saygoodbye ();

E: Load by Filesystemresource mode

  

        /*filesystemresource Load Mode *        /beanfactory Factory2 = new Xmlbeanfactory (New Filesystemresource ("e:\\workspace\\ Intellijideaworkspace\\springfunctions\\manvenspringtest\\src\\main\\resources\\applicationcontext.xml "));        Hello  hello4= factory2.getbean ("Hello", hello.class);        Hello4.sayhello ();        Hello4.saygoodbye ();

F: (filesystemxmlapplicationcontext) Other instructions

1. Default to project work path that is the root directory of the project

ApplicationContext appCt2 = new Filesystemxmlapplicationcontext ("Src/main/resources/xxxxxx.xml");

2. Prefix classpath: Indicates the relative path of the project under Classpath

ApplicationContext appCt2 = new Filesystemxmlapplicationcontext ("Classpath:xxxxxml");

3. Use the prefix file to represent the absolute path of the files

ApplicationContext appCt2 = new Filesystemxmlapplicationcontext ("File:d:/xxxx.xml");
ApplicationContext appCt2 = new Filesystemxmlapplicationcontext ("D:/xxxx.xml");

4. Multiple files can be loaded at the same time

string[] XMLCFG = new string[] {"Src/main/resources/xxxxx.xml", "Classpath:YYYY.xml"};
ApplicationContext appCt2 = new Filesystemxmlapplicationcontext (XMLCFG);

5. Use the multibyte to download all documents that meet the requirements

ApplicationContext appCt2 = new Filesystemxmlapplicationcontext ("Classpath:*.xml");

Classpath Mode loading

A: The files used if the above is consistent

B: Load via Classpathxmlapplicationcontext

  

  /*classpathxmlapplicationcontext Load Mode */        classpathxmlapplicationcontext Context  = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");        Hello hello = (hello) context.getbean ("Hello");        Hello.sayhello ();        Hello.saygoodbye ();

C: Load by Classpathresource mode

  

   /*classpathresource Load Mode */      beanfactory factory = new Xmlbeanfactory (New Classpathresource (" Applicationcontext.xml "));        Hello  hello3= factory.getbean ("Hello", hello.class);        Hello3.sayhello ();        Hello3.saygoodbye ();

D: Other Notes

1. No prefix: Default to the project's Classpath under relative path

ApplicationContext appct = new Classpathxmlapplicationcontext ("Xxxx.xml");

2. Prefix classpath: Indicates the relative path of the project under Classpath

ApplicationContext appct = new Classpathxmlapplicationcontext ("Classpath:YYYY.xml");

3. Use the prefix file to represent the absolute path of the files

ApplicationContext appct = new Classpathxmlapplicationcontext ("File:d:/xxxx.xml");

4. Multiple files can be loaded at the same time

string[] XMLCFG = new string[] {"Classpath:XXX.xml", "Yyyy.xml"};
ApplicationContext appct = new Classpathxmlapplicationcontext (XMLCFG);

5. Use the multibyte to download all documents that meet the requirements

ApplicationContext appct = new Classpathxmlapplicationcontext ("*.xml");

Web Mode loading

A: This needs to be configured by Contextloaderlistener

  

   <!--  Specifies the directory where the spring bean's configuration file resides. Default configuration in Web-inf directory-    <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath*:/applicationContext.xml</param-value>    </context-param>    <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>    </listener>

B: This allows the IOC container to be loaded into the environment.

Execution results

Code: Testdemo1.java

  

Import Org.springframework.beans.factory.beanfactory;import Org.springframework.beans.factory.xml.XmlBeanFactory ; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.context.support.filesystemxmlapplicationcontext;import Org.springframework.core.io.classpathresource;import Org.springframework.core.io.filesystemresource;import Service. Hello;public class TestDemo1 {public static void main (String []args) {System.out.println ("Classpathxmlapplicati        Oncontext "); /*classpathxmlapplicationcontext Load Mode */classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext        ("Classpath:applicationContext.xml");        Hello hello = (hello) context.getbean ("Hello");        Hello.sayhello ();        Hello.saygoodbye ();        System.out.println ("Classpathresource"); /*classpathresource Load Mode */beanfactory factory = new Xmlbeanfactory (New Classpathresource ("Applicationcontext.xml"))        ; Hello hello3= FactorY.getbean ("Hello", hello.class);        Hello3.sayhello ();        Hello3.saygoodbye ();        System.out.println ("Filesystemxmlapplicationcontext"); /*filesystemxmlapplicationcontext Load Mode */Filesystemxmlapplicationcontext Filesystemxmlapplicationcontext = new FileS        Ystemxmlapplicationcontext ("Classpath:applicationContext.xml");        Hello Hello2 = (hello) filesystemxmlapplicationcontext.getbean ("Hello");        Hello2.sayhello ();        Hello2.saygoodbye ();        System.out.println ("Filesystemresource"); /*filesystemresource Load Mode */beanfactory Factory2 = new Xmlbeanfactory (New Filesystemresource ("E:\\wo Rkspace\\intellijideaworkspace\\springfunctions\\manvenspringtest\\src\\main\\resources\\        Applicationcontext.xml "));        Hello hello4= factory2.getbean ("Hello", hello.class);        Hello4.sayhello ();    Hello4.saygoodbye (); }}

  

Operation Result:

  

Other:

The test cases above are implemented using the most original, which is unprofessional and inefficient for the project. Spring provides its own support for unit testing, which we can use to greatly optimize our test cases, and summarize and explain the next article.

How spring's IOC container loads initialize

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.