[Spring entry] using constructors and setter injection

Source: Internet
Author: User
Tags cdata

Constructor Mode injection:

The main thing is to understand the difference in the order of the parameters (index) of the constructor method injection, the distinction of the type (type), and the mixture of the two, and the use of the value label and CDATA When the incoming content has a special symbol. To ensure the normal operation of the program!

/*** Created by Wangzh on 2016/4/26. * Description: Inject entities through the constructor; * Two constructors differ by type; * When you configure spring, you can use index or type to differentiate; The value of the special symbol, which can be combined with <! [cdata[<beijing> ^_^]]> to escape;*/ Public classCar {PrivateString brand; PrivateString City; Private DoublePrice ; Private intmaxspeed;  PublicCar (string brand, String city,DoublePrice ) {        Super();  This. Brand =brand;  This. City =City ;  This. Price =Price ; }     PublicCar (string brand, String city,intmaxspeed) {        Super();  This. Brand =brand;  This. City =City ;  This. Maxspeed =maxspeed; } @Override PublicString toString () {return"car{" + "brand=" "+ Brand +" \ "+", city= ' + city + ' \ ' + ", price=" + p Rice + ", maxspeed=" + Maxspeed + '} '; }}

Spring-context.xml

<?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.xsd" >    <BeanID= "HelloWorld"class= "Org.study.java.lessions.spring.HelloWorld"Destroy-method= "Destroymethod"Init-method= "Initmethod">        < Propertyname= "Name"value= "Spring"></ Property>    </Bean>    <!--Dependent Bean Injection -    <BeanID= "Menbean"class= "Org.study.java.lessions.spring.Men">        < Propertyname= "Person"ref= "Personbean"></ Property>    </Bean>    <!--attribute field Assignment Value -    <BeanID= "Personbean"class= "Org.study.java.lessions.spring.Person">        < Propertyname= "Name"value= "Hager"/>        < Propertyname= "Age"value= "$"/>        < Propertyname= "Sex"value= "Male"/>    </Bean>    <!--injected through the constructor, specifying the order of the parameters. But, can't solve the problem of overloading -    <!--<bean id= "car" class= "Org.study.java.lessions.spring.Car" > -    <!--<constructor-arg value= "BMW" index= "0"/> -    <!--<constructor-arg value= "Beijing" index= "1"/> -    <!--<constructor-arg value= "30.00" index= "2"/> -    <!--</bean> -    <!--Troubleshoot overloading problems: Using type types to differentiate -    <BeanID= "Car"class= "Org.study.java.lessions.spring.Car">        <Constructor-argvalue= "BMW"Index= "0"/>        <Constructor-argvalue= "Beijing"Index= "1"/>        <Constructor-argvalue= "30.00"Index= "2"type= "Double"/>    </Bean>    <!--resolve issues that include special symbols: combining CDATA Processing -    <BeanID= "Car2"class= "Org.study.java.lessions.spring.Car">        <Constructor-argIndex= "0"type= "Java.lang.String">            <value><! [Cdata[<b ' M ' W ' > ^_^]]></value>        </Constructor-arg>        <Constructor-argvalue= "Beijing"Index= "1"/>        <Constructor-argvalue= "30.00"Index= "2"/>    </Bean></Beans>

Test Case:

/*** Created by Wangzh on 2016/4/26. * Description:*/ Public classconstructortest {@Test Public voidconstruct_test () {ApplicationContext ATX=NewClasspathxmlapplicationcontext (Newstring[]{"Spring-context.xml"}); //method One: Get through the bynameCar car = (car) atx.getbean ("Car"));        System.out.println (Car.tostring ()); //method Two: Inject with bytype, but if there are multiple identical types, then it will fail. Because I can't tell which one to use//because I also declare an id = car2 But the type is still the bean of car.Car car2 = Atx.getbean (car.class);        System.out.println (Car2.tostring ()); Assert.assertnotnull ("True"); } @Test Public voidconstruct2_test () {ApplicationContext ATX=NewClasspathxmlapplicationcontext (Newstring[]{"Spring-context.xml"}); Car Car= (Car) atx.getbean ("Car2");        System.out.println (Car.tostring ()); Assert.assertnotnull ("True"); }}

Output Result:

# using the Bytype method, you will be prompted: not the only statement, found two bean.org.springframework.beans.factory.NoUniquebean
2car,car2# Normal results are as follows: Using CDATA Wrapping Results I' m initmethod.car{brand= ' <b ' m ' W ' > ^_^ ', city= ' Beijing ', price=30.0, maxspeed=0}

Setter Mode injection:

[Spring entry] using constructors and setter 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.