Spring Boot装配配置文件properties的参数

spring装配配置文件主要分为两种:一种是主配置文件,另一种是其他配置文件。

在使用maven构建spring boot项目时会产生一个主要的配置文件application.properties

在这里插入图片描述

有些习惯yml的语法是application.yml都是一个意思,spring也能自动识别。

除了自动生成的配置文件外,其余的如file.propertiesredis.properties都是新生成的spring无法自动识别需要通过spring相关配置类或注解引入配置文件。

spring boot在启动时会自动自动加载默认配置文件的参数,但这些是spring boot定义了的,在主配置文件中自定义的参数仍然需要加载。

加载主配置文件的自定义参数@ConfigurationProperties注解

SpringBoot默认会读取文件名为application.properties的资源文件,@ConfigurationProperties注解以对象的形式加载文件的参数:

  • 默认配置文件定义参数:
define.name = _xiaoxu_
define.sex = man
define.age = 18
  • @ConfigurationProperties注解加载到对象属性中
@Repository
@ConfigurationProperties(prefix = "define")
@Data
public class Define {

    private String name;

    private String age;
    private String sex;
}

prefix定义变量前缀,其后的内容需要与属性字段对应。

  • 将对象DL注入到IOC容器中

在这里插入图片描述

  • 装配对象和调用
 @Autowired
 private Define define;
 
 @Test
 void four(){
     System.out.println("姓名:"+define.getName()+"年龄:"+define.getAge()+"性别:"+define.getSex());
 }

如下图所示,调用成功:
在这里插入图片描述

@Value加载默认配置文件参数

还是之前的参数,这里使用@Value注解

@Repository
@Data
public class DefineTwo {

    @Value("${define.name}")
    private String name;

    @Value("${define.age}")
    private String age;

    @Value("${define.sex}")
    private String sex;
}

@Value注解不需要引入文件,直接读取application.properties的属性,另外创建的类需要DL装配。

测试:

@Autowired
private DefineTwo defineTwo;
@Test
void five(){
    System.out.println("姓名:"+define.getName()+"年龄:"+define.getAge()+"性别:"+define.getSex());
}

在这里插入图片描述

@PropertySource读取自定义配置文件

定义file.properties文件

filepath=C:\\Users\\fireapproval\\Desktop\\数据集\\test.csv

redis直接在默认配置文件配置,这里起演示作用

//导入外部文件
@PropertySource("classpath:file.properties")
@Value("${filepath}")
private String filepath;

这里读取自定义文件后还面临一个重要的问题,就是@Value注解,在类中使用赋值给属性,但是却并不是由spring 的IOC容器管理,这是需要生产对象返回该属性的值:

 @Bean(name = "getFilePath")
 //@Scope(value = "prototype")
 public String getFilePath() throws UnsupportedEncodingException {
     return new String(this.filepath.getBytes("ISO-8859-1"),"UTF-8");
 }

如上是@Bean生产了一个对象获取了@Value的属性,再返回具体的字符串,idea读取properties默认是IOS-8859-1。

通过bean生产后在其他地方就可以通过@Autowired自动装配来使用:

 //文件地址
 @Autowired
 private String getFilePath;

另外在自动装配属性还遇到了一个问题,场景如下:

  • 在启动类导入文件,先注入属性,在生产bean注入返回属性的值

在这里插入图片描述

  • 在其他类中自动装配对象
//文件地址
@Autowired
private String getFilePath;

//获取全局的数据
ReadCSV readCSV = new ReadCSV();
List<ArrayList> maps = readCSV.readTwoColumn(getFilePath, 7, 9);    

但这里后一致出现getFilePath为null,这是由于对象生产和装配的时机不一样,导致bean还未生产
readTwoColumn就在装配了,因此需要将装配方法通过函数包裹:

 private List<ArrayList> getData(){
     ReadCSV readCSV = new ReadCSV();
     List<ArrayList> maps = readCSV.readTwoColumn(getFilePath, 7, 9);    
     return maps;

 }

将产生全局数据放在方法中,不能直接放在类内部。

spring引入外部属性或配置的注解还有:@Import: 用来导入其他配置类。
@ImportResource: 用来加载xml配置文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Spring Boot项目中使用MyBatis Starter,需要先添加以下依赖项: ```xml <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> ``` 然后,需要在application.properties文件中配置数据库连接信息: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&useSSL=false spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver ``` 接下来,需要创建一个MyBatis映射器接口和一个相应的XML文件。例如,创建一个UserMapper接口和一个user.xml文件: ```java public interface UserMapper { @Select("SELECT * FROM user") List<User> getAllUsers(); } ``` ```xml <mapper namespace="com.example.mapper.UserMapper"> <select id="getAllUsers" resultType="com.example.entity.User"> SELECT * FROM user </select> </mapper> ``` 最后,需要在Spring Boot的应用程序类上添加@MapperScan注释,以扫描映射器接口: ```java @SpringBootApplication @MapperScan("com.example.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在,就可以在Spring Boot应用程序中使用MyBatis进行数据库操作了。例如,可以像下面这样使用UserMapper接口: ```java @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public List<User> getAllUsers() { return userMapper.getAllUsers(); } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xvwen

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

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

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

打赏作者

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

抵扣说明:

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

余额充值