一、环境说明
  1. 项目结构

  2. StudentService

    package com.cookie.service;
    
    /**
    * @author cxq
    * @version 1.0
    * @date 2020/7/14 9:18
    * @desc
    */
    public interface StudentService { void add();
    }
  3. StudentServiceImpl

    package com.cookie.service.impl;
    
    import com.cookie.service.StudentService;
    import org.springframework.stereotype.Component; /**
    * @author cxq
    * @version 1.0
    * @date 2020/7/14 9:20
    * @desc
    */
    public class StudentServiceImpl implements StudentService { public void add() {
    System.out.println(" add student ... ");
    }
    }
  4. applicationContext.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"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> </beans>
二、XML
  1. 以bean的方式在核心配置文件中声明

    <!--
    xml声明
    id : bean的唯一标识
    class:bean所在的Java类的全类名
    -->
    <bean id="studentService" class="com.cookie.service.impl.StudentServiceImpl" />
  2. 通过ClassPathXmlApplicationContext读取配置文件

    /**
    * 基于xml声明bean
    */
    @Test
    public void method1(){
    // 1.获取容器:读取配置文件
    ApplicationContext applicationContext
    = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2.获取bean
    StudentService studentService = (StudentService) applicationContext.getBean("studentService"); // 3.调用对应的方法
    studentService.add();
    }
三、注解扫描
  1. 在核心配置文件中加入要扫描的类

    <!--
    2.注解扫描
    base-package :类所在的包
    -->
    <context:component-scan base-package="com.cookie.service" />
  2. 在对应类上加上@Component将该类放入IOC容器中,并起一个别名

    @Component("studentService")
    public class StudentServiceImpl implements StudentService { public void add() {
    System.out.println(" add student ... ");
    }
    }
  3. 通过ClassPathXmlApplicationContext读取配置文件

    /**
    * 2.注解扫描
    *
    */
    @Test
    public void method2(){
    // 1.获取容器:读取配置文件
    ApplicationContext applicationContext
    = new ClassPathXmlApplicationContext("applicationContext.xml"); // 2.获取bean
    StudentService studentService = (StudentService) applicationContext.getBean("studentService"); // 3.调用对应的方法
    studentService.add();
    }
四、Java类
  1. 创建一个java类CommonConfig

    package com.cookie;
    
    import com.cookie.service.StudentService;
    import com.cookie.service.impl.StudentServiceImpl;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration; /**
    * @author cxq
    * @version 1.0
    * @date 2020/7/14 10:15
    * @desc
    */
    @Configuration // 声明这是一个配置类
    public class CommonConfig { @Bean // 声明bean
    public StudentService studentService(){
    return new StudentServiceImpl();
    }
    }
  2. 通过AnnotationConfigApplicationContext读取该java配置类

     /**
    * 3.基于java类
    *
    */
    @Test
    public void method3(){ ApplicationContext applicationContext
    = new AnnotationConfigApplicationContext(CommonConfig.class); StudentService studentService = (StudentService) applicationContext.getBean("studentService"); studentService.add();
    }

Spring Bean的声明方式的更多相关文章

  1. Spring Bean 的装配方式

    Spring Bean 的装配方式 装配 Bean 的三种方式 一个程序中,许多功能模块都是由多个为了实现相同业务而相互协作的组件构成的.而代码之间的相互联系又势必会带来耦合.耦合是个具有两面性的概念 ...

  2. Spring———bean的创建方式,注入方式,复杂类型注入 概括

    Spring相关概念和类    1.IOC             inverse of control    控制反转   反转了创建对象的方式            以前:new 对象,管理和维护 ...

  3. Spring bean实例化的方式

    实例化过程如图,方式如图. 甩代码. 方式一:构造方法 搞一个bean,修改一下xml配置 package com.itheima.instance.constructor; public class ...

  4. springmvc学习指南 之---第25篇 Spring Bean有三种配置方式

    writed by不要张艳涛, 从tomcat转到了springmvc 现在开始有点不知道该看什么书了,看完了springmvc 学习指南之后 又查了一些书,好多都是内容相近,在找书的过程之中,发现s ...

  5. spring Bean的三种配置方式

    Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...

  6. Spring bean的bean的三种实例化方式

     Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...

  7. spring bean 的作用域之间有什么区别

    spring bean 的作用域之间有什么区别? spring容器中的bean可以分为五个范围.所有范围的名称都是说明的, 1.singleton:这种bean范围是默认的,这种范围确保不管接受到多个 ...

  8. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

  9. Spring Bean 生命周期之destroy——终极信仰

    上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...

  10. Spring基础——IOC九种bean声明方式

    Spring简介 Spring不是服务于开发web项目的功能,或业务.而是服务于项目的开发,方便各层间的解耦调用,方便对类的批量管理,是提高软件开发效率,降低后期维护成本的框架. Spring的核心思 ...

随机推荐

  1. Pycharm:鼠标滚动控制字体大小

    Pycharm字体放大的设置 1.File -> setting -> Keymap ->在搜寻框中输入:increase -> Increase Font Size(双击) ...

  2. Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)

    题目链接:Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2) 总结:Bwa两发,C读假题.发挥很一般,补题感觉到E都是能做的,红温. A. ...

  3. 深入理解 Future, CompletableFuture, ListenableFuture,回调机制

    深入理解 Future, CompletableFuture, ListenableFuture,回调机制 本文禁止转载. 本文从设计思想.具体实现等角度分析了 Future.CompletableF ...

  4. 同步工具-腾讯EMR表治理工具安装使用

    一.安装 1.root用户上传文件 cd wangrz -bey luoshu-1.0-bin.tar.gz 2.解压文件到服务目录 重新安装洛书需执行:rm -rf /usr/local/servi ...

  5. Codeforces 1536B Prinzessin der Verurteilung 题解 [ 紫 ] [ 后缀自动机 ] [ 动态规划 ] [ 拓扑排序 ]

    Prinzessin der Verurteilung:最短未出现字符串的板子. 思路 考虑在 SAM 上 dp,定义 \(dp_i\) 表示从 \(i\) 节点走到 NULL 节点所花费的最少步数. ...

  6. 海康SDK报错Structure.getFieldOrder()

    就是你调用的这个结构体以及其引用的其他结构体,可能没有getFieldOrder()的方法,你只要按照顺序把他填上去就好了.比如 public static class NET_DVR_TIME ex ...

  7. Vue3条件与列表渲染深度解析:实战技巧助你高效开发复杂界面

    一.条件渲染的高阶应用 1.1 多分支条件渲染(v-if/v-else-if/v-else) <!-- 评分等级展示案例 --> <div v-if="score > ...

  8. 【编程思想】C# delegate 委托的本质:方法对象的应用

    一.前言 翻回之前写的博客,前期写的结构确实差很多, 这次细看了<委托那些事(一).(二)>,忍不住重新写一下,之前把简单的事情复杂化了. 为什么现在思维不一样了,有一点我认为是见识的计算 ...

  9. PPT图片搭配

  10. (C++实现)2-NAF

    (C++实现)2-NAF 前言 ‍ 任何一个非负整数,都有一个唯一的 NAF (Non-adjacent form) 表示. 因着课程的缘由,我不得不研究一下 NAF 是怎么实现的,也是现学现用. ‍ ...