Spring IoC — 基于XML的配置
生活随笔
收集整理的這篇文章主要介紹了
Spring IoC — 基于XML的配置
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1、屬性注入
注意點: 1)如果類中顯示定義了一個帶參的構(gòu)造函數(shù),則一定還要顯示提供一個無參構(gòu)造函數(shù),否則使用屬性注入時將拋出異常。 2)JavaBean關(guān)于屬性命名的特殊規(guī)范。Spring只會檢查Bean中是否有對應(yīng)的Setter方法,至于Bean中是否有對應(yīng)的屬性變量則不做要求。如maxSpeed對應(yīng)setMaxSpeed(),brand對應(yīng)setBrand()。 所以<property>元素的屬性變量名應(yīng)滿足:xxx的屬性對應(yīng)setXxx()方法。變量的前兩個字母要么全部大寫,要么全部小寫。 Car類: 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/beanshttp://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());} } 輸出結(jié)果: brand:奧迪/maxSpeed:300/price:150000.0 2、構(gòu)造函數(shù)注入 注意點:循環(huán)依賴問題。Spring容器能順利實例化以構(gòu)造函數(shù)注入方式配置的Bean有一個前提是:Bean構(gòu)造函數(shù)入?yún)⒁玫膶ο蟊仨氁呀?jīng)準備就緒。如果兩個Bean都采用構(gòu)造函數(shù)注入,而且都通過構(gòu)造函數(shù)入?yún)⒁脤Ψ?#xff0c;則會發(fā)生循環(huán)依賴問題。這時可以將構(gòu)造函數(shù)注入方式調(diào)整為屬性注入方式。 Car類: 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/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd"><!--構(gòu)造函數(shù)注入: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><!--構(gòu)造函數(shù)注入: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><!--構(gòu)造函數(shù)注入: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><!--構(gòu)造函數(shù)注入:自動識別入?yún)㈩愋?--><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());} } 輸出結(jié)果: brand:car1_甲殼蟲 maxSpeed:0 price:300000.0 corp:null -------------------------------- brand:car2_寶馬 maxSpeed:300 price:200000.0 corp:null -------------------------------- brand:car3_紅旗CA72 maxSpeed:200 price:0.0 corp:中國一汽 -------------------------------- name:John car:car1_甲殼蟲 office:com.ioc.ch4_3_2.Office@9eed10a 3、工廠方法注入 有非靜態(tài)工廠方法和靜態(tài)工廠方法兩種方式 Car類: 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 {//創(chuàng)建Car的工廠方法public Car createHongQiCar(){Car car = new Car();car.setBrand("car5_奔馳");return car;}//工廠類方法是靜態(tài)的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/beanshttp://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());} } 輸出結(jié)果: brand:car5_奔馳 maxSpeed:0 price:0.0 ----------------------- brand:null maxSpeed:0 price:0.0?
注入方法的考量: 1)支持使用構(gòu)造函數(shù)注入的理由: · 構(gòu)造函數(shù)可以保證一些重要的屬性在Bean實例化時就設(shè)置好,避免了因為一些重要屬性沒有提供,導致一個無用Bean實例的情況; · 不需要為每個屬性提供Setter方法,減少了類的方法個數(shù);? ?? · 可以更好地封裝類變量,不需要為每個屬性指定setter方法,避免外部錯誤的調(diào)用。 2)更多的開發(fā)者更傾向于使用屬性注入方式,反對構(gòu)造函數(shù)注入的理由: · 如果一個類的屬性眾多,構(gòu)造函數(shù)的簽名將變成一個龐然大物,可讀性很差; · 靈活性不強,在有些屬性是可選的情況下,如果通過構(gòu)造函數(shù)注入,也需要為可選的參數(shù)提供一個null值; · 如果有多個構(gòu)造函數(shù),需要考慮配置文件和具體構(gòu)造函數(shù)匹配歧義的問題,配置上相對復(fù)雜; · 構(gòu)造函數(shù)不利于類的繼承和擴展,因為子類需要引用到父類復(fù)雜的構(gòu)造函數(shù); · 構(gòu)造函數(shù)注入有時會造成循環(huán)依賴的問題。 3)對于一個全新的應(yīng)用來說,不推薦使用工廠方法的注入方式。?
?
?
轉(zhuǎn)載于:https://www.cnblogs.com/yangyquin/p/5320866.html
總結(jié)
以上是生活随笔為你收集整理的Spring IoC — 基于XML的配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用transform和transiti
- 下一篇: 我的程序人生以及一些杂项