Spring Series "3" Application of constructor injection method for Bean injection

Source: Internet
Author: User

 Packagecn.com.xf; Public classUser {PrivateString name; Private intAge ; PrivateString Remark; //Omit Setter/getter Method@Override PublicString toString () {return"User [name=] + name +", age= "+ Age +", remark= "+Remark+ "]"; }}
 Packagecn.com.xf; Public classUserutil {Privateuser User;  PublicUser GetUser () {returnuser; }                //Note that this construction method     PublicUserutil (User u) { This. user=u; }         Public BooleanIsUser () {if(User! =NULL) {            return true; } Else {            return false; }    }}

The following is the spring configuration file

<?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-2.0.xsd">    <BeanID= "User"class= "Cn.com.xf.User">        < Propertyname= "Name"value= "Jikoy"></ Property>        < Propertyname= "Age"value= "+"></ Property>        < Propertyname= "Remark"value= "This is remark"></ Property>    </Bean>    <BeanID= "Userutil"class= "Cn.com.xf.UserUtil">        <Constructor-argref= "User"></Constructor-arg>    </Bean></Beans>

Above, I was not the same as the previous one, did not use the P tag, but the effect is the same, note <constructor-arg ref= "user" ></constructor-arg>

Package Cn.com.xf;import Org.springframework.context.configurableapplicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class Test {public    static void main ( String[] args) {        configurableapplicationcontext ctx=new classpathxmlapplicationcontext ("Applicationcontext.xml ");        User user= (user) Ctx.getbean ("user");        Ctx.close ();        SYSTEM.OUT.PRINTLN (user);}    }

The test class is still a sub-interface method that uses ApplicationContext and provides a close method.

Spring Series "3" Application of constructor injection method for Bean 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.