我在 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 回答
检查这是否有帮助:
application.properties文件:
在控制器中:
来自堆栈跟踪:
你有没有
h2
依赖于pom?如果没有,请尝试在pom中添加以下内容 .而且,顺便说一下,你不必手动创建
DataSource
bean . 它将使用配置中的属性创建 .Spring Boot为我们提供了许多创建应用程序的工具,例如,如果你想使用数据库H2,你只需要将依赖项添加到
pom
文件并在application.properties
文件中添加相应的属性,有一些默认属性允许透明数据库和应用程序之间的通信,例如:我的意思是
DataSource
配置由spring.datasource.*
中的外部配置属性控制,因此您不需要定义PersistentConfiguration
类,只需正确定义应用程序的这些属性即可 . 确保在pom.xml
文件中添加了依赖项H2
:您还可以检查数据库中的数据:
例如: