zoukankan      html  css  js  c++  java
  • Spring_day01

    Spring的核心机制: 依赖注入

    Spring框架的核心功能有两个:

      1. Spring容器作为超级大工厂,负责创建,管理所有的Java对象,这些Java对象被称为Bean;

      2. Spring容器管理容器中Bean之间的依赖关系,Spring使用一种被称为"依赖注入"的方式来管理Bean之间的依赖关系.

    使用Spring框架之后,调用者无须主动获取被依赖对象,调用者只要被动接受Spring容器为调用者的成员变量赋值即可.

    使用Spring框架之后的两个主要改变是:

      1.程序无须使用new调用构造器去创建对象.所有的Java对象都可以交给Spring容器去创建;

      2.当调用者需要调用被依赖对象的方法时,调用者无须主动获取被依赖对象,只需等待Spring容器注入即可.

    依赖注入通常有两种方式:

      1. setter方法注入

        IoC 容器使用成员变量的setter方法来注入被依赖对象

      2. 构造器注入

        IoC容器使用构造器来注入被依赖对象

    Person.java

    package com.itheima.app;
    
    public interface Person {
        public void useAxe();
    }

    Chinese.java

    package com.itheima.app.impl;
    
    import com.itheima.app.Axe;
    import com.itheima.app.Person;
    
    public class Chinese implements Person {
        
        private Axe axe;
        
        // setter方法注入所需的setter方法
        public void setAxe(Axe axe){
            this.axe = axe;
        }
        
        // 实现Person接口的useAxe()方法
        @Override
        public void useAxe() {
            // 调用axe的chop()方法
            // 表明Person对象依赖于axe对象
            System.out.println(axe.chop());
        }
        
    }

    Axe.java

    package com.itheima.app;
    
    public interface Axe {
        public String chop();
    }

    StoneAxe.java

    package com.itheima.app.impl;
    
    import com.itheima.app.Axe;
    
    public class StoneAxe implements Axe {
    
        @Override
        public String chop() {
            return "石斧砍柴好慢";
        }
    
    }

    beans.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">
      
      <!-- bean元素驱动Spring调用构造器创建对象-->
    <bean id="chinese" class="com.itheima.app.impl.Chinese">
        
          <!-- property子元素驱动Spring执行setAxe()方法 --> <property name="axe" ref="stoneAxe"></property> </bean> <bean id="stoneAxe" class="com.itheima.app.impl.StoneAxe"></bean> </beans>

    BeanTest.java

    @Test
        public void test3(){
          // 创建Spring容器 ApplicationContext ctx
    = new ClassPathXmlApplicationContext("beans.xml");

          // 获取Chinese实例
    // Chinese p = ctx.getBean("chinese",Chinese.class) Chinese p = (Chinese) ctx.getBean("chinese"); // 调用useAxe()方法
         p.useAxe(); }
  • 相关阅读:
    Codeforces 1045C Hyperspace Highways (看题解) 圆方树
    Codeforces 316E3 线段树 + 斐波那切数列 (看题解)
    Codeforces 803G Periodic RMQ Problem 线段树
    Codeforces 420D Cup Trick 平衡树
    Codeforces 295E Yaroslav and Points 线段树
    Codeforces 196E Opening Portals MST (看题解)
    Codeforces 653F Paper task SA
    Codeforces 542A Place Your Ad Here
    python基础 异常与返回
    mongodb 删除
  • 原文地址:https://www.cnblogs.com/datapool/p/7039614.html
Copyright © 2011-2022 走看看