Spring全回顾之构造方法注入值

首先创建一个类Car

package com.kk.spring.beans;

public class Car {

    private String brand;
    private String corp;
    private double price;
    private int maxSpeed;

    public Car(String brand, String corp, double price) {
        super();
        this.brand = brand;
        this.corp = corp;
        this.price = price;
    }

    public Car(String brand, String corp, int maxSpeed) {
        super();
        this.brand = brand;
        this.corp = corp;
        this.maxSpeed = maxSpeed;
    }


    @Override
    public String toString() {
        return "Car [brand=" + brand + ", corp=" + corp + ", price=" + price
                + ", maxSpeed=" + maxSpeed + ", getClass()=" + getClass()
                + ", hashCode()=" + hashCode() + ", toString()="
                + super.toString() + "]";
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(int maxSpeed) {
        this.maxSpeed = maxSpeed;
    }
}

配置文件,配置如下:

<?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:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

    <!-- 
        通过构造方法来配置bean的属性
     -->
    <bean id="car" class="com.kk.spring.beans.Car">
        <constructor-arg value="Audi" index="0"></constructor-arg>
        <constructor-arg value="ShangHai" index="1"></constructor-arg>
        <constructor-arg value="300000" type="double"></constructor-arg>
    </bean>

</beans>

说明:Spring会根据类的构造方法的,参数列表的参数类型,参数个数来自动判断来向那个构造方法注入值

再来看个例子:

<?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:util="http://www.springframework.org/schema/util"
    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
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
        <!-- 
            使用构造器注入属性可以指定参数的位置和参数的类型,以区分构造器
         -->
        <bean id="car2" class="com.kk.spring.beans.Car">
        <constructor-arg value="BMW" type="java.lang.String"></constructor-arg>
        <constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
        <!-- 另一种方式使用value子节点配置,属性值也可以使用value直接进行配置 -->
        <constructor-arg type="int">
            <value>260</value>
        </constructor-arg>
        <!-- 
        <constructor-arg value="240" type="int"></constructor-arg>
        -->
</beans>

写个测试类

package com.kk.spring.beans;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("applicaContext.xml");

Car car = (Car) ctx.getBean("car");
System.out.println(car);

car = (Car) ctx.getBean("car2");
System.out.println(car);
}
}

猜你喜欢

转载自blog.csdn.net/fx9590/article/details/80367518
今日推荐