Description
Usermanagerimp is the set value injection, USERMANAGERIMP2 is the construction injection
Interface is not injected, that is, there is no interface in the spring configuration file, but the <BEAN> is defined by the interface
Private Iuserdao Userdao;
Iusermanager usermanager2= (Iusermanager) Factory.getbean ("UserManager2");
Classes are injected, ①usermanagerimp and USERMANAGERIMP2 belong to the implementation class
②userdaoimp is the DAO class also to inject
The former ① attribute Userdao,userdao can be injected by setting value and constructing two methods.
The latter ② has no attributes, so it only writes <bean>
1. Documents
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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 "> <Beanname= "Usermanager"class= "COM.WANGCF.MANAGER.USERMANAGERIMP"> <!--Set Value injection - < Propertyname= "Userdao"ref= "Userdao"></ Property> </Bean> <Beanname= "UserManager2"class= "COM.WANGCF.MANAGER.USERMANAGERIMP2"> <!--Construction Injection - <Constructor-argref= "Userdao"/> </Bean> <Beanname= "Userdao"class= "COM.WANGCF.DAO.USERDAOIMP"></Bean> </Beans>
2.Dao
Package Com.wangcf.Dao; Public Interface Iuserdao { publicvoid addUser ();}
Package Com.wangcf.Dao; Public class Implements iuserdao{ publicvoid AddUser () { System.out.println (" Userdaoimp addUser ... "); }
3.manager
Package Com.wangcf.manager; Public Interface Iusermanager { publicvoid addUser ();}
PackageCom.wangcf.manager;ImportCom.wangcf.Dao.IUserDao; Public classUsermanagerimpImplementsiusermanager{PrivateIuserdao Userdao; //Set Value injection Public voidSetuserdao (Iuserdao Userdao) { This. Userdao =Userdao; } Public voidAddUser () {System.out.println ("Usermanagerimp addUser ..."); This. Userdao.adduser (); }}
PackageCom.wangcf.manager;ImportCom.wangcf.Dao.IUserDao; Public classUserManagerImp2Implementsiusermanager{PrivateIuserdao Userdao; //Construction Injection PublicUserManagerImp2 (Iuserdao Userdao) {//TODO auto-generated Constructor stub This. userdao=Userdao; } @Override Public voidAddUser () {//TODO auto-generated Method StubSystem.out.println ("UserManagerImp2 addUser ..."); This. Userdao.adduser (); } }
4.test
Packagecom.wangcf.test;Importorg.springframework.beans.factory.BeanFactory;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;ImportCom.wangcf.manager.IUserManager;Importjunit.framework.TestCase; Public classUsermanagertestextendstestcase{ Public voidTestadduser () {Beanfactory factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Iusermanager Usermanager= (Iusermanager) factory.getbean ("Usermanager"); Usermanager.adduser (); System.out.println ("============="); Iusermanager UserManager2= (Iusermanager) factory.getbean ("UserManager2"); Usermanager2.adduser (); }}
5. Output
Spring Interface Programming _ SetPoint injection and construction injection