记录一次在springboot的yml中读取maven配置项的坑

1.问题引出

首先,我在pom.xml文件中定义了一些连接MySQL的数据源参数。因为在maven插件和yml中都要用到,所以写在pom中。

在这里插入图片描述

注意上面的密码是使用单引号括起来的,因为密码包含#号,会被yml当作注释

然后,在yml中引用这些参数:

在这里插入图片描述

启动项目,报错,信息如下:

Caused by: java.lang.RuntimeException: 
Driver com.mysql.cj.jdbc.Driver claims to not accept jdbcUrl, 
https://spring.io/projects/spring-boot/springboot-liquibase

???,怎么出现了一个https://spring.io/projects/spring-boot/springboot-liquibase这个不存在的链接???

2.调试

修改启动类,打印一下yml配置的数据源信息

启动类代码如下:

@SpringBootApplication
public class Starter {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication();
        springApplication.addInitializers(applicationContext -> {
            ConfigurableEnvironment environment = applicationContext.getEnvironment();
            System.out.println("user:" + environment.getProperty("spring.datasource.username"));
            System.out.println("pwd:" + environment.getProperty("spring.datasource.password"));
            System.out.println("url:" + environment.getProperty("spring.datasource.url"));
            System.out.println("driver:" + environment.getProperty("spring.datasource.driver-class-name"));
        });
        springApplication.run(args);
    }
}

通过添加一个初始化器,在spring启动的过程中打印出环境信息。如果直接通过静态run方法启动,报错后程序结束就无法获取应用上下文了。(具体springboot启动的细节可以参考黑马满一航老师的spring高级视频,b站上有,讲的很好 视频地址。学懂后调试、找错、在boot启动中添加自己的逻辑都简简单单!!!)

打印的信息如下:

在这里插入图片描述

???我的url明明写的不是这个东西啊

3.问题产生的原因与解决

根据我的一个猜测,应该是Spring环境中默认内置了一个名为url的参数,值为spring的官网加上你的项目名的这样一个字符串。

于是把maven中的url参数名改一下,从url改为mysql-url:

在这里插入图片描述

启动项目,成功!!!
在这里插入图片描述

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Java,我们可以使用Spring框架提供的Yaml工具类来读取Yaml文件配置信息。以下是读取Yaml文件的示例代码: 1. 依赖添加 如果你使用的是Maven,可以在pom.xml文件添加如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-yaml</artifactId> </dependency> ``` 2. 读取Yaml文件 假设我们有一个名为config.yml的Yaml文件,包含如下内容: ```yml server: port: 8080 context-path: /demo database: url: jdbc:mysql://localhost:3306/mydb username: root password: 123456 ``` 我们可以使用以下代码将上述Yaml文件配置信息读取到一个Map对象: ```java import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import org.yaml.snakeyaml.Yaml; import java.io.InputStream; import java.util.Map; @Component @PropertySource(value = "classpath:config.yml", factory = YamlPropertySourceFactory.class) public class Config { @Value("${server.port}") private int port; @Value("${server.context-path}") private String contextPath; @Value("${database.url}") private String databaseUrl; @Value("${database.username}") private String databaseUsername; @Value("${database.password}") private String databasePassword; public int getPort() { return port; } public String getContextPath() { return contextPath; } public String getDatabaseUrl() { return databaseUrl; } public String getDatabaseUsername() { return databaseUsername; } public String getDatabasePassword() { return databasePassword; } public static class YamlPropertySourceFactory extends DefaultPropertySourceFactory { @Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { if (resource == null) { return super.createPropertySource(name, resource); } Yaml yaml = new Yaml(); try (InputStream inputStream = resource.getInputStream()) { Map<String, Object> map = yaml.load(inputStream); String sourceName = name != null ? name : resource.getResource().getFilename(); return new MapPropertySource(sourceName, map); } } } } ``` 上述代码,我们首先定义了一个名为Config的类,并使用@Component注解将其声明为Spring组件。然后,我们在该类定义了一些实例变量,用于存储Yaml文件配置信息。在实例变量上,我们使用@Value注解来指定对应的配置项,例如${server.port}表示读取server.port配置项的值,并将其赋值给port实例变量。最后,我们定义了一个名为YamlPropertySourceFactory的内部类,用于读取Yaml文件并将其转换成Map对象。在这个内部类,我们使用了SnakeYAML库来读取Yaml文件,并将其转换成Map对象。 为了在应用程序使用Config类的实例变量,我们可以使用@Autowired注解将其注入到其他Spring组件,例如Controller、Service等。 总的来说,使用Spring框架提供的Yaml工具类能够很方便地读取Yaml文件配置信息,并将其转换成Java对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值