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的核心思 ...
随机推荐
- Object类中toString()的使用
/* * Object类中toString()的使用: * * 1. 当我们输出一个对象的引用时,实际上就是调用当前对象的toString() * * 2. Object类中toString()的定义 ...
- C#中使用正则将字符串中某字符不区分大小写并按全字匹配替换为空
具体代码如下所示: //将字符串中desc不区分大小写并按全字匹配替换为空 var strText = "CreatDeSce DeSc,UserName AsC"; string ...
- 解决tsc编译器版本过低问题
我们知道,tsc是TypeScript的编译器,可以将TypeScript脚本(.ts文件)编译为JavaScript脚本(.js文件).根据约定,TypeScript脚本文件使用.ts后缀名,Jav ...
- Amoro提供grafana的metrics介绍
一.指标内容 +| Metric Name | Type | Tags | Description | +|---------------------------------------------- ...
- USACO24DEC Cake Game S 题解 [ 黄 ] [ 前缀和 ] [ adhoc ]
Cake Game:小清新前缀和题,但是我场上想了半天优先队列贪心假完了 /ll/ll/ll. 观察 本题有三个重要的结论,我们依次进行观察. 不难发现,第二个牛一定会拿 \(\frac{n}{2}- ...
- FreeSql学习笔记——3.查询
前言 FreeSql中查询的支持非常丰富,包括链式语法,多表查询,表达式函数:写法多种多样,可以使用简单的条件查询.sql查询.联表.子表等方式用于查询数据, 查询的格式也有很丰富,包括单条记录, ...
- Typora Emoji图标
转自: https://www.cnblogs.com/wangjs-jacky/p/12011208.html People :smile: :laughing: :blush: :sm ...
- C语言编程技巧 全局变量在多个c文件中公用的方法
在使用C语言编写程序时,经常会遇到这样的情况:我们希望在头文件中定义一个全局变量,并将其包含在两个不同的C文件中,以便这个全局变量可以在这两个文件中共享.举个例子,假设项目文件夹"proje ...
- Java中List通过Lambda实现排序
目录 1.正常排序,1,2,3 2.倒序 3,2,1 1.正常排序,1,2,3 list=list.stream().sorted(Comparator.comparing(VipCardVo::ge ...
- 浅谈李飞飞巴黎演讲:如果 AI 资源被少数公司垄断,整个生态系统都会完蛋
在巴黎人工智能峰会开幕式上,斯坦福大学教授.人工智能专家李飞飞发表了主题演讲,揭示了人工智能如何从"观察者"转变为重塑世界的"行动者".她在致辞中,分析了&qu ...