Spring Boot核心

@SpringBootApplication是Spring Boot的核心注解,他是一个组合注解。

 

@SpringBootApplication注解主要组合了@Configuration、@EnableAutoConfiguration、@ComponentScan;若不使用注解则可以在入口类上直接使用@Configuration、@EnableAutoConfiguration、@ComponentScan。

@EnableAutoConfiguration让Spring Boot根据类路径中的jar包依赖为当前项目进行自动配置。

例如,添加了spring-boot-starter-web依赖,会自动添加Tomcat和Spring MVC的依赖,那么Spring Boot会对Tomcat和Spring MVC进行自动配置

@SpringBootApplication注解的exclude参数指关闭特定的自动配置

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

 

Spring Boot提倡0配置,即无xml配置,但是在实际项目中,可能有一些特殊需求要求你必须使用xml,这时我们可以通过Spring提供的@ImportResource来加载xml配置,例如:

@ImportResource("classpath:some-context.xml","classpath:another-context.xml");Spring Boot还提供了基于安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联,从而实现类型安全的配置。Profile配置Profile是Spring用来针对不同的环境对不同的配置提供支持的,全局Profile配置使用application-{profile}.properties(如:application-prod.properties)通过application.properties中设置spring.profiles = prod来指定活动的profile。