更新时间:2022-10-04 21:09:46
1、属性注入
package com.ioc.ch4_3_1; /** * Created by gao on 16-3-25. */ public class Car { private int maxSpeed; public String brand; private double price; public Car() { } public Car(int maxSpeed, String brand, double price) { this.maxSpeed = maxSpeed; this.brand = brand; this.price = price; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public void setBrand(String brand) { this.brand = brand; } public void setPrice(double price) { this.price = price; } public int getMaxSpeed() { return maxSpeed; } public String getBrand() { return brand; } public double getPrice() { return price; } public String toString() { return "brand:" + brand + "/maxSpeed:" + maxSpeed + "/price:" + price; } }
beans.xml
<?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.0.xsd"> <bean id="car" class="com.ioc.ch4_3_1.Car"> <property name="maxSpeed"><value>300</value></property> <property name="brand"><value>奥迪</value></property> <property name="price"><value>150000.00</value></property> </bean> </beans>
测试类:
package com.ioc.ch4_3_1; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by gao on 16-3-25. */ public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_1\\beans.xml"); Car car = (Car) ctx.getBean("car"); System.out.println(car.toString()); } }
package com.ioc.ch4_3_2; /** * Created by gao on 16-3-25. */ public class Car { private int maxSpeed; public String brand; private double price; private String corp; public Car() { } public Car(String brand, double price) { this.brand = brand; this.price = price; } public Car(int maxSpeed, String brand, double price) { this.maxSpeed = maxSpeed; this.brand = brand; this.price = price; } public Car(String brand, String corp, int maxSpeed) { this.brand = brand; this.corp = corp; this.maxSpeed = maxSpeed; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public void setBrand(String brand) { this.brand = brand; } public void setPrice(double price) { this.price = price; } public String getCorp() { return corp; } public void setCorp(String corp) { this.corp = corp; } public int getMaxSpeed() { return maxSpeed; } public String getBrand() { return brand; } public double getPrice() { return price; } public String toString() { return "brand:" + brand + "\tmaxSpeed:" + maxSpeed + "\tprice:" + price + "\tcorp:" + corp; } }
Boss类:
package com.ioc.ch4_3_2; public class Boss { private String name; private Car car; private Office office; public Boss(String name, Car car, Office office) { this.name = name; this.car = car; this.office = office; } public Boss(String name, Car car) { this.name = name; this.car = car; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public String toString(){ return "name:"+name+"\tcar:"+car.getBrand()+"\toffice:"+office; } }
Office类:
package com.ioc.ch4_3_2; public class Office { }
beans.xml:
<?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.0.xsd"> <!--构造函数注入:type --> <bean id="car1" class="com.ioc.ch4_3_2.Car"> <constructor-arg type="java.lang.String"> <value>car1_甲壳虫</value> </constructor-arg> <constructor-arg type="double"> <value>300000.0</value> </constructor-arg> </bean> <!--构造函数注入:index--> <bean id="car2" class="com.ioc.ch4_3_2.Car"> <constructor-arg index="0" value="300"/> <constructor-arg index="1" value="car2_宝马"/> <constructor-arg index="2" value="200000.0"/> </bean> <!--构造函数注入:type&index --> <bean id="car3" class="com.ioc.ch4_3_2.Car"> <constructor-arg index="0" type="java.lang.String"> <value>car3_红旗CA72</value> </constructor-arg> <constructor-arg index="1" type="java.lang.String"> <value>中国一汽</value> </constructor-arg> <constructor-arg index="2" type="int"> <value>200</value> </constructor-arg> </bean> <!--构造函数注入:自动识别入参类型 --> <bean id="boss1" class="com.ioc.ch4_3_2.Boss"> <constructor-arg> <value>John</value> </constructor-arg> <constructor-arg> <ref bean="car1" /> </constructor-arg> <constructor-arg> <ref bean="office" /> </constructor-arg> </bean> <bean id="car" class="com.ioc.ch4_3_2.Car"/> <bean id="office" class="com.ioc.ch4_3_2.Office" /> </beans>
测试类:
package com.ioc.ch4_3_2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by gao on 16-3-25. */ public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_2\\beans.xml"); Car car1 = (Car) ctx.getBean("car1"); System.out.println(car1.toString()); System.out.println("--------------------------------"); Car car2 = (Car) ctx.getBean("car2"); System.out.println(car2.toString()); System.out.println("--------------------------------"); Car car3 = (Car) ctx.getBean("car3"); System.out.println(car3.toString()); System.out.println("--------------------------------"); Boss boss1 = (Boss) ctx.getBean("boss1"); System.out.println(boss1.toString()); } }
package com.ioc.ch4_3_3; /** * Created by gao on 16-3-25. */ public class Car { private int maxSpeed; public String brand; private double price; public Car() { } public Car(int maxSpeed, String brand, double price) { this.maxSpeed = maxSpeed; this.brand = brand; this.price = price; } public void setMaxSpeed(int maxSpeed) { this.maxSpeed = maxSpeed; } public void setBrand(String brand) { this.brand = brand; } public void setPrice(double price) { this.price = price; } public int getMaxSpeed() { return maxSpeed; } public String getBrand() { return brand; } public double getPrice() { return price; } public String toString() { return "brand:" + brand + "\tmaxSpeed:" + maxSpeed + "\tprice:" + price; } }
CarFactory类:
package com.ioc.ch4_3_3; public class CarFactory { //创建Car的工厂方法 public Car createHongQiCar(){ Car car = new Car(); car.setBrand("car5_奔驰"); return car; } //工厂类方法是静态的 public static Car createCar(){ Car car = new Car(); return car; } }
beans.xml:
<?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.0.xsd"> <bean id="carFactory" class="com.ioc.ch4_3_3.CarFactory"/> <bean id="car5" factory-bean="carFactory" factory-method="createHongQiCar"/> <bean id="car6" class="com.ioc.ch4_3_3.CarFactory" factory-method="createCar"/> </beans>
测试类:
package com.ioc.ch4_3_3; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Created by gao on 16-3-25. */ public class Test { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com\\ioc\\ch4_3_3\\beans.xml"); Car car5 = (Car) ctx.getBean("car5"); System.out.println(car5.toString()); System.out.println("-----------------------"); Car car6 = (Car) ctx.getBean("car6"); System.out.println(car6.toString()); } }