Springboot配置类
**配置类 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配置类的更多相关文章
- SpringBoot——配置类实现WebMvcConfigurer接口来配置拦截器、view-controller、视图解析器等
目的:为了保留SpringBoot对SpringMVC自动配置,另外我们还想要做一些自己拓展的功能 如何做扩展? 以配置view-controller实现跳转为例: 原先在SpringMvc中我们写v ...
- Springboot 配置类( @Configuration) 不能使用@Value注解从application.propertyes中加载值以及Environment为null解决方案
最近遇到个场景,需要在使用@Bean注解定义bean的时候为对象设置一些属性,比如扫描路径,因为路径经常发布新特性的时候需要修改,所以就计划着放在配置文件中,然后通过@ConfigurationPro ...
- 【yml】springboot 配置类 yml语法
参考:https://www.runoob.com/w3cnote/yaml-intro.html YAML 是 "YAML Ain't a Markup Language"(YA ...
- SPringBoot 配置类继承WebMvcConfigurationSupport和实现WebMvcConfigurer的使用
个人习惯使用 实现的方式 public class WebMvcConfiguration implements WebMvcConfigurer {
- springboot的yaml基础语法与取值,配置类,配置文件加载优先级
1.基本语法k:(空格)v:表示一对键值对(一个空格必须有):以空格的缩进来控制层级关系:只要是左对齐的一列数据,都是同一个层级的属性和值也是大小写敏感: server: port: 8081 pat ...
- springboot配置cxf
1.引入两个需要的jar <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf- ...
- SpringBoot配置Aop笔记【例子】
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- SpringBoot swagger-ui.html 配置类继承 WebMvcConfigurationSupport 类后 请求404
1 .SpringBoot启动类加上 注解 @EnableWebMvc @SpringBootApplication@EnableWebMvc public class Application { ...
- springboot项目启动之后初始化自定义配置类
前言 今天在写项目的时候,需要再springboot项目启动之后,加载我自定义的配置类的一些方法,百度了之后特此记录下. 正文 方法有两种: 1. 创建自定义类实现 CommandLineRunner ...
随机推荐
- ORACLE 8i 遇到报错:ORA-01631: max # extents (505) reached in table
近期在客户的一个8i生产库上使用statspack.发现alert中有报错: Mon Jun 16 13:17:52 2014 Errors in file /oracle/8.1.7/admin/p ...
- python调用nmap进行扫描
#coding=utf-8 import nmap import optparse import threading import sys import re ''' 需安装python_nmap包, ...
- 5. TCP客户/服务器程序示例
signal 信号是一种软件中断,异步发生,在进程运行的时候随时可能发生.信号可以: 由一个进程发给另一个进程,或发给自身 由内核发给某个进程 信号的action: signal handler,在信 ...
- apt仓库以及apt-get分析
1 debian repository 参考:https://wiki.debian.org/DebianRepository 1.1 版本代号 sid,still in development,该版 ...
- 0mq
- 文件批量转换成UTf-8
yum install -y enca 在文件夹根目录下面创建文件:iconv_shell.sh 里面填写下面的内容: #!/bin/bash for file in `find ./ -name ' ...
- (C)strcpy ,strncpy与strlcpy
1. 背景 好多人已经知道利用strncpy替代strcpy来防止缓冲区越界. 但是如果还要考虑运行效率的话,也许strlcpy是一个更好的方式. 2. strcpy strcpy 是依据 /0 作为 ...
- 优秀Java程序员必须了解的GC工作原理
一个优秀的Java程序员必须了解GC的工作原理.如何优化GC的性能.如何与GC进行有限的交互,因为有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率 ,才能提高整个应 ...
- 解决virtualbox安装增强工具失败的问题
virtualbox有个增强工具,安装之后用户体验是非常爽的.但是有些时候在安装增强工具会遇到一些小问题,无非是没有安装gcc,make之类的编译工具或是需要安装kernel*.而我遇到的问题在做了这 ...
- YTU 2391: 求素数
2391: 求素数 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 3 题目描述 设计一个程序,输出所有小于等于n(n为一个大于2的正整数)的素数. 要求:(1)每行输出 ...