- spring boot中使用@SpringBootApplication指定类为应用启动类,
自动扫描于当前类同级以及子包下的相应注解注册为spring beans,
在类中main方法中通过SpringApplication的run方法启动应用。 使用@SpringBootApplication注解,等价于同时使用@Configuration @EnableAutoConfiguration @ComponentScan这三个注解的默认属性,同时,使用@SpringBootApplication也可以接合使用@EnableAutoConfiguration @ComponentScan。
三个注解的作用
- @ComponentScan
可以通过该注解指定扫描某些包下包含如下注解的均自动注册为spring beans:
@Component, @Service, @Repository, @Controller,@Entity等等。 - @Configuration
@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的,作用为:配置spring容器(应用上下文)
作用相当于:
- @ComponentScan
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false">
</beans>
- @EnableAutoConfiguration
Spring Boot的自动化配置主要是通过@EnableAutoConfiguration来实现的