**配置类 MyAppConfig **


  1. import com.test.springboot.service.HelloService;
  2. import org.springframework.context.annotation.*;
  3. /**
  4. * @Configuration:注解告诉springboot当前类是一个配置类,是来替代之前的spring配置文件。
  5. * 在配置文件中用<bean></bean>标签添加组件
  6. */
  7. @Configuration
  8. @ComponentScan(basePackages = {"com.test.springboot"})
  9. public class MyAppConfig {
  10. //将方法的返回值添加到容器中,容器中这个组件默认的ID是方法名
  11. @Bean("helloService")
  12. public HelloService helloService() {
  13. System.out.println("配置类@bean给容器中添加组件了");
  14. return new HelloService();
  15. }
  16. }

**HelloService **


  1. public class HelloService {
  2. public void say(String name) {
  3. System.out.println("****helloservice***" + name);
  4. }
  5. }

测试类

  1. import com.test.springboot.bean.Person;
  2. import com.test.springboot.service.HelloService;
  3. import config.MyAppConfig;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11. /**
  12. * springboot单元测试
  13. * 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
  14. */
  15. @RunWith(SpringRunner.class)
  16. @SpringBootTest
  17. public class SpringBoot02ConfigApplicationTests {
  18. @Autowired
  19. Person person;
  20. @Autowired
  21. ApplicationContext ioc;
  22. @Test
  23. public void testHelloService() {
  24. System.out.println("****************************************");
  25. ApplicationContext context = new AnnotationConfigApplicationContext(MyAppConfig.class);
  26. HelloService helloService = (HelloService) context.getBean("helloService");
  27. System.out.println(helloService);
  28. boolean flag = context.containsBean("helloService");
  29. System.out.println("bean是否存在:" + flag);
  30. helloService.say("小明");
  31. }
  32. }

执行结果

  1. 2019-05-08 17:27:32.553 INFO 2588 --- [ main] c.t.s.SpringBoot02ConfigApplicationTests : No active profile set, falling back to default profiles: default
  2. 2019-05-08 17:27:34.786 INFO 2588 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
  3. 2019-05-08 17:27:35.123 INFO 2588 --- [ main] c.t.s.SpringBoot02ConfigApplicationTests : Started SpringBoot02ConfigApplicationTests in 3.134 seconds (JVM running for 4.183)
  4. ****************************************
  5. 配置类@bean给容器中添加组件了
  6. com.test.springboot.service.HelloService@6eaa21d8
  7. bean是否存在:true
  8. ****helloservice***小明
  9. 2019-05-08 17:27:35.741 INFO 2588 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
  10. Process finished with exit code 0

注解描述:

  • @Configuration : 指明当前类是一个配置类来替代之前的Spring配置文件,Spring boot的配置类,相当于Spring的配置文件。

    • Spring,通过配置文件添加组件
    • Spring boot,通过配置类的方式添加组件
  • @ComponentScan :作用就是根据定义的扫描路径,把符合扫描规则的类装配到spring容器中

  • @Bean :将方法的返回值添加到容器中

Springboot配置类的更多相关文章

  1. SpringBoot——配置类实现WebMvcConfigurer接口来配置拦截器、view-controller、视图解析器等

    目的:为了保留SpringBoot对SpringMVC自动配置,另外我们还想要做一些自己拓展的功能 如何做扩展? 以配置view-controller实现跳转为例: 原先在SpringMvc中我们写v ...

  2. Springboot 配置类( @Configuration) 不能使用@Value注解从application.propertyes中加载值以及Environment为null解决方案

    最近遇到个场景,需要在使用@Bean注解定义bean的时候为对象设置一些属性,比如扫描路径,因为路径经常发布新特性的时候需要修改,所以就计划着放在配置文件中,然后通过@ConfigurationPro ...

  3. 【yml】springboot 配置类 yml语法

    参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...

  4. SPringBoot 配置类继承WebMvcConfigurationSupport和实现WebMvcConfigurer的使用

    个人习惯使用  实现的方式 public class WebMvcConfiguration implements WebMvcConfigurer {

  5. springboot的yaml基础语法与取值,配置类,配置文件加载优先级

    1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...

  6. springboot配置cxf

    1.引入两个需要的jar <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf- ...

  7. SpringBoot配置Aop笔记【例子】

    众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...

  8. SpringBoot swagger-ui.html 配置类继承 WebMvcConfigurationSupport 类后 请求404

    1 .SpringBoot启动类加上  注解 @EnableWebMvc @SpringBootApplication@EnableWebMvc public class Application { ...

  9. springboot项目启动之后初始化自定义配置类

    前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...

随机推荐

  1. C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程

    C#串口通讯教程 简化一切 只保留核心功能 这可能是最易于理解的一篇教程   串口的定义,请自行了解. C#操作串口通讯在.Net强大类库的支持下,只需要三个步骤: 1 创建 2 打开 3 发送/接受 ...

  2. 关于java赋值操作的原子性问题

    17.7. Non-Atomic Treatment of double and long For the purposes of the Java programming language memo ...

  3. hdoj 1875 畅通project再续【最小生成树 kruskal &amp;&amp; prim】

    畅通project再续 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其它的小岛时都要通过划小船来实现. ...

  4. ABAP debug遇到问题

    新项目的系统,调试是老出现这个框不断弹出,一堆出来 都来不及关. 不确定是不是因为可用对话框不够的原因.

  5. SPFA 最短路 带负权边的---- 粗了解

    SPFA(Shortest Path Faster Algorithm)是Bellman-Ford算法的一种队列实现,减少了不必要的冗余计算. 算法大致流程是用一个队列来进行维护. 初始时将源加入队列 ...

  6. assign.py

    #链式赋值 m=n=[1,2,3] m[0]=2 print(m,n) #m,n都改变了 x=y='xxx' y='yyy' print(x,y) #只有y 改变 #序列解包 x,y,z=1,2,3 ...

  7. BZOJ_4311_向量_线段树按时间分治

    BZOJ_4311_向量_CDQ分治+线段树按时间分治 Description 你要维护一个向量集合,支持以下操作: 1.插入一个向量(x,y) 2.删除插入的第i个向量 3.查询当前集合与(x,y) ...

  8. data对象转化成后端需要的json格式

    data=JSON.stringify(json_data); $.ajax({type:'post',url:url+'warehouse/create_alliance_out/',data:da ...

  9. 如何下载WDK

    随着Windows Vista和Windows Server 2008的相继发布,微软的驱动开发工具也进行了相应的更新换代.原来的驱动开发工具包叫做DDK(Driver Develpment Kit) ...

  10. HBase之一:HBase原理和设计

    一.简介 HBase —— Hadoop Database的简称,Google BigTable的另一种开源实现方式,从问世之初,就为了解决用大量廉价的机器高速存取海量数据.实现数据分布式存储提供可靠 ...