依赖注入_配置构造注入

第一步:实体

public class Car {
    private String name;
    private Double price;
    ...

}

第二步:配置

<?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.xsd">

    <bean id="car" class="it.heima.domain.Car">
        <constructor-arg index="0" value="保时捷"/>
        <constructor-arg index="1" value="1000000"/>
    </bean>
</beans>

第三步:测试

public class CarTest {
    @Test
    public void test(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        Car car = applicationContext.getBean("car", Car.class);
        System.out.println(car);
    }

猜你喜欢

转载自blog.csdn.net/weixin_42333583/article/details/81534462