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(); }
  • 相关阅读:
    将中文字符串分割为数组 解决str_split中文乱码php
    %25%37%DD 。。。上述形式不是乱码。 这是urlencoding。 你可以使用js内置的方法 encodeURIComponent进行编码, 而使用decodeURIComponent把上述形式再解码为普通字符
    浏览器 cookie session
    javascript:void(0)与#区别
    $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)
    HTML <meta> 标签 遇到<meta http-equiv="refresh" content="0; url=">详解
    HBuilder的默认工作空间的修改
    Spring和Mybatis的整合
    嵌套查询--------关联一对多关系----------collection
    InputStream和OutputStream的一遍博客 分析非常到位
  • 原文地址:https://www.cnblogs.com/datapool/p/7039614.html
Copyright © 2011-2022 走看看