Springboot中抽取公共的properties文件

将公共的配置抽取出来,写在配置类中,以便后期的维护

  1. 首先创建一个Springboot项目
  2. 在resources文件夹下创建一个config.properties(文件名随意)文件,在这个文件中配置要抽取的公共配置
    在这里插入图片描述
    方式一
  3. 写一个读取配置类
package com.lemon.common;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Data
@Component
@PropertySource("classpath:config.properties")
@ConfigurationProperties(prefix = "config")
public class CommonConfig {
    private String name;
    private Integer age;
}

@Data : 简化get/set方法
@Component : 注入spring管理
@PropertySource(“classpath:config.properties”) : 指定配置文件地址和文件名
@ConfigurationProperties(prefix = “config”) 指定该配置项在配置文件中的前缀

方式二

package com.lemon.common;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

/**
 * @author Zhm
 * @date 2021/5/16 10:36
 **/
@Data
@Component
@PropertySource("classpath:config.properties")
public class CommonConfig {
    @Value("${config.name}")
    private String name;
    @Value("${config.age}")
    private Integer age;
}

@Value(${}) : 根据这个{里面给的名字去properties文件中获取对应的值}

两种方式的区别
在这里插入图片描述
4.测试类获取

import com.lemon.common.CommonConfig;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
@Slf4j
class ParcticeApplicationTests {

    @Autowired
    CommonConfig commonConfig;

    @Test
    void contextLoads() {
        log.info(commonConfig.getName());
        log.info(commonConfig.getAge()+"");
    }

}

结果显示
在这里插入图片描述

可以获取到对应的值

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
可以通过在Spring Boot使用定时任务来实现定时抽取一张表的数据插入到另一张表。以下是一个基本的示例: 1. 首先,需要在Spring Boot配置定时任务。可以通过在启动类上添加@EnableScheduling注解来启用定时任务。 ```java @SpringBootApplication @EnableScheduling public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 2. 然后,需要创建一个定时任务类,并在其编写抽取数据并插入到另一张表的逻辑。可以使用Spring Boot提供的@Scheduled注解来指定任务的执行时间。 ```java @Component public class DataTransferTask { @Autowired private SourceRepository sourceRepository; @Autowired private TargetRepository targetRepository; @Scheduled(cron = "0 0 0 * * ?") // 每天凌晨执行 public void transferData() { List<SourceData> sourceDataList = sourceRepository.findAll(); List<TargetData> targetDataList = new ArrayList<>(); for (SourceData sourceData : sourceDataList) { TargetData targetData = new TargetData(); // TODO:根据需求对数据进行转换 targetDataList.add(targetData); } targetRepository.saveAll(targetDataList); } } ``` 3. 最后,需要创建源表和目标表的实体类,并分别创建对应的Repository。 ```java @Entity public class SourceData { // TODO:定义源表的字段 } @Entity public class TargetData { // TODO:定义目标表的字段 } @Repository public interface SourceRepository extends JpaRepository<SourceData, Long> { } @Repository public interface TargetRepository extends JpaRepository<TargetData, Long> { } ``` 通过以上步骤,就可以实现定时抽取一张表的数据插入到另一张表的功能了。需要注意的是,在实际开发,可能需要根据具体需求对代码进行修改和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lemon20120331

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值