二、Spring框架 IOC+DI详解

版权声明:未经本人同意,不得私自转载 https://blog.csdn.net/weixin_41866744/article/details/87892317

IOC(Inversion of Control):中文名称:控制反转

Ioc是什么:IoC 完成的事情原先由程序员主动通过 new 实例化对象事情,转交给 Spring容器 负责.

控制反转中控制指的是:控制类的对象. 控制反转中反转指的是转交给 Spring 负责.
IoC 最大的作用:解耦 程序员不需要管理对象.解除了对象管理和程序员之间的耦合
 

Spring 创建对象的三种方式:

1.通过构造方法创建:

           无参构造创建:默认情况; 

           有参构造创建:需要明确配置:(1)需要在类中提供有参构造方法(2)在 applicationContext.xml 中设置

           调用哪个  构造方法创建 (3)如果设定的条件匹配多个构造方法执行最后的构造方法

   

package com.tao.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

public class People {
	
	private int id;
	private String name;

	public People() {
		super();
		System.out.println("无参构造");
	}

	public People(int id, String name) {
		this.id = id;
		this.name = name;
		System.out.println("有参构造[id,name]");
	}
	
	public People(String name,int id) {
		this.id = id;
		this.name = name;
		System.out.println("有参构造[name,id]");
	}	

}
<?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">

	<!--通过默认构造方法创建 index: 参数的索引,从 0 开始  name: 参数名  type:类型(区分开关键字 
        和封装类 int 和 Integer)  -->
	<bean id="people" class="com.tao.pojo.People">
		<constructor-arg index="0" type="int" name="id" value="1" />
		<constructor-arg index="1" type="java.lang.String" name="name" value="张三"/>
	</bean>


	
</beans>

2.实例工厂:

           实例工厂:需要先创建工厂,才能生产对象

           步骤:1.创建实例工厂;2.在 applicationContext.xml 中配置工厂对象和需要创建的对象
 

package com.tao.pojo;

public class PeopleFactory {
	public People newInstance(){
		System.out.println("Spring实例工厂创建对象");
		return new People(1,"测试对象");
	}

}
<?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">

	<!--2.通过实例工厂创建对象 -->
	<bean id="peopleFactory" class="com.tao.pojo.PeopleFactory"></bean>
	<bean id="people" factory-bean="peopleFactory" factory-method="newInstance"></bean> 

	
</beans>

3. 静态工厂:不需要创建工厂,快速创建对象.

    步骤:(1)编写一个静态工厂(在方法上添加 static);  (2)在 applicationContext.xml 中引用配置 

package com.tao.pojo;

public class PeopleFactory {
	public static People newInstance(){
		System.out.println("Spring实例工厂创建对象");
		return new People(1,"测试对象");
	}

}
<?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">

	<bean id="people" class="com.tao.pojo.PeopleFactory" factory-method="newInstance"> </bean>
	
</beans>
Spring给Bean属性赋值:
package com.tao.pojo;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader.Array;

public class People {
	
	private int id;
	private String name;
	private Set<String> sets;
	private List<String> lists;
	private Integer[] arrays;
	private Map<String, Object> maps;
	private Desk desk;
	
	public Desk getDesk() {
		return desk;
	}
	public void setDesk(Desk desk) {
		this.desk = desk;
	}

	public People() {
		super();
		System.out.println("无参构造");
	}

	public People(int id, String name) {
		this.id = id;
		this.name = name;
		System.out.println("有参构造[id,name]");
	}
	
	public People(String name,int id) {
		this.id = id;
		this.name = name;
		System.out.println("有参构造[name,id]");
	}
	
	public Set<String> getSets() {
		return sets;
	}

	public void setSets(Set<String> sets) {
		this.sets = sets;
	}

	public List<String> getLists() {
		return lists;
	}

	public void setLists(List<String> lists) {
		this.lists = lists;
	}

	public Integer[] getArrays() {
		return arrays;
	}

	public void setArrays(Integer[] arrays) {
		this.arrays = arrays;
	}

	public Map<String, Object> getMaps() {
		return maps;
	}

	public void setMaps(Map<String, Object> maps) {
		this.maps = maps;
	}

	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "People [id=" + id + ", name=" + name + ", sets=" + sets + ", lists=" + lists + ", arrays="
				+ Arrays.toString(arrays) + ", maps=" + maps + ", desk=" + desk + "]";
	}

}

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

	<!--通过默认构造方法创建 index: 参数的索引,从 0 开始  name: 参数名  type:类型(区分开关键字 
        和封装类 int 和 Integer) 
	<bean id="people" class="com.tao.pojo.People">
		<constructor-arg index="0" type="int" name="id" value="1" />
		<constructor-arg index="1" type="java.lang.String" name="name" value="张三"/>
	</bean> -->
	
	<!--2.通过实例工厂创建对象
	<bean id="peopleFactory" class="com.tao.pojo.PeopleFactory"></bean>
	<bean id="people" factory-bean="peopleFactory" factory-method="newInstance"></bean> -->

	
	<!--3.静态工厂 要求工厂类是创建对象的方法是静态的 
	<bean id="people" class="com.tao.pojo.PeopleFactory" factory-method="newInstance"></bean> -->
	
	<bean id="people" class="com.tao.pojo.People">
		<property name="id" value="1" />
		<property name="name" value="张三" />
		<property name="sets">
			<set>
				<value>张三</value>
				<value>李四</value>
			</set>
		</property>
		<property name="lists">
			<list>
				<value>王五</value>
				<value>赵六</value>
			</list>
		</property>
		<property name="arrays">
			<array>
				<value>1</value>
				<value>2</value>
			</array>
		</property>
		<property name="maps">
			<map>
				<entry key="a" value="b" />
				<entry key="c" value="d" />
			</map>
		</property>
		<property name="desk" ref="desk"></property>
	</bean>
	
	<!-- 定义对象属性 通过DI进行依赖 -->
	<bean id="desk" class="com.tao.pojo.Desk">
		<property name="id" value="1"></property>
		<property name="price" value="25.00"></property>
	</bean>
	
</beans>

DI((Dependency Injection):依赖注入

DI 是什么?
    DI 和 IoC 是一样的, 当一个类(A)中需要依赖另一个类()对象时,把 B 赋值给 A 的过程就叫做依赖注入.

<?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">
	
	<bean id="people" class="com.tao.pojo.People">
		<property name="id" value="1" />
		<property name="name" value="张三" />
                <!-- 进行依赖注入 -->
		<property name="desk" ref="desk"></property>
	</bean>
	
	<!-- 定义对象属性 通过DI进行依赖 -->
	<bean id="desk" class="com.tao.pojo.Desk">
		<property name="id" value="1"></property>
		<property name="price" value="25.00"></property>
	</bean>
	
</beans>

猜你喜欢

转载自blog.csdn.net/weixin_41866744/article/details/87892317
今日推荐