【SpringBoot】理解配置背后的 PropertySource 抽象

PropertySource

添加 PropertySource

  • <context:property-placeholder>

  • PropertySourcesPlaceholderConfigurer

    • PropertyPlaceholderConfigurer
  • @PropertySource
  • @PropertySources

Spring Boot 中的 @ConfigurationProperties

  • 可以将属性绑定到结构化对象上
  • ⽀持 Relaxed Binding
  • ⽀持安全的类型转换
  • @EnableConfigurationProperties

定制 PropertySource

主要步骤

  • 实现 PropertySource
  • 从 Environment 取得 PropertySources
  • 将⾃⼰的 PropertySource 添加到合适的位置

切⼊位置

  • EnvironmentPostProcessor
  • BeanFactoryPostProcessor

@PropertySource

官方文档地址:https://docs.spring.io/spring-framework/docs/5.3.23/javadoc-api/org/springframework/context/annotation/PropertySource.html

注解为向Spring的 Environment中添加PropertySource提供了一种方便的声明性机制。与@Configuration类一起使用。

使用demo

给定一个包含键/值对testbean.name=myTestBean的文件app.properties,下面的@Configuration类使用@PropertySourceapp.properties添加到Environment的propertysources集合。

 @Configuration
 @PropertySource("classpath:/com/myco/app.properties")
 public class AppConfig {

     @Autowired
     Environment env;

     @Bean
     public TestBean testBean() {
         TestBean testBean = new TestBean();
         testBean.setName(env.getProperty("testbean.name"));
         return testBean;
     }
 }

注意,Environment对象@Autowired到配置类中,然后在填充TestBean对象时使用。根据上面的配置,对testBean.getName()的调用将返回“myTestBean”。

解析和@Value注解中的${…}占位符

为了解析出和@Value注解中的${…}占位符,以使用PropertySource中的配置,必须确保在ApplicationContext使用的BeanFactory中注册了适当的嵌入式值解析器(an appropriate embedded value resolver)。当在XML中使用context:property-placeholder时,会自动进行注册。

在使用@Configuration类时,可以通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer来实现这一点。但是请注意,通过静态@Bean方法显式注册PropertySourcesPlaceholderConfigurer通常只有在需要定制配置(如占位符语法等)时才需要。可参见@Configuration的javadocs的“使用外部化值”部分和@Bean的javadocs的“关于beanfactorypostprocessor -返回@Bean方法的说明”了解详细信息和示例。

解析@PropertySource资源位置中的${…}占位符

任何${…}占位符出现在@PropertySource资源位置中,将根据已经在环境中注册的property sources进行解析。例如:

 @Configuration
 @PropertySource("classpath:/com/${my.placeholder:default/path}/app.properties")
 public class AppConfig {

     @Autowired
     Environment env;

     @Bean
     public TestBean testBean() {
         TestBean testBean = new TestBean();
         testBean.setName(env.getProperty("testbean.name"));
         return testBean;
     }
 }

假设“我的。”my.placeholder”出现在已经注册的property sources中——例如,系统属性或环境变量——占位符将被解析为相应的值。如果不是,那么"default/path"将被用作默认值。默认值(由冒号“:”分隔)是可选的。如果没有指定默认值且属性无法解析,则会抛出IllegalArgumentException异常。

关于使用@PropertySource重写属性的说明

在给定的属性键存在于多个.properties文件中的情况下,处理的最后一个@PropertySource注解将“胜出”并覆盖之前的同名键。

例如,给定两个属性文件a.propertiesb.properties,考虑以下两个使用@PropertySource注解引用它们的配置类:

 @Configuration
 @PropertySource("classpath:/com/myco/a.properties")
 public class ConfigA { }

 @Configuration
 @PropertySource("classpath:/com/myco/b.properties")
 public class ConfigB { }

覆盖顺序取决于这些类在应用程序上下文中注册的顺序。

 AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 ctx.register(ConfigA.class);
 ctx.register(ConfigB.class);
 ctx.refresh();

在上面的场景中,b.properties中的属性将覆盖a.properties中存在的任何副本,因为ConfigB是最后注册的。

在某些情况下,在使用@PropertySource注解时,严格控制属性源顺序可能是不可能或不实际的。例如,如果上面的@Configuration类是通过组件扫描注册的,那么排序就很难预测。在这种情况下——如果重写很重要——建议用户回到使用编程的PropertySource API。详细信息可参见ConfigurableEnvironmentMutablePropertySources 的javadocs。

注意:根据Java 8约定,该注解是可重复的。但是,所有这样的@PropertySource注解都需要在同一层声明:要么直接在配置类上声明,要么作为元注解在相同的自定义注解上声明。不建议混合使用直接注解和元注解,因为直接注解将有效地覆盖元注解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以按照以下步骤来整合Spring Boot和MyBatis Plus,并生成Mapper的抽象基类: 1. 首先,确保你已经在你的Spring Boot项目中添加了MyBatis Plus的依赖。你可以在项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- 其他依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>最新版本</version> </dependency> </dependencies> ``` 2. 接下来,创建一个Mapper抽象基类,用于继承MyBatis Plus提供的BaseMapper接口。该基类可以包含一些常用的CRUD方法,以供其他Mapper继承使用。以下是一个示例: ```java import com.baomidou.mybatisplus.core.mapper.BaseMapper; public interface MyBaseMapper<T> extends BaseMapper<T> { // 自定义一些通用的方法 // ... } ``` 3. 然后,在你的实体类对应的Mapper接口中继承自定义的MyBaseMapper接口。例如: ```java import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper extends MyBaseMapper<User> { // 可以在这里添加一些特定的方法 // ... } ``` 4. 最后,你可以通过MyBatis Plus的代码生成器来自动生成Mapper接口和XML映射文件。你可以在pom.xml文件中配置相关插件,例如: ```xml <build> <plugins> <!-- 其他插件 --> <plugin> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-maven-plugin</artifactId> <version>最新版本</version> <executions> <execution> <id>generate</id> <phase>generate-sources</phase> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <includeTables> <!-- 需要生成Mapper的表 --> </includeTables> <basePackage> <!-- Mapper接口的包名 --> </basePackage> <basePath> <!-- 生成文件的输出路径 --> </basePath> </configuration> </plugin> </plugins> </build> ``` 配置完成后,你可以启动Maven命令来生成Mapper接口和XML映射文件。执行以下命令: ``` mvn generate-sources ``` 这样,你就成功地整合了Spring Boot和MyBatis Plus,并生成了Mapper的抽象基类。接下来,你可以在项目中使用这些Mapper来进行数据库操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值