标签:
Beans-autowire.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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.hy.spring.beans.autowire.Address"
p:city="BeiJing" p:street="HuiLongGun">
</bean>
<bean id="car" class="com.hy.spring.beans.autowire.Car"
p:brand="AuDi" p:price="300000">
</bean>
<bean id="person" class="com.hy.spring.beans.autowire.Person"
p:name="Tom" p:address-ref="address" p:car-ref="car">
</bean>
<!-- 可以使用 autowire 属性指定自动装配的方式。
byName: 是根据bean 的名字和当前bean 的setter 风格的属性名进行自动装配,
若有匹配的,则进行自动装配,若没有匹配的,则不装配。
byType: 是根据bean 的类型和当前bean 的属性类型进行自动装配.
若IOC 容器中有1个以上的类型匹配的bean,则抛异常。
-->
<!--
<bean id="person" class="com.hy.spring.beans.autowire.Person"
p:name="Tom" autowire="byName">
</bean>
-->
<!--
<bean id="person" class="com.hy.spring.beans.autowire.Person"
p:name="Tom" autowire="byType">
</bean>
-->
</beans>
Address.java
package com.hy.spring.beans.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 + "]";
}
}
Car.java
package com.hy.spring.beans.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 + "]";
}
public Car(){
System.out.println("Car‘s is Counstructor ....");
}
}
Person.java
package com.hy.spring.beans.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
+ "]";
}
}
Main.java
package com.hy.spring.beans.autowire;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans-autowire.xml");
Person person = (Person) ctx.getBean("person");
System.out.println(person);
}
}
标签:
原文地址:http://www.cnblogs.com/yang-hao/p/5793867.html