标签:ret 依赖 create 属性 oid ice nbsp style contex
Car.class
public class Car { public String brand; private String corp; private double price; private int maxSpeed;
public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public int getMaxSpeed() { return maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } public String toString(){ return "brand:"+brand+"/maxSpeed:"+maxSpeed+"/price:"+price; } }
配置文件
<!-- 属性注入 -->
<!--bean id="car" class="com.smart.ditype.Car">
<property name="brand" value="红旗&CA72"/>
<property name="maxSpeed" value="200"/>
<property name="price" value="20000.00"/>
</bean>
public class DiTypeTest { public ApplicationContext factory = null; private static String[] CONFIG_FILES = { "com/smart/ditype/beans.xml" }; @BeforeClass public void setUp() throws Exception { factory = new ClassPathXmlApplicationContext(CONFIG_FILES); } @Test public void testCar(){ Car car = (Car)factory.getBean("car"); assertNotNull(car); System.out.println(car); } }
Car.class
public class Car { public String brand; private String corp; private double price; private int maxSpeed; public Car() {} public Car(String brand, double price) { this.brand = brand; this.price = price; } public Car(String brand, String corp, double price) { this.brand = brand; this.corp = corp; this.price = price; } public Car(String brand, String corp, int maxSpeed) { this.brand = brand; this.corp = corp; this.maxSpeed = maxSpeed; } }
配置文件:
<!--构造函数注入:type -->
<bean id="car1" class="com.smart.ditype.Car">
<constructor-arg type="java.lang.String">
<value>红旗CCCA72</value>
</constructor-arg>
<constructor-arg type="double">
<value>10000</value>
</constructor-arg>
</bean>
@Test public void testCar1(){ Car car1 = (Car)factory.getBean("car1"); assertNotNull(car1); System.out.println(car1); }
CarFactory.class
public class CarFactory { public Car createHongQiCar(){ Car car = new Car(); car.setBrand("红旗CA72"); return car; } public static Car createCar(){ Car car = new Car(); return car; } }
配置文件:
<!-- 工厂方法-->
<bean id="carFactory" class="com.smart.ditype.CarFactory" />
<bean id="car5" factory-bean="carFactory" factory-method="createHongQiCar">
</bean>
<bean id="car6" class="com.smart.ditype.CarFactory"
factory-method="createCar"></bean>
测试:
@Test public void testCar5(){ Car car5 = (Car)factory.getBean("car5"); assertNotNull(car5); System.out.println("car5:"+car5); } @Test public void testCar6(){ Car car6 = (Car)factory.getBean("car6"); assertNotNull(car6); System.out.println("car6:"+car6); }
标签:ret 依赖 create 属性 oid ice nbsp style contex
原文地址:http://www.cnblogs.com/lwx521/p/7774187.html