springboot学习,@SpringBootApplication(一)

资源目录结构

-----------resources
--------------static:静态资源(html,css,js,图片,文件等)
--------------templates:模板文件(freemarker,thymeleaf;默认不支持JSP)
--------------application.properties:配置文件

main方法启动注解@SpringBootApplication,即spring boot的主配置类主要包含了下列三个主要的注解
主要包含了下列三个主要的注解

  • @SpringBootConfiguration
    包括了@Configuration注解,可起到配置作用,将配置添加到到spring容器中
  • @EnableAutoConfiguration
    使spring boot可以自动配置(约定大于配置),包括下列两个重要的注解
    @AutoConfigurationPackage(扫描包,用户自定义)
    –>@Import(AutoConfigurationPackages.Registrar.class)
    ---->AutoConfigurationPackages.Registrar(扫描@SpringBootConfiguration注解所在的包类,作用:会将该包及其所有的子包纳入spring容器管理)
    @Import(AutoConfigurationImportSelector.class)(有选择性的导入到spring容器中,主要是第三方jar,配置等)
    –>AutoConfigurationImportSelector#selectImports
    ---->SpringFactoriesLoader FACTORIES_RESOURCE_LOCATION = “META-INF/spring.factories”; 起动三方依赖
    spring-boot-autoconfigure-x.x.x.RELEASE.jar,包含了整个J2EE所需要的所有依赖

举例说明
org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration

@Configuration
@EnableConfigurationProperties(HttpProperties.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnClass(CharacterEncodingFilter.class)
@ConditionalOnProperty(prefix = "spring.http.encoding", value = "enabled", matchIfMissing = true)
public class HttpEncodingAutoConfiguration {

}

通过观察该源码发现:
@Configuration:标识此类是一个配置类、将此类纳入spring ioc容器
@EnableConfigurationProperties(HttpProperties.class):通过改HttpProperties的predfix+属性名进行修改[配置文件中,yml/properties]
如默认编码spring.http.encoding.charset=UTF-8,可以再配置文件application.properties中添加配置spring.http.encoding.charset=ISO-8859-1修改

@ConditionalOnXXX,满足条件才加载,举例
@ConditionalOnProperty(prefix = “spring.http.encoding”, value = “enabled”, matchIfMissing = true)

  1. 当属性满足要求时,此条件成立:要求如果没有配置springhttp.encoding.enabled=xxx,则成立。
    即每一个XxAutoConfigluration都有很多条件@Conditiona10nXxx,当这些条件都满足时,则此配置自动生效。但是我们可以手工修改改自动装配:
    XxxProperties文件中的préfix+属性名=value
  2. 全局配置文件中的key,来源于某个Properties文件中的prefix+属性名

常见@CondItlonal

@CondItlonal扩展注解作用(判断是否满足当前稻定条件)
@CondItlonalOnJava系统的java版是否符台要求
@CondItlonalOnBean容器中存在指定Bean:
@CondItlonalOnMissingBean容器中不存在指定日ean:
@CondItlonalOnExpression满足SPEL表达式指定
@CondItlonalOnClass系统中有指定的类
@CondItlonalOnMissingClass系统中沿有指定的类
@CondItlonalOnSingleCandidate容器中只有一个指定的Bean,或者这个Bean是首选Bean
@CondItlonalOnProperty系统中指定的属性是否有指定的值
@CondItlonalOnResource类路径下是台存在指定资源文件
@CondItlonalOnWebApplication当前是web环境
@CondItlonalOnNotWebApplication当前不是web环后
@CondItlonalOnJndiJND存在指定项

注意:debug=true可以查看系统加载了哪里三方类
Positive matches列表表示springboot自动开启的装配
Negative matches列表表示springboot在此时并没有启用的自动装配

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值