Spring框架基于配置文件的bean属性注入

Bean注入基本类型的方式有4种

  1. set方式注入(重点)
  2. 构造方法的注入(重点)
  3. p名称空间注入(了解)
  4. spel(Spring Expression Language)注入(了解)

实体类

package com.springday.bean;

public class User {
	private String name;
	private Integer age;
	private Car car;

        public User(){}
	public User(String name, Integer age, Car car) {
		this.name = name;
		this.age = age;
		this.car = car;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
	
}
package com.springday.bean;

public class Car {
	private String name;
	private String color;

        public Car(){}
        public Car(String name, String color) {
		this.name = name;
		this.color = color;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Car [name=" + name + ", color=" + color + "]";
	}	
}

set方式注入



	
	
	
	
	

	
	
	
	

构造方法的注入



		      
	
	
	
	
	
	
	
	
	




    
    
	
	
	
	
	
	
	
	

p名称空间注入



    
    
    
    

spel(Spring Expression Language)表达式注入


    
	
	
	
	
	
	
	
	


	
    
		
		
		
	

总结

   其实注入方式就只有设值注入和构造注入这两种方式,也就是set方式和构造方法这两种方式,只需要掌握这两种就可以了,后面两种方法p名称空间注入和spel表达式注入本质上也是基于set方式注入,只需要了解就可以了。

你可能感兴趣的:(java,spring,框架)