首页 文章

带前缀的ConfigurationProperties不起作用

提问于
浏览
0

我在 Spring 季启动时非常新,并创建了我的第一个应用程序 . 在创建数据源时,我正在使用带有前缀和属性的@ConfigurationProperties从application.property中读取 .

但是,这个设置似乎对我不起作用,我的程序没有运行 .

来自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 没有从我的文件中读取属性 . 如果我在构建器方法中提供如下详细信息,则效果很好:

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:在类路径资源中定义名为'flywayInitializer'的bean时出错[org / springframework / boot / autoconfigure / flyway / FlywayAutoConfiguration $ FlywayConfiguration.class]:init方法的调用失败;嵌套异常是java.lang.IllegalArgumentException:driverClassName需要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 . org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java)中的support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:573)〜[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE] :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] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)〜[spring-beans-5.0.9.RELEASE . jar:5.0.9.RELEASE] at 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] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:304)〜[spring-beans-5.0.9.RELEASE.jar:5.0.9 . 在org.springframework.context的org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)〜[spring-beans-5.0.9.RELEASE.jar:5.0.9.RELEASE]中的RELEASE] . org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:859)中的support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1089)〜[spring-context-5.0.9.RELEASE.jar:5.0.9.RELEASE] )〜[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] at 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) org.springframework.boot.SpringApplication.run中的[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE](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 . 在com.boot.App.main上运行(SpringApplication.java:1265)[spring-boot-2.0.5.RELEASE.jar:2.0.5.RELEASE](app.java:15)[classes /:na]引起的:java.lang.IllegalArgumentException:driverClassName需要jdbcUrl . at com.zaxxer.hikari.HikariConfig.validate(HikariConfig.java:1059)〜[HikariCP-2.7.9.jar:na] at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:109)~ [HikariCP- 2.7.9.jar:na] at org.flywaydb的org.flywaydb.core.internal.util.jdbc.JdbcUtils.openConnection(JdbcUtils.java:51)〜[flyway-core-5.0.7.jar:na] . core.internal.database.DatabaseFactory.createDatabase(DatabaseFactory.java:67)〜[flyway-core-5.0.7.jar:na] at org.flywaydb.core.Flyway.execute(Flyway.java:1634)〜[flyway-core-5.0.7.jar:na]在org.springframework上的org.flywaydb.core.Flyway.migrate(Flyway.java:1168)〜[flyway-core-5.0.7.jar:na] . org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory中的boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)〜[spring-boot-autoconfigure-2.0.5.RELEASE.jar:2.0.5.RELEASE] .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个常用帧

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

3 回答

  • 0

    检查这是否有帮助:

    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);
    }
    
  • 0

    来自堆栈跟踪:

    java.lang.IllegalArgumentException:driverClassName需要jdbcUrl .

    你有没有 h2 依赖于pom?如果没有,请尝试在pom中添加以下内容 .

    <dependency>
      <groupId>com.h2database</groupId>
      <artifactId>h2</artifactId>
      <version>1.4.197</version>
    </dependency>
    

    而且,顺便说一下,你不必手动创建 DataSource bean . 它将使用配置中的属性创建 .

  • 0

    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
    

相关问题