【问题标题】:ConfigurationProperties with prefix is not working带前缀的 ConfigurationProperties 不起作用
【发布时间】:2019-03-28 19:16:36
【问题描述】:

我是 Spring Boot 的新手,正在创建我的第一个应用程序。在创建数据源时,我使用带有前缀和要从 application.property 读取的属性的 @ConfigurationProperties。

但是,此设置似乎对我不起作用,并且我的程序没有运行。

application.property 文件中的我的属性:

spring.datasource.url=jdbc:h2:file:~/appboot
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

我的代码:

@Configuration
public class PersistentConfiguration {

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    @Primary
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }
}

我了解@ConfigurationProperties 没有从我的文件中读取属性。如果我在 builder 方法中提供如下详细信息,则效果很好:

return DataSourceBuilder.create()
.url("jdbc:h2:file:~/appboot")
.username("sa")
.password("")
.driverClassName("org.h2.Driver")
.build();

我的 pom.xml 文件有:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

我的存储库类:

import org.springframework.data.jpa.repository.JpaRepository;

import com.boot.model.Shipwreck;

public interface ShipwreckRepository extends JpaRepository<Shipwreck, Long>{

}

我的主要课程:

@SpringBootApplication
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}

错误:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class] 中定义名称为“flywayInitializer”的 bean 创建错误:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException:驱动程序类名称需要 jdbcUrl。 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1699) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:495) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:333) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1277) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.boot.SpringApplication.run(SpringApplication.java:1265) [spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 com.boot.App.main(App.java:15) [classes/:na] 原因:java.lang.IllegalArgumentException:驱动程序类名称需要 jdbcUrl。 在 com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1059) ~[HikariCP-2.7.9.jar:na] 在 com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:109) ~[HikariCP-2.7.9.jar:na] 在 org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:51) ~[flyway-core-5.0.7.jar:na] 在 org.flywaydb.core.internal.database.DatabaseFactory.createDatabase(DatabaseFactory.java:67) ~[flyway-core-5.0.7.jar:na] 在 org.flywaydb.core.Flyway.execute(Flyway.java:1634) ~[flyway-core-5.0.7.jar:na] 在 org.flywaydb.core.Flyway.migrate(Flyway.java:1168) ~[flyway-core-5.0.7.jar:na] 在 org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.0.5.RELEASE.jar:2.0.5.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695) ~[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] ...省略了18个常用框架

如果我还需要提供任何其他信息,请告诉我。

【问题讨论】:

  • 您是如何访问数据库的? RepositoryDataSource JdbcTemplate ?例如,如果您使用Spring Data JPA Repositories,则除了属性之外什么都不需要。
  • 我想我只通过 JPA 存储库使用它。 spring-boot-starter-data-jpa 被添加到我的 pom.xml 中,我用我的存储库类编辑了我的问题。它现在是空的。
  • 可以添加相应的代码来避免任何假设吗?

标签: maven spring-boot java-10 configurationproperties


【解决方案1】:

Spring Boot 为我们提供了许多创建应用程序的便利,例如,如果您想使用数据库 H2,您只需将依赖项添加到您的 pom 文件中,并在 application.properties 文件中添加相应的属性,那里是一些允许数据库和应用程序之间透明通信的默认属性,例如:

# To save the db in memory
#spring.datasource.url=jdbc:h2:mem:test

# To save the db in a file within the project
spring.datasource.url=jdbc:h2:./db/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.platform=h2

我的意思是DataSource 配置由spring.datasource.* 中的外部配置属性控制,因此您不需要定义PersistentConfiguration 类,正确定义这些属性就足够了,应用程序运行良好。确保在 pom.xml 文件中添加了依赖项 H2

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>runtime</scope>
</dependency>

你也可以查看数据库中的数据:

http://<host>:<port>/<context-path>/h2-console/

例如:

http://localhost:8080/my-first-app/h2-console

【讨论】:

    【解决方案2】:

    检查这是否有帮助:

    import java.util.Map;
    
    import javax.sql.DataSource;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.jdbc.DataSourceBuilder;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ConfigurationProperties
    public class PersistentConfiguration {
    
      Map<String, String> datasource;
    
      public DataSource getDatasources() {
        System.out.println("datasources ==="+datasource);
        return DataSourceBuilder.create()
            .url(datasource.get("url"))
            .username(datasource.get("username"))
            .password(datasource.get("password"))
            .driverClassName(datasource.get("driver-class-name"))
            .build();
      }
    
      public void setDatasource(Map<String, String> datasource) {
        this.datasource = datasource;
      }
    }
    

    application.properties 文件:

    datasource[url]=jdbc:mysql://localhost:3000/andatabase?useSSL=false
    datasource[username]=root
    datasource[password]=password
    datasource[driver-class-name]=com.mysql.jdbc.Driver
    

    在控制器中:

    @Autowired
    PersistentConfiguration config;
    
    @GetMapping("/language")
    public ResponseEntity control() {
    
    config.getDatasources();
    
    return new ResponseEntity("success", HttpStatus.OK);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-07
      • 2013-02-03
      • 2018-07-14
      • 2012-04-04
      • 1970-01-01
      • 2018-12-08
      • 2017-12-05
      • 1970-01-01
      相关资源
      最近更新 更多