一、@SpringBootApplication的功能
1.一般放在项目的启动类上,把启动类注入到容器中。
2.定义容器要扫描的范围,加载环境中的bean。
3.把bean注入到容器中,原因如下:
(1)@SpringBootApplication对如下注解进行了包装;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan()
(2)@SpringBootConfiguration注解又封装了@Configuration;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
(3)@Configuration注解里又包含了@Component;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
(4)@Component的功能就是把一个Bean注入到容器中去。
二、@SpringBootApplication的用法
1.@SpringBootApplication默认扫描该注解类所在的包;
@SpringBootApplication
public class TestSpringBoot{
public static void main(String[] args) {
SpringApplication.run(TestSpringBoot.class, args);
}
}
2.scanBasePackages属性显示指定要扫描的包的范围;
@SpringBootApplication(scanBasePackages="com.Bean")
3.scanBasePackageClasses属性显示指定要扫描的类;
@SpringBootApplication(scanBasePackageClasses=Student.class)
4.exclude属性显示指定要排除的类;
@SpringBootApplication(scanBasePackages="com.Bean", exclude=Student.class)
5.excludeName属性显示指定要排除的“类的名字”;
@SpringBootApplication(scanBasePackages="com.Bean", excludeName="com.example.beans.Student")