Spring依赖注入

一、什么是依赖注入

当我们创建POJO类时,这些类都会有各自的属性,属性的类型可能时java自带的基本类型或引用类型,或者是我们自己定义的对象。对于使用spring来配置bean时,如果要给bean提供初始化参数,就要用到依赖注入的方式,所谓的依赖注入就是通过spring的配置文件,将参数传递到bean实例对象的过程。

二、通过属性注入实现依赖注入

1、原理

属性注入是在Bean实例被创建后,通过反射的方式调用Setter放来注入属性值的。

2、步骤

1、定义POJO类

创建两个POJO类,User类Pet类User类中有一个属性类型为Pet型
User类

    public class User {
    	private int id;
    	private String username;
    	private String password;
    	private Pet pet;
    	
    	public User() {	
    		super();
    		System.out.println("User类的构造器被调用了");
    	}
    	
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getUsername() {
    		return username;
    	}
    	public void setUsername(String username) {
    		this.username = username;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    	
    	public Pet getPet() {
    		return pet;
    	}
    
    	public void setPet(Pet pet) {
    		this.pet = pet;
    	}
    	
    	@Override
    	public String toString() {
    		return "User [id=" + id + ", username=" + username + ", password=" + password + ", pet=" + pet + "]";
    	}
    }  

Pet类

        public class Pet {

    	private int id;
    	//宠物类型
    	private String type;
    	
    	public Pet() {
    		super();
    		System.out.println("Pet类的构造器被调用");
    	}
    
    	public int getId() {
    		return id;
    	}
    
    	public void setId(int id) {
    		this.id = id;
    	}
    
    	public String getType() {
    		return type;
    	}
    
    	public void setType(String type) {
    		this.type = type;
    	}
    	
    	@Override
    	public String toString() {
    		return "Pet [id=" + id + ", type=" + type + "]";
    	}
    	
    }

2、配置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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 
		1、一对bean标签即代表一个POJO类
		2、属性id有程序员设置,一般来说建议与类名相同
		3、属性class,即该类的全类名
	 -->
	<bean id="user" class="priv.wfq.spring.model.User">
		<!-- property这个标签是依赖注入是使用的标签,可以理解为POJO类中的属性 -->
		<!-- 
			property标签中的属性name,是我们要注入的属性的标识.
			name属性的值为  Setter方法中,setXXX中的 XXX将首字母小写后的字符串 ,
			而不是我们定义属性时的变量名.
		 -->
		 <!-- 当属性类型为基本数据类型或引用数据类型时,值通过value这个属性来传递 -->
		<property name="id" value="1"></property>
		<property name="username" value="小明"></property>
		<property name="password" value="xiaoming123"></property>
		<!-- 当属性类型为我们自己定义的类时,通过 ref 这个属性来传递对象实例,且这个对象要在xml中有配置,
			 值为该对象bean标签下的 属性 id -->
		<property name="pet" ref="pet"></property>
	</bean>
	
	<bean id="pet" class="priv.wfq.spring.model.Pet">
		<property name="id" value="1000"></property>
		<property name="type" value="小狗"></property>
	</bean>
	</beans>

3、测试

  • 测试的时候自己在Setter方法中加入输出来做一些判断

测试类

    public void testUserServiceImpl() {
    	//获取IOC容器
    	ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");	
    	//通过ApplicationContext对象的getBean方法来获取对象实例	
        User user = (User) context.getBean("user");
    	System.out.println(user);
    }
1、给User类所有参数都初始化

2、给User类除pet属性外所有参数都初始化

4、测试结果总结

  • spring配置文件中使用property标签做初始化,是调用类的Setter方法来实现依赖注入的
  • 容器会先调用类的无参构造器创建出对象的实例,在来实现依赖注入。由于User这个Bean的配置中有初始化参数pet,但是这个实例还没有被创建,所以容器并没有真正开始做User类的初始化,而是转头先将Pet类创建实例,做初始化。最后在来给User类初始化
posted @ 2019-04-24 21:15  hamawep  阅读(205)  评论(0编辑  收藏  举报