**配置类 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. Mmseg中文分词算法解析

    Mmseg中文分词算法解析 @author linjiexing 开发中文搜索和中文词库语义自己主动识别的时候,我採用都是基于mmseg中文分词算法开发的Jcseg开源project.使用场景涉及搜索 ...

  2. spring中构造函数注入

    spring中构造函数注入,简单来说,就是通过beans.xml中,设置对应的值.而且通过bean类中的构造函数进行注入这些值. 文件结构 watermark/2/text/aHR0cDovL2Jsb ...

  3. uva 10806 Dijkstra, Dijkstra. (最小费最大流)

    uva 10806 Dijkstra, Dijkstra. 题目大意:你和你的伙伴想要越狱.你的伙伴先去探路,等你的伙伴到火车站后,他会打电话给你(电话是藏在蛋糕里带进来的),然后你就能够跑去火车站了 ...

  4. java之集合Collection 具体解释之4

    package cn.itcast_04; public class Student { private String name; private int age; public Student() ...

  5. 如何在外部采用AES-128对HLS的TS流进行加密

    http://www.wowza.cn/hls/howToSecureHlsByAes128External

  6. python day- 16 面向对象

    1.类的相关知识 类:是指具有相同属性和技能的一类事物. 比如:人类 ,植物类,动物类,狗类. 对象:是类中的某一个实例,是类的具体表现. 比如:具体到某个人,某一个植物,某一条狗. class 是p ...

  7. YTU 2424: C语言习题 字符串比较

    2424: C语言习题 字符串比较 时间限制: 1 Sec  内存限制: 128 MB 提交: 1042  解决: 613 题目描述 写一函数,实现两个字符串的比较.即自己写一个strcmp函数,函数 ...

  8. YTU 2456: 评委打分

    2456: 评委打分 时间限制: 1 Sec  内存限制: 128 MB 提交: 283  解决: 52 题目描述  一个歌唱比赛,比赛每次会从观众中随即抽取几名观众给分(观众至少有5个,分数为0~1 ...

  9. CentOS 7 安装jdk9

    1.下载jdk9 http://download.oracle.com/otn-pub/java/jdk/9.0.4+11/c2514751926b4512b076cc82f959763f/jdk-9 ...

  10. 使用pyinstaller----python转exe

    自己写了一个定时清理日志的脚步,因为服务器是Windows,不想部署Python环境,就考虑到转成exe文件即可. 从网上了解到python转exe方法比较多,我选用了pyinstaller,过程比较 ...