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 ... 
随机推荐
- Linux中查找最耗CPU的Java代码问题
			第一步: 查看消耗CPU最高的进程PID [lolaage@web2 tomcat-ns]$ top top - 13:23:32 up 42 days, 19:11, 3 users, load ... 
- ASP.NET请求过程-基本过程
			客户端发送请求到达服务器 输入域名->DNS解析->指定IP 服务器(如果网页直接输入IP地址则直接到服务器)->请求到达服务器->HTTP.SYS接受HTTP请求->转 ... 
- C++浮点数据的输出控制
			#include <iostream> #include <Windows.h> using namespace std; int main(void) { double va ... 
- SAS学习笔记20 CAT函数
- MySQL AND 和 OR 联合使用带来的坑
			MySQL 基础篇 三范式 MySQL 军规 MySQL 配置 MySQL 用户管理和权限设置 MySQL 常用函数介绍 MySQL 字段类型介绍 MySQL 多列排序 MySQL 行转列 列转行 M ... 
- ASP.NET Core本身已经集成了一个轻量级的IOC容器
			1.services.AddTransient<IApplicationService,ApplicationService>//服务在每次请求时被创建,它最好被用于轻量级无状态服务(如我 ... 
- 50道高级sql练习题;大大提高自己的sql能力(附具体的sql)
			问题及描述:--1.学生表Student(SID,Sname,Sage,Ssex) --SID 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别--2.课程表Course(CID ... 
- javaweb常识
			Tomcat下载地址www.apache.org 在电脑中查看java版本:cmd中输入java -version tomcat解压后目录 bin:放可执行文件(如startup.bat shut ... 
- 关闭google默认打开翻译提醒
			关闭google默认打开翻译提醒 在header中添加以下代码: <meta name="google" content="notranslate" /& ... 
- LeetCode 【1】 Two Sum --001
			5月箴言 住进布达拉宫,我是雪域最大的王.流浪在拉萨街头,我是世间最美的情郎.—— 仓央嘉措 从本周起每周研究一个算法,并以swift实现之 001 -- Two Sum (两数之和) 题干英文版: ... 
