1. 程式人生 > >Spring入門學習(自動裝配) 第四節

Spring入門學習(自動裝配) 第四節

Spring入門學習 第四節

自動裝配

  1. 分別建立三個類Address.javaCar.javaPerson.java
    其中Perosn類包含有Address和Car的屬性
    package com.fafa.spring.autowire;
    public class Address {
    
    	private String city;
    	private String street;
    
    	public String getCity() {
    		return city;
    	}
    
    	public void setCity(String city)
    { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } @Override public String toString() { return "Address [city=" + city + ", street=" + street + "]"; } }
    package com.fafa.spring.autowire;
    
    public class
    Car { private String brand; private double price; public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString
    () { return "Car [brand=" + brand + ", price=" + price + "]"; } }
    package com.fafa.spring.autowire;
    
    public class Person {
    
    	private String name;
    
    	private Address address;
    
    	private Car car;
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public Address getAddress() {
    		return address;
    	}
    
    	public void setAddress(Address address) {
    		this.address = address;
    	}
    
    	public Car getCar() {
    		return car;
    	}
    
    	public void setCar(Car car) {
    		this.car = car;
    	}
    
    	@Override
    	public String toString() {
    		return "Person [name=" + name + ", address=" + address + ", car=" + car
    				+ "]";
    	}
    }
    
  2. 建立相應的bean配置檔案beans-autowire.xml
    <bean id="address" class="com.fafa.spring.autowire.Address" 
    	p:city="NanJing" p:street="QiXiaQu"></bean>
    	
    <!-- <bean id="address2" class="com.fafa.spring.autowire.Address" 
    	p:city="Dao" p:street="XuanWuQu"></bean> -->
    
    <bean id="car" class="com.fafa.spring.autowire.Car"
    	p:brand="Audi" p:price="300000"></bean>
    
    在之前是通過bean的引用來給屬性賦值,現在可以使用自動裝配的方式直接賦值。
    <!-- 通過ref引用來賦值的 -->
    <bean id="person" class="com.fafa.spring.autowire.Person"
    	p:name="Tom" p:address-ref="address" p:car-ref="car"></bean>
    
    Autowire可以byNamebyType進行自動裝配
    byName根據bean的名字和當前bean的setter風格的屬性名進行自動裝配,若有匹配的則進行自動裝配,若沒有匹配的則不裝配
    byType根據bean的型別和當前bean的屬性的型別進行自動裝配,若IOC容器中有一個以上的型別匹配的bean,則丟擲異常
    <!-- 
    	可以使用autowire 屬性指定自動裝配的方式,
    	byName根據bean的名字和當前bean的setter風格的屬性名進行自動裝配,若有匹配的則進行自動裝配,若沒有匹配的則不裝配
    	byType根據bean的型別和當前bean的屬性的型別進行自動裝配,若IOC容器中有一個以上的型別匹配的bean,則丟擲異常
    -->
    <bean id="person" class="com.fafa.spring.autowire.Person"
    	p:name="Tom" autowire="byType"></bean>
    
  3. 測試:
    測試方法:
    @Test
    public void testPerson(){
    	ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath*:beans-autowire.xml");
    	Person person = (Person) ctx.getBean("person");
    	System.out.println(person);
    }
    
    測試結果:
    Person [name=Tom, address=Address [city=NanJing, street=QiXiaQu], car=Car [brand=Audi, price=300000.0]]