springBoot加载

@Configuration

表明该类是一个配置类常常配合@Bean使用,让容器管理对象

@Configuration(proxyBeanMethods = true)

proxyBeanMethods = true表示@Configuration注解的类走不走代理,如果走代理会从单例池拿对象,拿到的对象是同一个,不走代理不会从单例池拿对象

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. }

@Bean

将指定类交给spring容器管理

  1. @SpringBootApplication
  2. public class TestSpringBootAnnoApplication {
  3. @Bean
  4. public UserService userService(){
  5. return new UserService();
  6. }
  7. public static void main(String[] args) {
  8. ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
  9. UserService userService = context.getBean(UserService.class);
  10. System.out.println(userService);
  11. }
  12. }

@Lazy

类的懒加载,调用时加载类并交给spring管理。

@Scope

指定交给spring容器管理的bean对象是单例或是多例的

@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)

@Scope("singleton")单例(默认) @Scope("prototype")多例

  1. @SpringBootApplication
  2. public class TestSpringBootAnnoApplication {
  3. @Bean
  4. @Scope("prototype")
  5. public UserService userService(){
  6. return new UserService();
  7. }
  8. public static void main(String[] args) {
  9. ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
  10. UserService userService = context.getBean(UserService.class);
  11. UserService userService2 = context.getBean(UserService.class);
  12. System.out.println(userService==userService2);
  13. }
  14. }

spring.factories

springboot在启动时会先寻找resource/META-INFO文件夹下有没有spring.factories文件,如果有就会解析,注入相应的类

META-INFO

  1. #Auto
  2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  3. com.example.testspringbootanno.service.UserService

@ComponentScan

定义spring容器扫描的路径,扫描的spring注解才会被解析

@Profile

指定注入到spring容器的Bean生效的范围

  1. spring.profiles.active=dev
  1. @SpringBootApplication
  2. public class TestSpringBootAnnoApplication {
  3. @Bean
  4. @Scope("prototype")
  5. @Profile("dev")
  6. public UserService userService(){
  7. return new UserService();
  8. }
  9. public static void main(String[] args) {
  10. ConfigurableApplicationContext context = SpringApplication.run(TestSpringBootAnnoApplication.class, args);
  11. UserService userService = context.getBean(UserService.class);
  12. UserService userService2 = context.getBean(UserService.class);
  13. System.out.println(userService==userService2);
  14. }
  15. }

@ConditionalOnBean

满足spring容器有某个springBean对象条件时把该对象交给spring管理。

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnBean(name = "oderService")
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }
  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnBean(OrderService.class)
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnMissingBean

满足spring容器没有某个springBean对象条件时把该对象交给spring管理。

  1. @Configuration
  2. public class Appconfig {
  3. //@Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnMissingBean(OrderService.class)
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnSingleCandidate

是否符合指定的类型只有一个

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. public OrderService oderService1(){
  9. return new OrderService();
  10. }
  11. @Bean
  12. @ConditionalOnSingleCandidate(OrderService.class)
  13. public UserService userService(){
  14. return new UserService();
  15. }
  16. }

@ConditionalOnClass

存在某个class生效

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnClass(name = "com.example.testspringbootanno.service.OrderService")
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnMissingClass

不存在某个class生效

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnMissingClass("com.example.testspringbootanno.service.OrderService")
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnExpression

根据表达式结果判断是否生效

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnExpression("${test.condition}==true")
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnJava

指定的java版本生效

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnJava(JavaVersion.ELEVEN)
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnWebApplication

Web环境生效

@ConditionalOnNotWebApplication

不是Web环境生效

@ConditionalOnProperty

判断配置文件中某个key是否存在

  1. test.condition=true
  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnProperty(name = "test.condition")
  9. //@ConditionalOnProperty(name = "test1.condition",matchIfMissing = true) 确实了照样匹配
  10. public UserService userService(){
  11. return new UserService();
  12. }
  13. }

@ConditionalOnResource

某个资源存在时生效、

  1. @Configuration
  2. public class Appconfig {
  3. @Bean
  4. public OrderService oderService(){
  5. return new OrderService();
  6. }
  7. @Bean
  8. @ConditionalOnResource(resources = "http://www.baidu.com")
  9. public UserService userService(){
  10. return new UserService();
  11. }
  12. }

@ConditionalOnWarDeployment

应用是war包时生效

@ConditionalOnCloudPlatform

应用在某个云平台时生效

读取配置文件

方法一:

  1. @Component
  2. @ConfigurationProperties(prefix = "user")
  3. public class Aconfig {
  4. private String username;
  5. private String password;
  6. }

