需求:将某个普通类做为组件注册到容器中,可通过如下办法

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联合使用的更多相关文章

  1. 【Spring注解开发】组件注册-使用@Configuration和@Bean给容器中注册组件

    写在前面 在之前的Spring版本中,我们只能通过写XML配置文件来定义我们的Bean,XML配置不仅繁琐,而且很容易出错,稍有不慎就会导致编写的应用程序各种报错,排查半天,发现是XML文件配置不对! ...

  2. Spring中常见的bean创建异常

    Spring中常见的bean创建异常 1. 概述     本次我们将讨论在spring中BeanFactory创建bean实例时经常遇到的异常 org.springframework.beans.fa ...

  3. @Configuration 和 @Bean

    1. @Bean: 1.1 定义 从定义可以看出,@Bean只能用于注解方法和注解的定义. @Target({ElementType.METHOD, ElementType.ANNOTATION_TY ...

  4. 【转】Spring 中三种Bean配置方式比较

    今天被问到Spring中Bean的配置方式,很尴尬,只想到了基于XML的配置方式,其他的一时想不起来了,看来Spring的内容还没有完全梳理清楚,见到一篇不错的文章,就先转过来了. 以前Java框架基 ...

  5. Springboot@Configuration和@Bean详解

    Springboot@Configuration和@Bean详解 一.@Configuration @Target({ElementType.TYPE}) @Retention(RetentionPo ...

  6. spring注解第01课 @Configuration、@Bean

    一.原始的 xml配置方式 1.Spring pom 依赖 <dependency> <groupId>org.springframework</groupId> ...

  7. Spring的Java配置方式—@Configuration和@Bean实现Java配置

    Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...

  8. @Configuration与@Bean作用

    Spring的Java配置方式是通过@Configuration和@Bean这两个注解来实现 @Configuration可以作用在任意类上,表示该类是一个配置类,其实就相当于一个xml配置文件. @ ...

  9. Spring @Configuration 和 @Bean 注解

    @Configuration 和 @Bean 注解 带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源.@Bean 注解告诉 Spri ...

随机推荐

  1. CenOS 7 安装Redis

    1.Redis介绍 Redis是一个使用ANSI C编写的开源.支持网络.基于内存.可选持久性的键值对存储数据库.从2015年6月开始,Redis的开发由Redis Labs赞助,而2013年5月至2 ...

  2. 第三坑:jar包编译版本

    这个是之前往was上发应用的时候踩的一个坑,当时我们知道was的jdk版本是1.6,然后我们是用1.7的jdk,编译版本选的是1.6,然后放上去不对,我们以为是编译的问题,然后又下载了1.6的jdk, ...

  3. python学习-14 基本数据类型3

    1.字符串 获取字符串的字符,例如: test = 'abcd' a= test[0] # 通过索引,下标,获取字符串中的某一个字符 print(a) b = test[0:1] # 通过下标的 范围 ...

  4. 数据类型 _python

    字符串 str # a ="asd bfg" # print(a.capitalize()) #首字母大写 # print(a.title()) #每个单词首字母大写 # prin ...

  5. Codefroces 1245 F. Daniel and Spring Cleaning

    传送门 考虑简单的容斥 设 $F(n,m)$ 表示 $a \in [1,n] , b \in [1,m]$ 的满足 $a+b=a \text{ xor } b$ 的数对的数量 那么答案即为 $F(r, ...

  6. (七)Activiti之历史活动查询和历史任务查询和流程状态查询

    一.历史活动查询 本章案例是基于上一章节案例的基础上,流程走完后进行测试的,也就是下图的流程从学生请假到班主任审批都已经完成,本章用来测试查询历史活动和历史任务的 activiti5的历史活动包括所有 ...

  7. sys.dm_exec_query_stats的total_worker_time的单位是微秒还是毫秒

    该视图sys.dm_exec_query_stats存放的就是当前所有执行计划的详细信息,比如某条执行计划共占CPU多少等等.因为该视图对编译次数.占用CPU资源总量.执行次数等都进行了详细的记录,所 ...

  8. 记录我第一篇用Markdown写的Blog

    Markdown的介绍 喝水不忘挖井人-Markdown的创造者 Markdown 最初是由 John Gruber 和 Aaron Swartz 于 2004 年共同设计的(在这里插一句,Aaron ...

  9. C#多线程的简单理解

    一.CLR线程池基础 创建和销毁线程是一个昂贵的操作,所以CLR管理了一个线程池(thread pool),可以将线程池看成一个黑盒. CLR初始化时,线程池中是没有线程的.线程的初始化与其他线程一样 ...

  10. 封装jquery的ajax

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...