Spring strategy learning notes (1.03) -- call constructor to create Bean

Source: Internet
Author: User

Generally, If you specify the class attribute for a bean, the Spring IoC container is required to call the constructor to create a bean.

As follows, there is a product class, which has two sub-classes: Battery and disc.

Product class:

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc. shop;/*** title: product. java * product abstract class ** @ author Jackie * @ since APR 14,201 3 8:09:49 * @ version V1.0 */public abstract class product {private string name; private double price; Public Product () {}/*** <p> title: </P> * <p> constructor with parameters </P> * @ Param name * @ Param price */Public Product (string name, double price) {This. name = Name; this. price = price;}/*** @ return the name */Public String getname () {return name ;} /*** @ Param name the name to set */Public void setname (string name) {This. name = Name;}/*** @ return the price */Public double getprice () {return price ;} /*** @ Param price the price to set */Public void setprice (double price) {This. price = price;} Public String tostring () {return name + "" + price ;}}

Battery class:

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc. shop;/*** title: Battery. java class function description ** @ author Jackie * @ since APR 14,201 3 8:14:26 * @ version V1.0 */public class battery extends product {private Boolean rechargeable; Public battery () {super ();} public battery (string name, double price) {super (name, price);}/*** @ return the rechargeable */Public Boolean isrechargeable () {return rechargeable;}/*** @ Param rechargeable * The Rechargeable to set */Public void setrechargeable (Boolean rechargeable) {This. rechargeable = rechargeable ;}}

Disc class:

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc. shop;/*** title: disc. java class function description ** @ author Jackie * @ since APR 14,201 3 8:18:30 * @ version V1.0 */public class disc extends product {private int capacity; Public disc () {super ();} public disc (string name, double price) {super (name, price);}/*** @ return the capacity */Public int getcapacity () {return capacity;}/*** @ Param capacity * the capacity to set */Public void setcapacity (INT capacity) {This. capacity = capacity ;}}

Bean configuration file:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="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.2.xsd">    <bean id="aaa" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Battery"><property name="name" value="AAA" /><property name="price" value="2.5" /><property name="rechargeable" value="true" />    </bean>    <bean id="cdrw" class="com.jackie.codeproject.springrecipesnote.springioc.shop.Disc"><property name="name" value="CD-RW" /><property name="price" value="1.5" /><property name="capacity" value="700" />    </bean></beans>

If the <constructor-Arg> element is not specified, the default constructor without parameters is called. For each <property> element, spring injects the value through the setter method.The bean configuration above is equivalent:

      Product aaa = new Battery();      aaa.setName("AAA");      aaa.setPrice(2.5);      aaa.setRechargeable(true);      Product aaa = new Battery();      aaa.setName("AAA");      aaa.setPrice(2.5);      aaa.setCapacity(700);

On the contrary, if one or more <constructor-Arg> elements exist, spring will call the most appropriate constructor that matches the parameters.

The following are test functions:

/** Copyright 2013-2015 */package COM. jackie. codeproject. springrecipesnote. springioc. shop; import Org. JUnit. after; import Org. JUnit. test; import Org. springframework. context. applicationcontext; import Org. springframework. context. support. classpathxmlapplicationcontext;/*** title: producttest. java class function description test class ** @ author Jackie * @ since APR 14,201 3 8:31:45 * @ version V1.0 */public class producttest { Private applicationcontext; @ test public void testproduct () {applicationcontext = new classpathxmlapplicationcontext ("applicationcontext. XML "); product AAA = (product) applicationcontext. getbean ("AAA"); product cdrw = (product) applicationcontext. getbean ("cdrw"); system. out. println (AAA); system. out. println (cdrw) ;}@ after public void after () {If (null! = Applicationcontext) {applicationcontext = NULL ;}}}

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.