方法二:

  1. @ConfigurationProperties(prefix = "user")
  2. public class Aconfig {
  3. private String username;
  4. private String password;
  5. }
  1. @Configuration
  2. @EnableConfigurationProperties(Aconfig.class)
  3. public class Appconfig {
  4. }

还可以扫描,用于properties文件过多统一管理

  1. @Configuration
  2. @ConfigurationPropertiesScan("com.zhuoyue.properties")
  3. public class Appconfig {
  4. }

配置文件加载顺序

JVM环境变量(Envionment variables)>电脑环境变量>application.properties>application.yml

SpringBoot加载相关注解的更多相关文章

  1. Springboot 加载配置文件源码分析

    Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...

  2. struts2是如何加载相关的package元素节点信息的

    这不是一篇纯技术文章,而是一篇分享我个人在前后端分离路上收获的点点滴滴的文章,以此来为准备尝试前后端分离或者想了解前后端分离的童鞋做一个大体的讲解. 上一家公司是家小公司,做了一年的全栈开发,对前端的 ...

  3. [翻译][MVC 5 + EF 6] 7:加载相关数据

    原文:Reading Related Data with the Entity Framework in an ASP.NET MVC Application 1.延迟(Lazy)加载.预先(Eage ...

  4. Entity Framework加载相关实体——延迟加载Lazy Loading、贪婪加载Eager Loading、显示加载Explicit Loading

    Entity Framework提供了三种加载相关实体的方法:Lazy Loading,Eager Loading和Explicit Loading.首先我们先来看一下MSDN对三种加载实体方法的定义 ...

  5. Volley 图片加载相关源码解析

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47721631: 本文出自:[张鸿洋的博客] 一 概述 最近在完善图片加载方面的 ...

  6. [翻译 EF Core in Action 2.4] 加载相关数据

    Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...

  7. SpringBoot加载子模块配置文件的方法

    这两天开始学习SpringBoot框架,按照官方的文档,很轻易地就把单模块的项目启动了,但在使用maven搭建多模块的时候遇到了子模块配置文件没有加载的问题 项目架构是这样的 zero |-ws |- ...

  8. SpringBoot加载配置文件(@PropertySource@importSource@Value)

    情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...

  9. springboot加载application.yml文件null

    话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...

  10. springboot加载bean过程探索

    springboot一般通过以下main方法来启动项目 @SpringBootApplication public class DemoApplication { public static void ...

随机推荐

  1. 看到项目中的DateTimeFormat和JsonFormat就头大

    刚来这家公司的时候, 发现很多同事还在用这种方式写代码 当时以为是偶然, 刚才在群里发现还有好多人在交流应当加哪些注解, 声明时区问题. 当写一个东西感到麻烦的时候, 那么大概率是有低成本的更优解的 ...

  2. python常用数据结构模块--collections

    import collections ''' python常用数据结构模块--collections collections是日常工作中的重点.高频模块,常用类型有: 计数器(Counter) 双向队 ...

  3. 13-之容器资源需求、资源限制及Metric-server(Heapster)

    目录 容器资源需求.资源限制及Heapster Heapster 资源指标API及自定义指标API k8s-promtheus监控部署 node-exporter prometheus kube-st ...

  4. centos7.6安装rz命令上传文件

    centos7.6安装rz命令 1.执行安装命令:yum -y install lrzsz 2.进行上传操作:rz   跳转到本地文件选择后即可上传

  5. python中的KeyError报错

    from util import str_util #业务逻辑:兼容不同的结构体:resCode转化数据 #检查字典中是否存在键 if 'resCode' not in resultJson if ' ...

  6. IIS管理器中远程管理其它web服务器上的IIS站点

    IIS管理器中远程管理其它web服务器上的IIS站点 当生产环境服务器和部署项目过多时, 就需要单独一台机器来统一管理这些项目, 部署配置如下: 环境:项目服务器5台,运维服务器1台 应用:由运维服务 ...

  7. Linux 释放内存及占用较高问题排查

    1. 查看内存情况 #按 k 查看 free #按兆M查看 free -m total:总计物理内存的大小. used:已使用多大. free:可用有多少. Shared:多个进程共享的内存总额. B ...

  8. mac 编译安装ffmpeg

    下载源码: https://ffmpeg.org/download.html 解压, ./configure --disable-x86asm --prefix=/usr/local/ffmpeg_m ...

  9. docker-compose 搭建 redis 集群

    准备配置文件 bind 0.0.0.0 # redis端口 port ${PORT} requirepass redispwd # 关闭保护模式 protected-mode no # 开启集群 cl ...

  10. IDEA 开发SSM

    1.配置MAVEN 2.初始化SpringBoot 官网API:https://spring.io/projects/spring-boot 初始化SpringBoot:https://start.s ...