Spring Interface Programming _ SetPoint injection and construction injection

Source: Internet
Author: User
Tags stub

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

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.