本文共 2228 字,大约阅读时间需要 7 分钟。
/*来标注一个主程序类,说明这是一个springboot程序*/import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class HelloworldMainApplication {public static void main(String[] args) { SpringApplication.run(HelloworldMainApplication.class, args);}}
@SpringBootApplication:SpringBoot应用标注在某个类上,就说明哪个类是SpringBoot是主配置类,SpringBoot就启动这个类的main方法来启动应用;
@SpringBootApplication是一个复合注解
import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Inherited;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.boot.SpringBootConfiguration;import org.springframework.boot.context.TypeExcludeFilter;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.FilterType;import org.springframework.context.annotation.ComponentScan.Filter;import org.springframework.core.annotation.AliasFor;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Inherited@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class}), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class})})
包括
@EnableAutoConfiguration
作用: 它主要利用了一个 EnableAutoConfigurationImportSelector选择器给Spring容器中来导入一些组件
3.1 @AutoConfigurationPackage 自动配置包 将主配置类的所在包以及下面所有子包里的插件扫描进spring容器 3.2@Import({EnableAutoConfigurationImportSelector.class}) Spring底层注解import,**关键功能是Import注解**
@SpringBootConfiguration继承自@Configuration,二者功能也一致,一个为spring注解,一个为SpringBoot注解,标注当前类是配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例名就是方法名。
@EnableAutoConfiguration的作用启动自动的配置,@EnableAutoConfiguration注解的意思就是Springboot根据你添加的jar包来配置你项目的默认配置,比如根据spring-boot-starter-web
,来判断你的项目是否需要添加了webmvc和tomcat,就会自动的帮你配置web项目中所需要的默认配置。
@ComponentScan,扫描当前包及其子包下被@Component,@Controller,@Service,@Repository注解标记的类并纳入到spring容器中进行管理。是以前的context:component-scan(以前使用在xml中使用的标签,用来扫描包配置的平行支持)。所以本demo中的User为何会被spring容器管理。
转载地址:http://lqthn.baihongyu.com/