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 ...
随机推荐
- 让你的eclipse实现写JAVA代码,HTML,CSS,JAVASCRIPT代码提示
1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist 改动Auto Activation triggers for java的值为:zj ...
- Python 包的制作(__init__.py)
如何制作一个自己的包:首先,需要创建一个文件夹,将其作为顶层包,在此文件夹内我们可以定义各个不同的子文件夹与 .py 文件作为各个子包与模块注意:在每个包文件夹下都需要有一个 __init__.py ...
- aop中获取方法的注解
@Around(value="@annotation(apiLog)") public Object around(ProceedingJoinPoint pjp, ApiLog ...
- POJ2942 Knights of the Round Table 点双连通分量 二分图判定
题目大意 有N个骑士,给出某些骑士之间的仇恨关系,每次开会时会选一些骑士开,骑士们会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件:1.任意相互憎恨的两个骑士不能相邻.2.开会人数为大于2的奇 ...
- elastica安装
https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html
- (linux)wake_lock机制
Android的休眠唤醒主要基于wake_lock机制,只要系统中存在任一有效的wake_lock,系统就不能进入深度休眠,但可以进行设备的浅度休眠操作.wake_lock一般在关闭lcd.tp但 ...
- bzoj3090: Coci2009 [podjela]
这个范围明显树包的 然而值并不滋磁 想了一会发现可以带一维当前子树用了多少边,搞定当前向上还能送多少 然后发现会有搞不定的情况,要向上传负数 每次都要重新初始化,负数强制要要 #include< ...
- Silverlight DataBinding Converter:根据binding对象调整显示
Silverlight DataBinding Converter:根据binding对象调整显示 我希望写一系列关于Silverlight DataBinding的文章,分别讲解Silverligh ...
- 小米2在Eclipse 调试,要注意下列步骤。(转)
小米2在Eclipse 调试,要注意下列步骤.1.连接线,打开设置:USB线连接小米2,在设置-->开发者选项->USB 调是打开.如果这一步,就业在Eclipse中真机调试,下面的步骤不 ...
- Lucene 的四大索引查询 ——bool 域搜索 通配符 范围搜索
Lucene 的四大索引查询 清单1:使用布尔操作符 Java代码 //Test boolean operator blic void testOperator(String indexD ...