一、为什么SpringBoot产生于Spring4?

Spring4中增加了@Condition annotation, 使用该Annotation之后,在做依赖注入的时候,会检测是否满足某个条件来决定是否注入某个类。

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)
@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)
@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)
@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)
@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)
@ConditionalOnNotWebApplication(不是web应用)

Conditional

@ConditionalSpringFramework的功能,SpringBoot在它的基础上定义了@ConditionalOnClass@ConditionalOnProperty的一系列的注解来实现更丰富的内容。
观察@ConditionalOnClass会发现它注解了@Conditional(OnClassCondition.class)

    @Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnClassCondition.class)
public @interface ConditionalOnClass {
Class<?>[] value() default {};
String[] name() default {};
}

OnClassCondition则继承了SpringBootCondition,实现了Condition接口。

    public interface Condition {
boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata);
}

查看SpringFramework的源码会发现加载使用这些注解的入口在ConfigurationClassPostProcessor中,这个实现了BeanFactoryPostProcessor接口,前面介绍过,会嵌入到Spring的加载过程。
这个类主要是从ApplicationContext中取出Configuration注解的类并解析其中的注解,包括 @Conditional@Import@Bean等。
解析 @Conditional 逻辑在ConfigurationClassParser类中,这里面用到了 ConditionEvaluator 这个类。

ConditionEvaluator中的shouldSkip方法则使用了 @Conditional中设置的Condition类。
作者:wcong
链接:https://www.jianshu.com/p/1d0fb7cd8a26

二、@Enable*注解和属性映射

@Enable*注解

@Enable*在Spring 3框架就引入了这些注解,用来替代XML配置文件。 
很多Spring开发者都知道@EnableTransactionManagement注释,它能够声明事务管理;@EnableWebMvc注释,它能启用Spring MVC;以及@EnableScheduling注释,它可以初始化一个调度器。 
这些注释事实上都是简单的配置,通过@Import注释导入。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({ EnableAutoConfigurationImportSelector.class,
AutoConfigurationPackages.Registrar.class })
public @interface EnableAutoConfiguration { /**
* Exclude specific auto-configuration classes such that they will never be applied.
*/
Class<?>[] exclude() default {}; }

EnableAutoConfigurationImportSelector类使用了Spring Core包的SpringFactoriesLoader类的loadFactoryNamesof()方法。 
SpringFactoriesLoader会查询META-INF/spring.factories文件中包含的JAR文件。 
当找到spring.factories文件后,SpringFactoriesLoader将查询配置文件命名的属性。在例子中,是org.springframework.boot.autoconfigure.EnableAutoConfiguration。

属性映射@ConfigurationProperties注解

@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties { private String host;
private int port = DBPort.PORT;
private String uri = "mongodb://localhost/test";
private String database; // ... getters/ setters omitted
}

@ConfigurationProperties注释将POJO关联到指定前缀的每一个属性。例如,spring.data.mongodb.port属性将映射到这个类的端口属性。

Spring Boot条件注解的更多相关文章

  1. spring boot: 条件注解@Condition

    @Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解). 比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多 ...

  2. 峰哥说技术: 05-Spring Boot条件注解注解

    Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术  Spring Boot条件注解 @EnableAutoConfiguration开启自 ...

  3. Spring Boot 常用注解汇总

    一.启动注解 @SpringBootApplication @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documen ...

  4. Spring Boot中注解@ConfigurationProperties

    在Spring Boot中注解@ConfigurationProperties有三种使用场景,而通常情况下我们使用的最多的只是其中的一种场景.本篇文章带大家了解一下三种场景的使用情况. 场景一 使用@ ...

  5. Spring Boot常用注解总结

    Spring Boot常用注解总结 @RestController和@RequestMapping注解 @RestController注解,它继承自@Controller注解.4.0之前的版本,Spr ...

  6. 3个Spring Boot核心注解,你知道几个?

    Spring Boot 核心注解讲解 Spring Boot 最大的特点是无需 XML 配置文件,能自动扫描包路径装载并注入对象,并能做到根据 classpath 下的 jar 包自动配置. 所以 S ...

  7. Spring Boot@Component注解下的类无法@Autowired的问题

    title: Spring Boot@Component注解下的类无法@Autowired的问题 date: 2019-06-26 08:30:03 categories: Spring Boot t ...

  8. Spring boot 基于注解方式配置datasource

    Spring boot 基于注解方式配置datasource 编辑 ​ Xml配置 我们先来回顾下,使用xml配置数据源. 步骤: 先加载数据库相关配置文件; 配置数据源; 配置sqlSessionF ...

  9. 【SpringBoot】15. Spring Boot核心注解

    Spring Boot核心注解 1 @SpringBootApplication 代表是Spring Boot启动的类 2 @SpringBootConfiguration 通过bean对象来获取配置 ...

随机推荐

  1. extjs分页

    1.本地分页:设置store的proxy属性为pagingmemoryproxy实例 2.远程分页

  2. 【JS】自学

    JS自学网址: http://www.runoob.com/js/js-tutorial.html

  3. mysql 内置功能 触发器介绍

    使用触发器可以在用户对表进行[增.删.改]操作时前后定义一些操作,注意:没有查询 创建触发器 create trigger 触发器的名字 之前(before)或者之后(after)  行为(inser ...

  4. string、const char*、 char* 、char[]相互转换(待整理)

    string.const char*. char* .char[]相互转换(全) https://blog.csdn.net/rongrongyaofeiqi/article/details/5244 ...

  5. [py]django表单不清空实现的2种方法

    参考 参考: django实现内容不清空2种方法 django form的作用 1.生成html标签 2.验证输入内容 form生成表单 zhuji/forms.py - 实例化表单 - 定制form ...

  6. openstack 部署笔记--neutron计算节点

    控制节点 # vim /etc/neutron/neutron.conf [DEFAULT] # ... transport_url = rabbit://openstack:root@control ...

  7. 记两个国外CTF的弱pwn

    两道题都来自CSAW CTF 18.PWN学得不够多,如果哪里错了,欢迎留言交流. 第一个题 get_it checksec检查之后,发现栈保护没开,很可能是栈溢出.IDA打开F5看伪源码. int ...

  8. [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  9. js时钟

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. lnmp搭建环境易错误点

    1.虚拟主机使用桥接网络 2.nginx配置server 3.先ping通,service iptables stop 4.php-fpm开启,使之能正确解析php文件,nginx开启 5.mysql ...