Spring的核心机制:依赖注入,控制反转,构造方法注入,setter方法注入

一. 依赖注入的概念:

  1. 依赖注入和控制反转的含义相同,当某个Java对象(调用者)需要调用另一个java对象(被调用者,即被依赖对象)时,传统的方法是采用“new”的方式,
  2. 在使用spring框架后,对象的实例调用不在由调用者创建,而是由spring容器创建,spring容器会空着程序之间的关系,而不是由调用者的程序代码而控制。
  3. 从spring容器来看,spring容器负责将被依赖对象赋值给调用者的成员变量,这就相当于调用者注入了他的依赖实例,这就是spring的依赖注入。

二. 依赖注入的类型
依赖注入的作用就是spring框架创建时,动态的将其所依赖的对象注入到Bean组件中,其现在主要有两种主要的注入方式构造方法setter方法注入

三. 演示这两种方式实例

package ssm.Entity;

public class AdminInfo {
    
    
    private int id;
    private String name;
    private  String pwd;
    public void setId(int id){
    
    
        this.id=id;
    }
    public AdminInfo(){
    
    
    }
    public  AdminInfo(String name,String pwd){
    
    
        this.name=name;
        this.pwd=pwd;
    }
    
    public void pring(){
    
    

        System.out.print(id+"--" + name +"--"+pwd);
    }
}

配置文件
applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 配置一个bean,将指定类配置给Spring,让Spring来创建其对象的实例 -->
	<bean id="helloSpring" class="com.ssm.HelloSpring">
		<!-- 为属性赋值 -->
		<property name="userName" value="张三"></property>
	</bean>
	
	<bean id="adminInfo" class="com.ssm.AdminInfo">
		<property name="id" value="5"></property>
		<constructor-arg name="name" value="admin" ></constructor-arg>
		<constructor-arg name="pwd" value="123456"/>
	</bean>
	<bean id="admin"  class="com.ssm.AdminInfo" p:id="8" p:name="yzpc" p:pwd="yzpc" />
	
</beans>
package com.ssm;

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

public class TestHelloSpring {
    
    

	public static void main(String[] args) {
    
    
		
		//初始化Spring容器,加载applicationContext.xml文件
		ApplicationContext ctx= new ClassPathXmlApplicationContext("applicationContext.xml");
		//通过容器获取配置中的helloSpring的实例
		HelloSpring helloSpring=(HelloSpring) ctx.getBean("helloSpring");
		System.out.println(helloSpring);
		helloSpring.show();

	}

}

运行结果:

在这里插入图片描述
setter方法注入:

  1. 将AdminInfo生成set方法

  2. 改变配置

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
		<!-- 使用setter方法注入-->
		<bean id="admin"  class="com.ssm.AdminInfo" p:id="8" p:name="yzpc" p:pwd="yzpc" />
</beans>		

运行结果:
在这里插入图片描述
两种方式的对比:他们都具有优缺点在实际开发中科根据自己的需求灵活变通

  1. setter:与传统的javaBean写法类似,在开发中更容易被接受和理解。
  2. 对于复杂的依赖关系如果使用构造方法,会是的程序代码变得更加臃肿。
  3. 对于依赖关系无须变化bean,构造方法注入更有用处。

猜你喜欢

转载自blog.csdn.net/qq_14930709/article/details/106315388