日期:2014-05-19 浏览次数:21070 次
package com.wp.model; import org.springframework.beans.factory.annotation.Autowired; public class Boss { @Autowired private Car car; @Autowired private Office office; public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public Office getOffice() { return office; } public void setOffice(Office office) { this.office = office; } @Override public String toString() { return office.getOfficeNo() + " " + car.getBrand() + " " + car.getPrice(); } }
<?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-2.5.xsd"> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="boss" class="com.wp.model.Boss"/> <bean id="office" class="com.wp.model.Office"> <property name="officeNo" value="001"/> </bean> <bean id="office2" class="com.wp.model.Office"> <property name="officeNo" value="002"/> </bean> <bean id="car" class="com.wp.model.Car" scope="singleton"> <property name="brand" value=" 红旗 CA72"/> <property name="price" value="2000"/> </bean> </beans>
package com.wp.client; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.wp.model.Boss; public class Client { public static void main(String[] args) { String[] locations = { "spring.xml" }; ApplicationContext ctx = new ClassPathXmlApplicationContext(locations); Boss boss = (Boss) ctx.getBean("boss"); System.out.println(boss); } }