Spring(一)IOC容器初始化和DI

一.由来

 1.IOC

IOC是Inversion of Control的缩写,多数书籍翻译成“控制反转”。1996年,Michael Mattson在一篇有关探讨面向对象框架的文章,首先提出了IOC 这个概念。IOC理论提出的观点大体是这样的:借助于“第三方”实现具有依赖关系的对象之间的解耦。这个第三方就是"IOC容器"。个人理解:在java中我们一般都是通过new来创建对象。但是有了ioc容器我们就能直接拿对象,因为容器已经按我们的配置(各种注入方式)创建好了对象。我们从"创建"=>"拿",这就是反转。

2.DI 

 IOC也叫依赖注入(DI)。2004年,Martin Fowler探讨了同一个问题,既然IOC是控制反转,那么到底是“哪些方面的控制被反转了呢?”,经过详细地分析和论证后,他得出了答案:“获得依赖对象的过程被反转了”。控制被反转之后,获得依赖对象的过程由自身管理变为了由IOC容器主动注入。于是,他给“控制反转”取了一个更合适的名字叫做“依赖注入(Dependency Injection)”。他的这个答案,实际上给出了实现IOC的方法:注入。所谓依赖注入,就是由IOC容器在运行期间,动态地将某种依赖关系注入到对象之中。所以,依赖注入(DI)和控制反转(IOC)是从不同的角度的描述的同一件事情,就是指通过引入IOC容器,利用依赖关系注入的方式,实现对象之间的解耦。个人理解:其实就是通过依赖注入实现控制反转。

 

二.初始化ioc容器的两种方法

1.xml文件配置  (顺带说明bean的初始化时间)

这是普遍使用的初始化方法。通常命名为applicationContext.xml.

新建2个实体类:Student 和Car

package org.test.object;

public class Student {
		private int sno;
		private String name;
		public int getSno() {
			return sno;
		}
		public void setSno(int sno) {
			this.sno = sno;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public Student(int sno, String name) {
			super();
			this.sno = sno;
			this.name = name;
			System.out.println("有参构造");
		}
		
		public Student() {
			
		}
		
		
package org.test.object;

public class Car {
		private String name;
		private String company;
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getCompany() {
			return company;
		}
		public void setCompany(String company) {
			this.company = company;
		}
		public Car(String name, String company) {
			super();
			this.name = name;
			this.company = company;
		}
		public Car() {
			
		}
}

在applicationContext.xml文件中配置(set方法注入) 并初始化。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	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
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- xml形式产生ioc容器 -->
<!-- setter getter 注入 -->

<bean id="student" class="org.test.object.Student">	
<property name="sno" value="117405521"></property>
<property name="name" value="吴迪"></property>
</bean>

<bean id="car" class="org.test.object.Car">
<property name="name" value="奥迪"></property>
<property name="company" value="aaa"></property>
</bean>



</beans>

测试方法:

	public static void XML() {
		ApplicationContext context	= new ClassPathXmlApplicationContext("applicationContext.xml");
		//Student stu = (Student) context.getBean("student");
		String[] names = context.getBeanDefinitionNames();
		for(String name :names) {
			
		System.out.println(name);
		
		}
		System.out.println(111);
		
	}

 

getBeanDefinitionNames()能拿到容器目前注入的所有bean的名字。

运行测试:

在代码块中我没有getBean(),但是结果显示2个实体类已被初始化。这就说明一旦启动Ioc容器,容器初始化时里面的bean也已经被初始化。

2.@Configuration注解初始化IOC容器

https://blog.csdn.net/zyhlwzy/article/details/88815508。推荐者这篇博客,非常详细。

 

三.xml配置的几种注入方式

1.set注入

<bean id="student" class="org.test.object.Student">	
<property name="sno" value="117405521"></property>
<property name="name" value="吴迪"></property>
</bean>

<bean id="car" class="org.test.object.Car">
<property name="name" value="奥迪"></property>
<property name="company" value="aaa"></property>
</bean>

先写到这,容我措词一会。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值