1. First import the required packages:
2. File structure:
3. Look at the XML configuration file first:
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" ><Beans> <!--let spring manage the creation and dependency of objects, which must be defined in the spring configuration file - <!--Configure the beans that need to be managed by spring (created and placed inside the spring IOC container) - <Beanname= "Hello"class= "Com.wangcf.HelloWorld"> <!--Configure the attributes that the bean needs to inject (it is injected by the Set method) - < Propertyname= "Name"value= "Direct"/> </Bean> <Beanname= "Userdao"class= "Com.wangcf.Dao.UserDao"></Bean> <Beanname= "Usermanager"class= "Com.wangcf.UserManager"> <!--injected with the value of DAO, value is injected constant value, ref injection reference value - < Propertyname= "Userdao"ref= "Userdao"></ Property> </Bean></Beans>
4.1 See HelloWorld section:
PackageCOM.WANGCF; Public classHelloWorld {PrivateString name; PublicHelloWorld () {System.out.println (The HelloWorld constructor starts to create ... "); } Public voidsetName (String name) {System.out.println ("SetName. Inject the value of name into .... "); This. Name =name; } Public voidShow () {System.out.println ("Holloworld show ..." +name); } }
Packagecom.wangcf.Test;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.wangcf.HelloWorld;Importjunit.framework.TestCase; Public classHolloworldtestextendstestcase{ Public voidtest1 () {//get spring's applicationcontext configuration file into the IOC containerBeanfactory factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); /**complete the above steps and you will print: The *helloworld constructor starts to create ... *setname. * Note that the properties of the class are injected from the beginning.*/ //get beans from the IOC container through the Beanfactory Getbean methodObject Obj=factory.getbean ("Hello"); System.out.println (Obj.getclass (). GetName ()); HelloWorld HW=(HelloWorld) obj; SYSTEM.OUT.PRINTLN (HW); Hw.show (); //get beans from the IOC container through the Beanfactory Getbean methodHelloWorld hw2= (HelloWorld) factory.getbean ("Hello"); System.out.println (HW2); }}
Print:
This shows that in implementing
Beanfactory factory=New classpathxmlapplicationcontext ("Applicationcontext.xml");
, the contents of the configuration file are executed.
5.1 See Usermanager section:
PackageCOM.WANGCF;ImportCom.wangcf.Dao.UserDao; Public classUsermanager {PrivateUserdao Userdao; //inject DAO with the Set method, which is not a constant value, but a reference value Public voidSetuserdao (Userdao Userdao) {System.out.println ("Usermanager Userdao Setuserdao. Inject DAO into the Usermanager container "); This. Userdao =Userdao; } Public voidAddUser () {System.out.println ("Usermanager AddUser"); Userdao.adduser (); } PublicUsermanager () {System.out.println ("Usermanager start creating ...."); }}
Package Com.wangcf.Dao; Public class Userdao { publicvoid AddUser () { System.out.println ("Userdao addUser" ); } Public Userdao () { System.out.println ("Userdao start creating ...");} }
Packagecom.wangcf.Test;Importjunit.framework.TestCase;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.wangcf.UserManager; Public classUsermanagertestextendstestcase{ Public voidTestadduser () {Beanfactory factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Usermanager Usermanager= (Usermanager) factory.getbean ("Usermanager"); System.out.println ("------------"); Usermanager.adduser (); }}
Print
Summary:
The properties of the Usermanager class and the HelloWorld class are injected through dependency.
The instantiation of an object is a dependency injection, so you don't have to use new one every time?
A supplement to spring's first example