SpringBoot为什么不需要xml配置文件?

一、@SpringBootApplication注解

这个注解相当于三个注解的功能集成

  • @EnableAutoConfiguration:启用Spring Boot的自动bean加载机制
  • @ComponentScan:在应用程序所在的包上启用扫描
  • @Configuration:允许在Spring中注册额外的bean或导入其他配置类
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
// 以下三个注解是SpringBoot中非常重要的内容
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
    @Filter(    type = FilterType.CUSTOM,  classes = {TypeExcludeFilter.class}), 
    @Filter(    type = FilterType.CUSTOM,  classes = {AutoConfigurationExcludeFilter.class}) })
public @interface SpringBootApplication {
1. @SpringBootConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

可以看到上边的代码中,@SpringBootConfiguration 注解的上方还有一个@Configuration的注解内容,表示这个注解本质来说也就是一个Configuration,即这就是SpringBoot为什么不需要xml配置文件的原因,总的来说,这个注解允许了配置bean和导入其他的配置类的行为。

2. @EnableAutoConfiguration
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

这个注解总的来说就是为了解决自动装配的问题,即 启用Spring Boot的自动bean加载机制,具体的内容,后续再进行描述。

3. @ComponentScan
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    ...
}

这个就是自动扫描包的注解配置,具体需要注意的点就是这段注释

 * <p>Either {@link #basePackageClasses} or {@link #basePackages} (or its alias
 * {@link #value}) may be specified to define specific packages to scan. If specific
 * packages are not defined, scanning will occur from the package of the
 * class that declares this annotation.

转译成中文的意思大概就是,如果注解上没有配置 basePackageClassesbasePackages, 则会默认扫描当前注解声明的类的包路径。


二、代码包扫描

如下图所示,如果SpringBootApplication注解没有定义包路径scanBasePackages,则会自动默认扫描与Application.java这个类所属包下的内容

默认的包结构及其作用

1.主程序Application.java放在根包,在其他类之上。

2.@SpringBootApplication注解写在主程序上。

3.Spring对类的扫描默认仅涵盖主程序所在的包及子包

 注意: 如果定义了 scanBasePackageClassesscanBasePackages,可能会导致启动类包路径下的bean无法被扫描到,所以启动类的包路径也要加入进去,避免出现无法正常扫描包的问题



三、零Spring配置文件

SpringBoot中建议放弃通过XML定义Spring应用程序,推荐在代码类上面通过@Configuration实现配置。如有需要,还可以通过@lmportResource来导入xml配置文件。

即通俗点来说,在SpringBoot项目中,最好不要使用xml的配置文件,而使用@Configuration定义相关的类,如果真的有需要,请使用@ImportResource来导入xml配置文件。

 注:要使@Configuration生效,你需要将它写在SpringBoot入口程序类上面,或者使用@EnableAutoConfiguration或 @SpringBootApplication这两个注解来开启自动加载功能。


四、个性化加载配置


五、外部参数配置信息加载

Spring应用程序可以通过属性文件,YAML文件,环境变量和命令行参数等方式的外部化参数配置


没有创建application.yml文件的时候使用这种方式

  • 启动时命令行传参java-jar app.jar --name="test"
  • java -jar springboot-demo-1.0.0.jar --spring.profile.active="test"
  • SpringBoot配置信息中的特殊值:SPRING_APPLICATION_JSON='{"name":"test"}'
  • java -jar springboot-demo1-1.0.0.jar --SPRING_APPLICATION_JSON="{\"spring.profiles.active\":\"dev\"}"
  • 如果是web应用,可以读取Servletconfig init参数
  • 如果是web应用,可以读取ServletContext init参数
  • JNDI属性来自java:comp/env
  • Java系统属性(System.getProperties())
  • 操作系统环境变量
  • 配置文件application.properties、application.yml、application-[profile}.properties、application-[profile}.yml
  • 一般来说,我们创建一个SpringBoot项目,一般都有好几个yml文件,如:
    • application.yml 主文件
    • application-dev.yml 开发环境配置
    • application-test.yml 测试环境配置
    • application-prod.yml 生产环境配置

以上的几个配置文件分别是使用在不同的环境下的,但是我们可以通过修改application.yml的内容,来决定使用哪个文件,如下方所示:

这里表示使用的开发环境的配置文件。

@PropertySource注解导入的配置:@PropertySource(value={"person.properties"})

程序入口通过
SpringApplication.setDefaultProperties方法设定的参数配置

        SpringApplication springApplication = new SpringApplication(Example.class);
        springApplication.setAdditionalProfiles();
        Properties properties = new Properties();
        properties.setProperty("name", "setDefaultProperties-tony");
        springApplication.setDefaultProperties(properties);
        springApplication.run(args);


六、环境化配置 - profile

  • profile是什么机制?
    1.Spring配置文件提供的一种隔离应用程序配置的方法,使其仅在特定环境中可用。2.可通过profile指定Bean的应用环境(如开发、测试、生产等环境)3.可通过profile指定不同环境的配置参数值
  • 如何指定profile
    通过配置参数spring.profiles.active来指定应用启用的profiles。默认default1.在环境变量中指定: jvm参数、命令行程序参数、application.properties中都可以2.代码中指定: springApplication.setAdditionalProfiles("dev,test");
  • 如何在开发中使用
    1.Configuration类或者Bean定义方法上,都可以通过添加@Profile(" dev")注解,实现指定环境下生效。2.配置文件中:<beans profile= "dev" ><bean …>…</bean></bean>

七、配置文件

  • 配置文件可以存放在哪些位置?
    1.当前项目运行的盘符/config文件夹下面:file:./config/2.当前项目运行的目录下面(命令执行的所在目录): file:./3.classpath下面的config文件夹: classpath:/config4.classpath的根目录(我们平常就是用这种) : classpath:/

上述配置文件按优先级排列,排在上面的位置会覆盖优先级降低的配置。

  • 自定义配置名称和存储路径
    spring.config.name(spring_config_name)=properties-file-namespring.config.location(spring_config_location)=classpath:/config /, file:./config/(注:从右到左反序搜索)必须将它们定义为环境属性,通常是操作系统环境变量,JVM参数或命令行参数。

  • 八、配置文件格式

    SpringBoot支持两种配置文件的格式: .properites、.yml

    yaml语法精简版说明:

  • 大小写敏感
  • 使用空格缩进表示层级(不要用TAB),同一层级的左侧对齐
  • map键值对通过“:”分隔
  • list列表元素通过“-”表示

properites示例:

spring.datasource.username=test


九、参数使用

方式一:
通过@Value("${my.name}")注解y将指定的参数配置注入到属性。

方式二:
注入Environment对象。

 方式三:
通过注解@ConfigurationProperties(prefix = "my")
将注解加在指定的类上,spring会为实例对象的属性进行赋值,属性需有getters和setters方法。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值