美文网首页
Spring IOC&DI

Spring IOC&DI

作者: Vicent_Z | 来源:发表于2023-06-26 10:12 被阅读0次

1.Spring IOC

IOC Inverse of Control 反转控制:将原本在程序中手动创建UserService对象的控制权,交由Spring框架管理

  • 创建对象的控制权被反转到了Spring框架

1.1.原理

1-1.jpg

通过工厂+反射+配置文件的方式将对象创建进行解耦

1.2.开发流程

  • 添pom.xml中加Spring依赖
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>
  • 编写Spring核心配置文件

resources目录下创建applicationContext.xml文件:

1-2.jpg

beans下添加bean标签:

<!--    UserSerivce的创还能权交给了Spring-->
<bean id="userService" class="com.ioc.demo1.UserServiceImpl"></bean>
  • 在程序中读取Spring配置文件,通过Spring框架获得Bean,完成相应操作

测试代码:

/**
 * Spring的方式实现
 */
@Test
public void sayHelloBySpring(){
    //创建Spring的工厂类
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    //通过工厂获得类
    UserService userService = (UserService) applicationContext.getBean("userService");
    userService.sayHello();
}

运行结果:

Hello Spring

2.DI依赖注入

DI Dependency Injection 依赖注入,就是在Spring创建对象过程中,将这个对象所以来的属性注入进去

2.1.开发流程

  • 在被注入类中添加属性和测试方法

      private String name;
    
      public String getName() {
          return name;
      }
    
      public void setName(String name) {
          this.name = name;
      }
      @Override
      public void sayHelloName() {
          System.out.println("Hello Spring " + name);
      }
    
  • xml中添加依赖注入的配置

      <!--    UserSerivce的创还能权交给了Spring-->
      <bean id="userService1" class="com.ioc.demo1.UserServiceImpl">
          <!--        设置属性-->
          <property name="name" value="李四"></property>
      </bean>
    
  • 测试代码

      /**
       * 测试依赖注入
       */
      @Test
      public void sayHelloBySpring1(){
          //创建Spring的工厂类
          ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
          //通过工厂获得类
          UserService userService = (UserService) applicationContext.getBean("userService1");
          userService.sayHelloName();
      }
    
  • 运行结果

    Hello Spring 李四

3.1.Spring工厂类

1-3.jpg

主要使用的是

  • ApplicationContext接口
  • ClassPathXmlApplicationContext类
  • FileSystemXmlApplicationContext类
  • BeanFactory接口

3.1.1.FileSystemXmlApplicationContext使用

在e盘根目录下创建xml

1-4.jpg

xml配置如下

<!--    UserSerivce的创还能权交给了Spring-->
<bean id="userService1" class="com.ioc.demo1.UserServiceImpl">
    <!--        设置属性-->
    <property name="name" value="磁盘李四"></property>
</bean>

添加测试代码

/**
 * 磁盘加载xml
 */
@Test
public void sayHelloBySpring2(){
    //创建Spring的工厂类
    ApplicationContext applicationContext = new FileSystemXmlApplicationContext("e:\\applicationContext.xml");
    //通过工厂获得类
    UserService userService = (UserService) applicationContext.getBean("userService1");
    userService.sayHelloName();
}

运行结果

Hello Spring 磁盘李四

3.1.2.BeanFactory使用

测试方法

/**
 * 传统方式的工厂类:BeanFactory
 */
@Test
public void sayHelloByFactory(){
    BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
//        BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("e:\\applicationContext.xml"));
    UserService userService = (UserService) beanFactory.getBean("userService1");
    userService.sayHelloName();
}

运行结果:

Hello Spring 磁盘李四

相关文章

网友评论

      本文标题:Spring IOC&DI

      本文链接:https://www.haomeiwen.com/subject/ueiwydtx.html