0006SpringBoot中@Configuration与@Bean联合使用
需求:将某个普通类做为组件注册到容器中,可通过如下办法
1、定义HelloService类
package springboot_test.springboot_test.service;
public class HelloService {
}
2、定义配置类MyConfig.java
package springboot_test.springboot_test.config; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springboot_test.springboot_test.service.HelloService; @Configuration
public class MyConfig {
//方法名称即为组件的id值
@Bean
public HelloService helloService(){
return new HelloService();
}
}
3、在原有测试类PersonTest.java中增加testHelloService()测试方法
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import springboot_test.springboot_test.SpringbootTestApplication;
import springboot_test.springboot_test.bean.Person; @SpringBootTest(classes = SpringbootTestApplication.class)
@RunWith(SpringRunner.class)
public class PersonTest {
@Autowired
private Person person; @Autowired
private ApplicationContext ac; //在通过@Configuration和@Bean定义MyConfig.java之前,打印值为false,即容器中不存在helloService组件
//在配置之后,打印值为true
@Test
public void testHelloService(){
boolean flag =ac.containsBean("helloService");
System.out.println(flag);
} @Test
public void getPerson(){
System.out.println(person);
}
}
0006SpringBoot中@Configuration与@Bean联合使用的更多相关文章
- 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件
写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...
- Spring中常见的bean创建异常
Spring中常见的bean创建异常 1. 概述 本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...
- @Configuration 和 @Bean
1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...
- 【转】Spring 中三种Bean配置方式比较
今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...
- Springboot@Configuration和@Bean详解
Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...
- spring注解第01课 @Configuration、@Bean
一.原始的 xml配置方式 1.Spring pom 依赖 <dependency> <groupId>org.springframework</groupId> ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- @Configuration与@Bean作用
Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...
- Spring @Configuration 和 @Bean 注解
@Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...
随机推荐
- css中盒子模型与box-sizing属性
盒子模型 w3c标准:定义的width为 内容,有padding,border 都会使得 最终呈现的宽度为 定义的width+padding+border的总和,有margin另加 ie标准:定义的w ...
- 彻底理解js中this的指向,不必硬背(转)
转自: http://www.h5cn.com/js/jishu/2016/0226/18248.html 首先必须要说的是,this的指向在函数定义的时候是确定不了的,只有函数执行的时候才能确定th ...
- python 创建虚拟环境时报错OSError, setuptools下载失败
错误信息如下: Using base prefix 'c:\\users\\huful\\appdata\\local\\programs\\python\\python36-32'New pytho ...
- APP 和小程序中通过日期格式获取时间戳的一个bug
介绍一下背景:业务逻辑就不多说了,就说关键出问题的一步,需要将 2019-10-10 这个格式转换为时间戳.在不同平台不同场景下问题还很怪异 app上:ios 安卓线上的都有问题 ios模拟器没问题 ...
- mysql-系统表的使用
https://blog.csdn.net/wind520/article/details/38728655
- 精选实用 Chrome 扩展(20)
● Reading List 简介:收藏网页,稍后阅读 ● OneTab 简介:收起当前已打开的标签页,需要的时候恢复 ● IE Tab 简介:网页用IE打开 ● uBlock Origin ● Pe ...
- python学习-21 集合 2
集合的其他方法 1.交差补集 math = {'xm','xh','xg','xx'} english ={'xm','xh','dm','john'} print(math.symmetric_di ...
- docker 实践三:操作容器
在学习了 docker 镜像的内容后,我们在来看 docker 的另一个核心点:容器. 注:环境为 CentOS7,docker 19.03 docker 的容器是镜像的一个运行实例.docker 镜 ...
- 怎样在网页中嵌入JS代码
有四种方法: 方法1: 在<script>标签内直接写代码 <body> <button id="btn">click</button&g ...
- C# 重载,重写,代理,枚举实例
1.日期说法时区不同所取到的值也不同, 多个国的服务器要注意这个玩意 DateTime newDate = DateTime.Now; Console.WriteLine(newDate.ToStri ...