Spring IoC详解

一、Spring IoC的概念
Ioc指的是控制反转。由开发人员采用 “new 被调用者” 的方式来创建对象变成由Spring容器创建对象,这就叫做控制反转。实现控制反转的就是Spring Ioc容器。
二、Spring Ioc容器
Spring IoC容器有两个接口:BeanFactory接口和ApplicationContext接口。实际开发中,一般使用第二个接口。
SpringIoc的应用过程:
1.创建实体类

2.在src目录下创建Spring的配置文件applicationContext.xml

  • <bean>:该标签让spring容器创建对象
  • id:表示spring创建的对象在spring容器中的唯一标识。
  • class:要创建的对象的类的权限名。
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean id="stu" class="com.spring.ioc.Student"></bean>
</beans>

3.创建容器解析配置文件

 public static void main(String[] args) {
		//1.创建spring容器
		ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
		//2.通过容器获取实例
		Student stu = (Student)app.getBean("stu");
		//3.可用实例去调用该实例的方法.....

三、依赖注入(DI)
概念:IoC容器的具体实现方法,用于给Sring容器创建的对象的属性赋值,还可以实现对象之间的依赖关系。
实现方式:使用构造方法注入和使用属性的set方法注入
创建实体类:

@Data
public class Husband {
	private String name;
	private int age;
	private double salary;
	private Date date;
	private Wife wife;
	public Husband(String name, int age,Wife wife) {
		super();
		this.name = name;
		this.age = age;
		this.wife = wife;
	}	
}

1.使用set方法注入

  • <property>:该标签用于设置一个属性
  • name:表示属性名
  • value:表示属性值
  • ref:需要的数据为bean类型,用ref。如一个对象或一个类。ref的值是对象的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
        https://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <bean id="husband" class="com.spring.ioc.Husband">
    	<property name="name" value="Lucas"></property>
    	<property name="age" value="33"></property>
    	<property name="salary" value="3000.5"></property>
    	<property name="date" ref="now"></property>
    	<property name="wife" ref="w"></property>
    </bean>
    <bean id="w" class="com.spring.ioc.Wife">
    	<property name="name" value="Lucy"></property>
    	<property name="age" value="33"></property>
    </bean>
    <bean id="now" class="java.util.Date"></bean>
</beans> 

2.使用构造方法注入

  • <constructor-arg>:该标签用于指定构造方法中的参数
  • name:参数在构造函数中的名字
  • value:参数的值
  • index:参数在构造函数中的索引位置,从0开始
<bean id="husband" class="com.spring.ioc.Husband">
    	<constructor-arg name="name" value="Lucas"></constructor-arg>
    	<constructor-arg name="age" value="33"></constructor-arg>
    	<constructor-arg name="wife" ref="w"> </constructor-arg>
    </bean>
    <bean id="w" class="com.spring.ioc.Wife">
    	<constructor-arg name="name" value="Lucy" index="0"></constructor-arg>
    	<constructor-arg name="age" value="33" index="1"></constructor-arg>
    </bean>

四.创建Spring容器的方式:
1.ClassPathXmlApplicationContext:从类的根路径下加载配置文件。使用的是配置文件用该类创建spring容器
ApplicationContext app= new ClassPathXmlApplicationContext(“applicationContext.xml”);
2. AnnotationConfigApplicationContext:用注解配置容器对象时,需要使用此类来创建spring容器。使用的是配置类,用该类创建spring容器
ApplicationContext app = new AnnotationConfigApplicationContext(Config.class);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值