带前缀的ConfigurationProperties无法正常工作

时间:2018-10-24 11:30:58

标签: maven spring-boot java-10 configurationproperties

我在春季启动和创建我的第一个应用程序方面是一个新手。创建数据源时,我使用@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:在类路径资源[org / springframework / boot / autoconfigure / flyway / FlywayAutoConfiguration $ FlywayConfiguration.class]中创建名称为'flywayInitializer'的bean时出错:调用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.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:driverClassName需要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个常见框架

请让我知道是否还需要提供其他信息。

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

从堆栈跟踪中:

  

java.lang.IllegalArgumentException:driverClassName需要jdbcUrl。

您在pom中有h2依赖性吗?如果没有,请尝试在pom中添加以下内容。

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

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

答案 2 :(得分:0)

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

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文件中添加了依赖项H2

pom.xml

还可以检查数据库中的数据:

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

例如:

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