资源目录结构
-----------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)
- 当属性满足要求时,此条件成立:要求如果没有配置springhttp.encoding.enabled=xxx,则成立。
即每一个XxAutoConfigluration都有很多条件@Conditiona10nXxx,当这些条件都满足时,则此配置自动生效。但是我们可以手工修改改自动装配:
XxxProperties文件中的préfix+属性名=value - 全局配置文件中的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环后 |
@CondItlonalOnJndi | JND存在指定项 |
注意:debug=true可以查看系统加载了哪里三方类
Positive matches列表表示springboot自动开启的装配
Negative matches列表表示springboot在此时并没有启用的自动装配