注意:实体需要有无参构造(无参构造IOC可以new一个对象,然后才能注入参数)
实体需要有setter方法才能注入到实体中.
第一步:实体
public class Car { private String name; private Double price; public Car() { } public Car(String name, Double price) { this.name = name; this.price = price; } public String getName() { return name; } public void setName(String name) { this.name = name; }
第二步:配置
<!--setter方法注入--> <bean id="car1" class="it.heima.domain.Car"> <property name="name" value="保时捷"/> <property name="price" value="11000"/> </bean>
第三步:测试
@Test public void test(){ ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml"); Car car = applicationContext.getBean("car1", Car.class); System.out.println(car); }