SpringBoot注解之@SpringBootApplication

一、@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")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值