Spring IOC (Inversion Of Control反转控制容器

一、对于IOC容器的简单理解

在java开发中将程序中的对象交给容器管理,而不是在对象的内部管理。

那么两个简单的问题去分析理解IOC容器

1.为什么叫反转:

IOC容器实现了将对象的管理由对象内部直接管理(比如在一个类的main方法中new了一个String对象)转换成在容器中被动的被注入,创建。把这种对对象管理方式的改变称为反转。

2.控制了什么:

IOC容器控制了外部资源的统一获取(不仅仅包括对象,还有其他的文件)

(敲下黑板:spring容器是IOC容器,但是IOC容器不是spring容器,因为基于IOC容器设计的框架不仅只有spring)

二、应用上下文

现在我们手上有了一个魔杖(IOC容器)但是我们还需要对应的魔咒(应用上下文)来驱动魔杖

spring容器有两种实现:

1.最基础的BeanFactory(实体工厂)由于功能过于简单所以不经常使用

2.由BeanFactory派生的多种应用上下文,其抽象接口是ApplicationContext,spring根据应用场景的不同给我们设计了几种应用上下文:

(1) AnnotationConfigApplicationContext:从一个或多个基于java的配置类中加载上下文定义,适用于java注解的方式;

(2)ClassPathXmlApplicationContext:从类路径下的一个或多个xml配置文件中加载上下文定义,适用于xml配置的方式;

(3)FileSystemXmlApplicationContext:从文件系统下的一个或多个xml配置文件中加载上下文定义,也就是说系统盘符中加载xml配置文件;

(4)AnnotationConfigWebApplicationContext:专门为web应用准备的,适用于注解方式;

我们在这里使用的是ClassPathXmlApplication,xml配置方式

三、简单的实现IOC控制对象

1.创建一个应用上下文applicationContext_ioc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:util="http://www.springframework.org/schema/util"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  7. xmlns:p="http://www.springframework.org/schema/p"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. https://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. https://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. https://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/util
  15. http://www.springframework.org/schema/util/spring-util.xsd
  16. ">
  17. <beans>

2.创建一个类Student

  1. package spring.ioc.unities;
  2. public class Student {
  3. private String stuNo;
  4. private String stuName;
  5. private int gender;
  6. private String nativePlace;
  7. private String classNo;
  8. private String tel;
  9. public String getStuNo() {
  10. return stuNo;
  11. }
  12. public void setStuNo(String stuNo) {
  13. this.stuNo = stuNo;
  14. }
  15. public String getStuName() {
  16. return stuName;
  17. }
  18. public void setStuName(String stuName) {
  19. this.stuName = stuName;
  20. }
  21. public int getGender() {
  22. return gender;
  23. }
  24. public void setGender(int gender) {
  25. this.gender = gender;
  26. }
  27. public String getNativePlace() {
  28. return nativePlace;
  29. }
  30. public void setNativePlace(String nativePlace) {
  31. this.nativePlace = nativePlace;
  32. }
  33. public String getClassNo() {
  34. return classNo;
  35. }
  36. public void setClassNo(String classNo) {
  37. this.classNo = classNo;
  38. }
  39. public String getTel() {
  40. return tel;
  41. }
  42. public void setTel(String tel) {
  43. this.tel = tel;
  44. }
  45. @Override
  46. public String toString() {
  47. return "Student [stuNo=" + stuNo + ", stuName=" + stuName + ", gender=" + gender + ", nativePlace="
  48. + nativePlace + ", classNo=" + classNo + ", tel=" + tel + "]";
  49. }
  50. }

3.在xml中通过bean标签俩控制Bean

(1)标签中必要的两个属性

id(用于识别ioc容器中的bean)

class(id对应的类的位置,通常是包名+类名,建议直接复制,手写有可能出错)


  1. <bean id="stu1" class="spring.ioc.unities.Student"></bean>
  1.  

(2)标签中其他的属性

scope属性:

 作用域  说明
 Singleton  

Spring IOC容器中一个bean定义只有一个对象实例,默认情况下会在容器启动时初始化

 

 Prototype 每次从容器获取bean都是新的对象 
 Request  每次Http请求都会创建一个新的bean,只使用在WebApplicationContext中
 Session  类似request,每次有新的会话都会创建一个新的Bean,只使用在WebApplicationContext中

3.main函数中测试确认:

  1. public static void main(String[] args) {
  2. ApplicationContext ioc = new ClassPathXmlApplicationContext("applictionContext_ioc.xml");
  3. Student stuA = (Student) ioc.getBean("stu1");
  4. Student stuB = (Student) ioc.getBean("stu1");
  5. //在ioc中获取的bean:
  6. System.out.println("获取的属性为singleton的bean-------------------------");
  7. System.out.println(stuA.toString()+stuB.toString());
  8. //验证A,B是否是同一个实体
  9. System.out.println("stua和stub是否是一个bean:"+(stuA==stuB));
  10. Student stuC = (Student) ioc.getBean("stu2");
  11. Student stuD = (Student) ioc.getBean("stu2");
  12. System.out.println("获取的属性为prototype的bean-------------------------");
  13. System.out.println(stuC.toString()+stuD.toString());
  14. System.out.println("stuc和stud是否是一个bean:"+(stuC==stuD));
  15. }

获得结果如下:

可以看出singleton属性的bean其实是单例模式下的bean.

版权声明:本文为zzu-superYi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/zzu-superYi/p/11963481.html