Spring Bean的声明方式
一、环境说明
项目结构

StudentService
package com.cookie.service; /**
* @author cxq
* @version 1.0
* @date 2020/7/14 9:18
* @desc
*/
public interface StudentService { void add();
}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 ... ");
}
}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
以bean的方式在核心配置文件中声明
<!--
xml声明
id : bean的唯一标识
class:bean所在的Java类的全类名
-->
<bean id="studentService" class="com.cookie.service.impl.StudentServiceImpl" />
通过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();
}
三、注解扫描
在核心配置文件中加入要扫描的类
<!--
2.注解扫描
base-package :类所在的包
-->
<context:component-scan base-package="com.cookie.service" />
在对应类上加上@Component将该类放入IOC容器中,并起一个别名
@Component("studentService")
public class StudentServiceImpl implements StudentService { public void add() {
System.out.println(" add student ... ");
}
}
通过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类
创建一个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();
}
}通过AnnotationConfigApplicationContext读取该java配置类
/**
* 3.基于java类
*
*/
@Test
public void method3(){ ApplicationContext applicationContext
= new AnnotationConfigApplicationContext(CommonConfig.class); StudentService studentService = (StudentService) applicationContext.getBean("studentService"); studentService.add();
}
Spring Bean的声明方式的更多相关文章
- Spring Bean 的装配方式
Spring Bean 的装配方式 装配 Bean 的三种方式 一个程序中,许多功能模块都是由多个为了实现相同业务而相互协作的组件构成的.而代码之间的相互联系又势必会带来耦合.耦合是个具有两面性的概念 ...
- Spring———bean的创建方式,注入方式,复杂类型注入 概括
Spring相关概念和类 1.IOC inverse of control 控制反转 反转了创建对象的方式 以前:new 对象,管理和维护 ...
- Spring bean实例化的方式
实例化过程如图,方式如图. 甩代码. 方式一:构造方法 搞一个bean,修改一下xml配置 package com.itheima.instance.constructor; public class ...
- springmvc学习指南 之---第25篇 Spring Bean有三种配置方式
writed by不要张艳涛, 从tomcat转到了springmvc 现在开始有点不知道该看什么书了,看完了springmvc 学习指南之后 又查了一些书,好多都是内容相近,在找书的过程之中,发现s ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- Spring bean的bean的三种实例化方式
Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean ...
- spring bean 的作用域之间有什么区别
spring bean 的作用域之间有什么区别? spring容器中的bean可以分为五个范围.所有范围的名称都是说明的, 1.singleton:这种bean范围是默认的,这种范围确保不管接受到多个 ...
- Spring 学习笔记(2) Spring Bean
一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...
- Spring Bean 生命周期之destroy——终极信仰
上一篇文章 Spring Bean 生命周期之我从哪里来 说明了我是谁? 和 我从哪里来? 的两大哲学问题,今天我们要讨论一下终极哲学我要到哪里去? 初始化 Spring Bean 有三种方式: @P ...
- Spring基础——IOC九种bean声明方式
Spring简介 Spring不是服务于开发web项目的功能,或业务.而是服务于项目的开发,方便各层间的解耦调用,方便对类的批量管理,是提高软件开发效率,降低后期维护成本的框架. Spring的核心思 ...
随机推荐
- SQL Server 内存占用高分析及解决办法(超详细)
SQL Server 内存占用高分析及解决办法(超详细) 一.问题 1.1.SQL Server内存占用高 ,内存不释放 1.2.SQL Server 内存使用策略 SQL Server对服务器内存的 ...
- superset 1.3版本WIN10安装实录
首先说下,为什么要这么做,因为二开需要,二开要有源码,然后对源码修改,编译,所以不能通过类似https://zhuanlan.zhihu.com/p/271695878这种方式,直接安装: 1.去Gi ...
- 记一次LLVM平行宇宙修包实战
最近加入了LLVM平行宇宙计划小组,在小组内提交了一定数量的PR.这个计划究竟是做什么的呢?LLVM平行宇宙计划是基于LLVM技术栈构建openEuler软件包,大白话讲就是原本一个软件包是用gc ...
- NetCore.Encrypt —— 整合加密
前言 最近呢又接触到加密了,回顾之前用到的加密经历,使用过DES.RSA.MD5.BASE64,前面也更新过两篇加密的文章,MD5加密和DES加密.之前的使用都是在.Net Framework平台,这 ...
- vue路由$router.push()的三种传参方式
- CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
CSnakes 是一个用于在.NET项目中嵌入Python代码的工具,由.NET源生成器和运行时组成,能够实现高效的跨语言调用,Github:https://github.com/tonybalone ...
- autMan奥特曼机器人-青龙运行结果推送到autMan
一.使用到的autMan云插件为"青龙推送autMan"或"JD未来活动定时运行" 二选一即可,两都不可同时安装,有冲突 青龙推送autMan:这个插件仅用于将 ...
- 浅谈Processing中的 println() 打印输出函数[String]
简单看一下Processing中的打印输出函数println()相关用法. 部分源码学习 /** * ( begin auto-generated from println.xml ) * * Wri ...
- Vitepress+EdgeOne Pages快速迁移旧网站内容
Vitepress+EdgeOne Pages快速迁移旧网站内容 目录 Vitepress+EdgeOne Pages快速迁移旧网站内容 下载旧网站文章.图片 网站文章转Markdown Vitepr ...
- vue-element-admin安装趟坑
1.下载源码 2.执行 npm install --registry=https://registry.npm.taobao.org 如果遇到"git ls-remote -h -t&quo ...