Control inversion and dependency injection of Spring framework

Source: Internet
Author: User

Learn the spring framework must understand control inversion and dependency injection. Here is an example to illustrate.

IOC (Inversion of Control): The application itself creates and maintains dependent objects, which are now created and maintained by external containers (Spring); the transfer of control;
is called control reversal.

First step: Configure Applicationcontextcreateproject.xml and Applicationcontext.xml (overall)

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd ">             <BeanID= "Create"class= "Createobject.createhelloworld"Factory-method= "Create">               </Bean>  </Beans>
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "><!--referencing other configuration files -    <ImportResource= "Createobject/applicationcontextcreateproject.xml"/></Beans>
 Package CreateObject;  Public class Createhelloworld {    // static factory public    static  Helloword Create () {                returnnew  Helloword ();    }}

 Package CreateObject;  Public class Helloword {    publicvoid  hellow () {        System.out.print ("Hellow Spring" );    }}

Test class:

 PackageCreateObject;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classtexthell{ Public Static voidMain (string[] args) {//TODO auto-generated Method Stub//Load configuration fileApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Helloword te= (Helloword) context.getbean ("Create");       Te.hellow (); }}

DI (Dependency Injection): (An implementation of control inversion) during a program's operation; dynamically injecting dependent objects into the component (creating objects) the work of new is given to spring; pre-reserved interface; set Get method

First Step: Configure Springapplicationcontext.xml

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-2.5.xsd "><BeanID= "Wanlix"class= "com. Test.test ">< Propertyname= "Name"value= "Zhangga"/>< Propertyname= "Speak"value= "If you don't hit the weasel one day,"/></Bean></Beans>

Step Two: Test the class

 Packagecom. Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTest { PublicString name;  PublicString speak;  PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getspeak () {returnspeak; }     Public voidSetspeak (String speak) { This. Speak =speak; }     Public Static voidMain (string[] args) {ApplicationContext context=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Test Te= (test) Context.getbean ("Wanlix"); System.out.print (Te.getname ()+ "SAY:" +te.getspeak ()); }}

Control inversion and dependency injection of Spring framework

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.