**配置类 MyAppConfig **


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

**HelloService **


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

测试类

import com.test.springboot.bean.Person;
import com.test.springboot.service.HelloService;
import config.MyAppConfig;
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.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.junit4.SpringRunner; /**
* springboot单元测试
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*/ @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests { @Autowired
Person person;
@Autowired
ApplicationContext ioc; @Test
public void testHelloService() {
System.out.println("****************************************");
ApplicationContext context = new AnnotationConfigApplicationContext(MyAppConfig.class);
HelloService helloService = (HelloService) context.getBean("helloService");
System.out.println(helloService);
boolean flag = context.containsBean("helloService");
System.out.println("bean是否存在:" + flag); helloService.say("小明");
}
}

执行结果

2019-05-08 17:27:32.553  INFO 2588 --- [           main] c.t.s.SpringBoot02ConfigApplicationTests : No active profile set, falling back to default profiles: default
2019-05-08 17:27:34.786 INFO 2588 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
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)
****************************************
配置类@bean给容器中添加组件了
com.test.springboot.service.HelloService@6eaa21d8
bean是否存在:true
****helloservice***小明
2019-05-08 17:27:35.741 INFO 2588 --- [ Thread-2] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 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. 【转载】细聊分布式ID生成方法

    一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id (2)订单标识:order-id (3)帖子标识:tiezi-id 这个记录标识往往就是数据 ...

  2. MCE----Machine-check exception

    http://en.wikipedia.org/wiki/Machine_Check_Exception Machine-check exception From Wikipedia, the fre ...

  3. Django-权限信息自定义标签

    自定义权限标签: import re from django.template import Library from django.conf import settings register = L ...

  4. Pycharm下运行程序查看每个变量的值的方法(类似于Spyder和MATLAB)

    昨天,用了大量篇幅讲了Spyder的各种问题,之所以要用Spyder,最重要的一个原因就是能够非常方便的查看中间变量的值.类似MATLAB的工作空间,非常方便.如下图所示: 但是Spyder的代码自动 ...

  5. 阿里云安装nginx 启动失败的原因。

    阿里云编译安装nginx服务器后启动一直报下面错误. 百度了一圈,看到一个说要先关掉apache服务,感觉这个好像是对的,立马做了下面操作. 果然把nginx起了起来. 从这边才知道apache和ng ...

  6. 检測磁盘驱动的健康程度SMART

    在server中,全部组件中一般最easy坏掉的就是磁盘.所以一般採取RAID来保证系统的稳定性,通过冗余磁盘的方式防止磁盘故障. 现代硬件驱动器一般支持SMART(自我监測分析和报告技术),它可以监 ...

  7. liberOJ #6173. Samjia 和矩阵 hash+后缀数组

    #6173. Samjia 和矩阵 题目链接  : 点这里 题目描述 给你一个只包含大写字母的矩阵,求有多少本质不同的子矩阵. 输入格式 第一行包含两个整数 nnn , mmm ,表示矩阵 nnn 行 ...

  8. C标准库中atoi的一种可能的实现

    为避免与标准库中的atoi产生歧义, 我将自己编写的函数命名为strToInt, 以下是示例代码 #include <stdio.h> int strToInt(const char *s ...

  9. MTK平台下Battery驱动分析

    主要涉及代码: Kernel: kernel-3.10\drivers\power\mediatek\ kernel-3.10\drivers\misc\mediatek\mach\mt6580\&l ...

  10. /dev下添加设备节点的方法步骤(通过device_create)

    将自己开发的内核代码加入到Linux内核中,需要3个步骤: 1.确定把自己开发代码放入到内核合适的位置 将demo_chardev.c文件拷贝到.../drivers/char/目录下. demo_c ...