Spring的小IOC案例

spring项目
  • 创建maven项目 ,引入junit、spring的jar包:在maven工程的pom.xml文件的根标签(project)内添加如下配置:
<dependencies>
	<!-- 添加junit的jar包 -->
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.10</version>
	</dependency>
	<!-- 添加spring的jar包 -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>4.1.3.RELEASE</version>
	</dependency>
</dependencies>
  • 创建User类,声明name和age属性,并添加对应的setter和getter方法,以及toString方法
package com.pojo;

public class User {
  private String name;
  private String age;
  
  public String getName() {
  	return name;
  }
  public void setName(String name) {
  	this.name = name;
  }
  public String getAge() {
  	return age;
  }
  public void setAge(String age) {
  	this.age = age;
  }
  @Override
  public String toString() {
  	return "User [name=" + name + ", age=" + age + "]";
  }
}
  • User类的bean实例,交给Spring容器创建,在核心配置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">
   <!--将User对象交给spring容器管理  ,class为类的全限定名
       class属性:被管理对象的完整类名
       id属性:Spring能找到当前 Bean 的一个依赖的编号名称不可重复,不能使用特殊字符
      	property 元素是定义类的属性,其中的 name 属性定义的是属性的名称,而 value 是它的值。
   -->
   <!-- 将User接口的实现类的实例交给spring创建 -->
   <bean id="user" class="com.pojo.User" name="">
   	<property name="name" value="熊大"></property> 
  	<property name="age" value="18"></property>
   </bean>
</beans>
  • 然后新建testspring类
 package com.Test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pojo.User;

public class testSpring {
	static ApplicationContext context=null;
	static {
		 context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}
	
	@Test
	public void testUser() {
		User u = (User) context.getBean("user");
		System.out.println(u);
	}
}
  • 下面运行我们的代码,可以正常运行,并且拿到在applicationContext.xml配置的Bean
五月 31, 2020 12:41:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@ea30797: startup date [Sun May 31 12:41:16 CST 2020]; root of context hierarchy
五月 31, 2020 12:41:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
User [name=熊大, age=18]


介绍下IOC:控制反转
传统模式:我们所有的对象都是new出来,什么放在堆中的是吧,而 IOC是反转控制 (Inversion Of Control)的缩写,把控制权从我们手里交到spring容器手里,直接从Spring那里去获取一个对象。

DI:Dependency Injection(依赖注入)
依赖注入是指 Spring 创建对象的过程中,将对象依赖属性(简单值,集合,对象)通过配置设值给该对象

(1).Set方式注入
(2).构造方法注入(略)

  • 下面我们在创建一个customer类
package com.pojo;

public class customer {
	
	//这里只关联一个User类对象
	private User u;

	public User getU() {
		return u;
	}

	public void setU(User u) {
		this.u = u;
	}

	@Override
	public String toString() {
		return "customer [u=" + u + "]";
	}
}
  • 下面在applicationContext.xml中配置一下customer类
	<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
   <!--将User对象交给spring容器管理  ,class为类的全限定名
       class属性:被管理对象的完整类名
       id属性:Spring能找到当前 Bean 的一个依赖的编号名称不可重复,不能使用特殊字符
      	property 元素是定义类的属性,其中的 name 属性定义的是属性的名称,而 value 是它的值。
   -->
   
   <!-- <context:component-scan base-package="com.pojo"/> -->
   <!-- 将User接口的实现类的实例交给spring创建 -->
   <bean id="user" class="com.pojo.User" name="">
   	<property name="name" value="熊大"></property> 
  	<property name="age" value="18"></property>
   </bean>
   
   	<!-- 将customer类的实例交给spring创建   注入另一个类的对象时:使用ref-->
	<bean id="customer" class="com.pojo.customer">
		<property name="u" ref="user"></property>
	</bean>
   
</beans>

  • 修改testspring代码
package com.Test;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.pojo.User;
import com.pojo.customer;

public class testSpring {
	static ApplicationContext context = null;
	static {
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	@Test  //测试User
	public void testUser() {
		User u = (User) context.getBean("user");
		System.out.println(u);
	}

	@Test //测试customer
	public void testcutomer() {
		customer cus = (customer) context.getBean("customer");
		System.out.println(cus);
	}
}
  • 运行结果如下
五月 30, 2020 11:26:15 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@aec6354: startup date [Sat May 30 23:26:15 CST 2020]; root of context hierarchy
五月 30, 2020 11:26:15 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
customer [u=User [name=熊大, age=18]]

IoC 和 DI 其实是同一个概念的不同角度描述,DI 相对 IoC 而言,明确描述了“被注入对象依赖 IoC 容器,配置依赖对象”
思考:IOC控制反转如何实现?
最后我们简单说说IoC是如何实现的。想象一下如果我们自己来实现这个依赖注入的功能,我们怎么来做? 无外乎:

  • 读取标注或者配置文件,看看user依赖的是哪个源文件,拿到类名

  • 使用反射的API,基于类名实例化对应的对象实例

  • 将对象实例,通过构造函数或者 setter,传递给 user

我们发现其实自己来实现也不是很难,Spring实际也就是这么做的。这么看的话其实IoC就是一个工厂模式的升级版!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值