Spring:IOC容器创建对象

郎家岭伯爵 2022年01月17日 693次浏览

什么是IOC

Ioc—Inversion of Control,即“控制反转”,不是什么技术,而是一种设计思想。在Java开发中,IOC意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制。如何理解好Ioc呢?理解好IOC的关键是要明确“谁控制谁,控制什么,为何是反转(有反转就应该有正转了),哪些方面反转了”,那我们来深入分析一下:

  • 谁控制谁,控制什么:传统JavaSE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象;而IOC是有专门一个容器来创建这些对象,即由Ioc容器来控制对象的创建;谁控制谁?当然是IoC容器控制了对象;控制什么?那就是主要控制了外部资源获取(不只是对象包括比如文件等)。

  • 为何是反转,哪些方面反转了:有反转就有正转,传统应用程序是由我们自己在对象中主动控制去直接获取依赖对象,也就是正转;而反转则是由容器来帮忙创建及注入依赖对象;为何是反转?因为由容器帮我们查找及注入依赖对象,对象只是被动的接受依赖对象,所以是反转;哪些方面反转了?依赖对象的获取被反转了。

IOC实现

pom文件

注:

  • 需导入springframework依赖。
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>

  6. <groupId>org.example</groupId>
  7. <artifactId>Spring-IOC-05</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <dependency>
  11. <groupId>org.springframework</groupId>
  12. <artifactId>spring-context</artifactId>
  13. <version>5.2.12.RELEASE</version>
  14. <scope>test</scope>
  15. </dependency>
  16. </dependencies>
  17. <properties>
  18. <maven.compiler.source>15</maven.compiler.source>
  19. <maven.compiler.target>15</maven.compiler.target>
  20. </properties>

  21. </project>

创建一个pojo(Hello.java)

注:

  • pojo需有getter、setter方法;
  • pojo需重写toString方法。
  1. package com.langjialing.pojo;

  2. public class Hello {

  3. private String helloString;

  4. public String getHelloString() {
  5. return helloString;
  6. }

  7. public void setHelloString(String helloString) {
  8. this.helloString = helloString;
  9. }

  10. @Override
  11. public String toString() {
  12. return "Hello{" +
  13. "helloString='" + helloString + '\'' +
  14. '}';
  15. }
  16. }

创建beans.xml

注:

  • 此xml文件即为IOC容器注册对象的位置;
  • beans.xml的文件名可以任意取值,例如:ajdohduaho.xml;
  • 文件格式来源于Spring官方
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">

  6. <bean id="hello" class="com.langjialing.pojo.Hello"
  7. name="hello-langjialing hello-langjialing1,hello-langjialing2;hello-langjialing3">
  8. <property name="helloString" value="langjialing"></property>
  9. </bean>

  10. </beans>

bean.xml文件

bean.xml文件

IOC容器成功创建对象截图

IOC容器成功创建对象截图

编写测试类

注:

  • 对象创建的方式为ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");,这也是Spring官方指定的。
  • context创建完成后,即可通过context来操作对象了。
  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;

  3. public class MyTest {
  4. public static void main(String[] args) {
  5. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  6. System.out.println(context.getBean("hello-langjialing"));
  7. System.out.println(context.getBean("hello-langjialing1"));
  8. System.out.println(context.getBean("hello-langjialing2"));
  9. System.out.println(context.getBean("hello-langjialing3"));
  10. }
  11. }

测试结果

测试结果

如果有多个对象怎么处理?

注:

  • 通常此情况应用于多人协同开发的场景。

创建第二个pojo类(Hello1.java)

同样需要有getter和setter方法,并重写toString方法。

  1. package com.langjialing.pojo;

  2. public class Hello1 {
  3. private String helloString1;

  4. public String getHelloString1() {
  5. return helloString1;
  6. }

  7. public void setHelloString1(String helloString1) {
  8. this.helloString1 = helloString1;
  9. }

  10. @Override
  11. public String toString() {
  12. return "Hello1{" +
  13. "helloString1='" + helloString1 + '\'' +
  14. '}';
  15. }
  16. }

创建第二个beans.xml文件(beans-1.xml)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">

  6. <bean id="hello1" class="com.langjialing.pojo.Hello1"
  7. name="hello-langjialing-1">
  8. <property name="helloString1" value="langjialing-hello1"></property>
  9. </bean>


  10. </beans>

将第二个xml文件import到第一个xml文件中

注:

  • 在beans.xml文件中添加<import resource="beans-1.xml"></import>,代表引入xml文件。

beans.xml完整代码:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. https://www.springframework.org/schema/beans/spring-beans.xsd">

  6. <bean id="hello" class="com.langjialing.pojo.Hello"
  7. name="hello-langjialing hello-langjialing1,hello-langjialing2;hello-langjialing3">
  8. <property name="helloString" value="langjialing"></property>
  9. </bean>

  10. <import resource="beans-1.xml"></import>


  11. </beans>

测试类中进行测试

测试类代码:

  1. import org.springframework.context.ApplicationContext;
  2. import org.springframework.context.support.ClassPathXmlApplicationContext;

  3. public class MyTest {
  4. public static void main(String[] args) {
  5. ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
  6. System.out.println(context.getBean("hello-langjialing"));
  7. System.out.println(context.getBean("hello-langjialing1"));
  8. System.out.println(context.getBean("hello-langjialing2"));
  9. System.out.println(context.getBean("hello-langjialing3"));

  10. System.out.println("============");
  11. System.out.println(context.getBean("hello1"));
  12. }
  13. }

引入成功

引入成功

文件目录结构

文件目录结构

文件目录结构