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