Spring五种属性注入的方法

Spring五种属性注入的方法

1.构造方法注入值

2.set、get方法注入值

3.p名称空间注入

4.SpEL方法注入

5.集合类型注入

各个序号对应一个方法!!!!
这里我只是写在一起了!

1.构造方法的方式注入值

Car1:

package com.lzhpo.demo5;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car1 {

    private String name;
    private Double price;

    public Car1(String name, Double price) {
        this.name = name;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Car1{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

2.set、get方法的方法注入值

Car2:

package com.lzhpo.demo5;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car2 {
    private String name;
    private Double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "Car2{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

3.p名称方法注入值

需要加约束:xmlns:p="http://www.springframework.org/schema/p"

Car3:

package com.lzhpo.demo5;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car3 {

    private String name;
    private Double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "Car3{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

4.SpEL的属性注入值

Car4:

package com.lzhpo.demo5;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car4 {
    private String name;
    private Double price;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getPrice() {
        return price;
    }

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

    @Override
    public String toString() {
        return "Car4{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

5.Spring集合属性的注入值

注入数组类型

Car5:

package com.lzhpo.demo5;

import java.util.Arrays;
import java.util.List;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car5 {
    private String[] Arrays;

    public String[] getArrays() {
        return Arrays;
    }

    public void setArrays(String[] arrays) {
        Arrays = arrays;
    }

    @Override
    public String toString() {
        return "Car5{" +
                "Arrays=" + java.util.Arrays.toString(Arrays) +
                '}';
    }
}

注入集合类型

Car6:

package com.lzhpo.demo5;

import java.util.List;

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class Car6 {

    private List list;

    public List getList() {
        return list;
    }

    public void setList(List list) {
        this.list = list;
    }

    @Override
    public String toString() {
        return "Car6{" +
                "list=" + list +
                '}';
    }
}

配置文件:




    
    
        
        
    

    
    
        
        
    

    
    

    
    
        
        
    

    
    
        
        
            
                小民
                张三
                李四
            
        
    
    
    
        
        
        
            
                老王
                小花
                小晓
            
        
    

测试类:

package com.lzhpo.demo5;

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

/**
 * @author lzhpo
 * @version 1.0
 * @since JDK 1.8.0_181
 * 类说明:
 **/
public class demo5 {

    public static void main(String[] args) {

    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");

    //1.构造方法的方式注入值
    Car1 car1 = (Car1) applicationContext.getBean("car1");
    System.out.println("=========1.构造方法的方式注入值=========");
    System.out.println(car1);

    System.out.println();

    //2.set、get方法的方法注入值
    Car2 car2 = (Car2) applicationContext.getBean("car2");
    System.out.println("=========2.setter/getter方法的方式注入值=========");
    System.out.println(car2);

    System.out.println();

    //3.p名称方法注入值
    Car3 car3 = (Car3) applicationContext.getBean("car3");
    System.out.println("=========3.p名称方法的方式注入值=========");
    System.out.println(car3);

    System.out.println();

    //4.使用SpEL的属性注入值
    Car4 car4 = (Car4) applicationContext.getBean("car4");
    System.out.println("=========4.使用SpEL的属性注入值=========");
    System.out.println(car4);

    System.out.println();

    //5.Spring集合属性的注入值(数组)
    Car5 car5 = (Car5) applicationContext.getBean("car5");
    System.out.println("=========5.Spring集合属性的注入值(数组)=========");
    System.out.println(car5);

    System.out.println();

    //5.Spring集合属性的注入值(list)
        //换成set、map这些都是一样的道理,吧list改一下就行了
    Car6 car6 = (Car6) applicationContext.getBean("car6");
    System.out.println("=========5.Spring集合属性的注入值(list)=========");
    System.out.println(car6);


    }
}

运行结果:

=========1.构造方法的方式注入值=========
Car1{name='宝马', price=1000000.0}

=========2.setter/getter方法的方式注入值=========
Car2{name='奔驰', price=1500000.0}

=========3.p名称方法的方式注入值=========
Car3{name='大众', price=300000.0}

=========4.使用SpEL的属性注入值=========
Car4{name='奥迪', price=500000.0}

=========5.Spring集合属性的注入值(数组)=========
Car5{Arrays=[小民, 张三, 李四]}

=========5.Spring集合属性的注入值(list)=========
Car6{list=[老王, 小花, 小晓]}

你可能感兴趣的:(后端-框架-Spring)