本节书摘来自异步社区《Spring攻略(第2版)》一书中的第1章,第1.3节,作者: 【美】Gary Mak , Josh Long , Daniel Rubio著,更多章节内容可以访问云栖社区“异步社区”公众号查看
1.3.1 问题你想要调用构造程序在Spring IoC容器中创建一个Bean,这是创建Bean最常见和直接的方法。这和Java中使用new操作符创建对象相同。
1.3.2 解决方案通常,当你为一个Bean指定了class属性,就将要求Spring IoC容器调用构造程序创建Bean实例。
1.3.3 工作原理假定你打算开发一个在线销售产品的购物应用程序。首先,你创建一个Product类,这个类有多个属性,比如产品名称和价格。因为商店中有许多类型的产品,所以你定义Product类为抽象类,用于不同产品子类的扩展。
package com.apress.springrecipes.shop; public abstract class Product { private String name; private double price; public Product() {} public Product(String name, double price) { this.name = name; this.price = price; } // Getters and Setters ... public String toString() { return name + " " + price; } }然后你创建两个产品子类:Battery和Disc。每个类都有自己的属性。
package com.apress.springrecipes.shop; public class Battery extends Product { private boolean rechargeable; public Battery() { super(); } public Battery(String name, double price) { super(name, price); } // Getters and Setters ... } package com.apress.springrecipes.shop; public class Disc extends Product { private int capacity; public Disc() { super(); } public Disc(String name, double price) { super(name, price); } // Getters and Setters ... }为了在Spring IoC容器中定义一些产品,创建如下Bean配置文件:
<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.0.xsd"> <bean id="aaa" class="com.apress.springrecipes.shop.Battery"> <property name="name" value="AAA" /> <property name="price" value="2.5" /> <property name="rechargeable" value="true" /> </bean> <bean id="cdrw" class="com.apress.springrecipes.shop.Disc"> <property name="name" value="CD-RW" /> <property name="price" value="1.5" /> <property name="capacity" value="700" /> </bean> </beans>如果没有指定元素,将会调用默认的不带参数的构造程序。然后对于每个元素,Spring将通过设值方法注入值。前述的Bean配置等价于如下代码片段:
Product aaa = new Battery(); aaa.setName("AAA"); aaa.setPrice(2.5); aaa.setRechargeable(true); Product cdrw = new Disc(); cdrw.setName("CD-RW"); cdrw.setPrice(1.5); cdrw.setCapacity(700);相反,如果有一个或者多个元素,Spring将调用匹配参数的最合适的构造程序。
<beans ...> <bean id="aaa" class="com.apress.springrecipes.shop.Battery"> <constructor-arg value="AAA" /> <constructor-arg value="2.5" /> <property name="rechargeable" value="true" /> </bean> <bean id="cdrw" class="com.apress.springrecipes.shop.Disc"> <constructor-arg value="CD-RW" /> <constructor-arg value="1.5" /> <property name="capacity" value="700" /> </bean> </beans>因为Product类和子类在构造程序上没有歧义,前述的Bean配置等价于下面的代码片段:
Product aaa = new Battery("AAA", 2.5); aaa.setRechargeable(true); Product cdrw = new Disc("CD-RW", 1.5); cdrw.setCapacity(700); 你可以编写下面的Main类从Spring IoC容器读取产品进行测试: package com.apress.springrecipes.shop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Product aaa = (Product) context.getBean("aaa"); Product cdrw = (Product) context.getBean("cdrw"); System.out.println(aaa); System.out.println(cdrw); } } 相关资源:敏捷开发V1.0.pptx