Spring Boot 不读取数据源的 application.properties
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33873806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot not reading application.properties for DataSource
提问by SheppardDigital
I've put my DataSource details in /resources/application.properties file:
我已将我的数据源详细信息放在 /resources/application.properties 文件中:
spring.datasource.url = jdbc:mysql://localhost:3306/dsm
spring.datasource.username = root
spring.datasource.password = admin123
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
I've tried adding a direct reference to the properties file, this this didn't make any difference.
我试过添加对属性文件的直接引用,这没有任何区别。
@PropertySource("classpath:application.properties")
The error being generated is:
正在生成的错误是:
2015-11-23 14:44:30.232 INFO 54329 --- [on(2)-127.0.0.1] o.s.b.c.embedded.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2015-11-23 14:44:31.522 INFO 54329 --- [on(2)-127.0.0.1] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2015-11-23 14:44:31.535 INFO 54329 --- [on(2)-127.0.0.1] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2015-11-23 14:44:31.629 INFO 54329 --- [on(2)-127.0.0.1] org.hibernate.Version : HHH000412: Hibernate Core {4.3.11.Final}
2015-11-23 14:44:31.632 INFO 54329 --- [on(2)-127.0.0.1] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2015-11-23 14:44:31.636 INFO 54329 --- [on(2)-127.0.0.1] org.hibernate.cfg.Environment : HHH000021: Bytecode provider name : javassist
2015-11-23 14:44:31.824 INFO 54329 --- [on(2)-127.0.0.1] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
23-Nov-2015 14:44:32.005 WARNING [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver Not loading a JDBC driver as driverClassName property is null.
23-Nov-2015 14:44:32.008 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.jdbc.pool.ConnectionPool.init Unable to create initial connections of pool.
java.sql.SQLException: The url cannot be null
Looking at my properties file, both the dialect and the url are defined, and they're not being picked up, which is leading me to believe that the file isn't being ready.
查看我的属性文件,方言和 url 都已定义,并且没有被选中,这让我相信该文件尚未准备好。
My application class incase it helps:
我的应用程序类柜面它有帮助:
@SpringBootApplication
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
}
}
回答by SheppardDigital
It turns out the problem was that Intellj was deploying classes and files to TomCat which had previous been deleted.
事实证明,问题在于 Intellj 正在将类和文件部署到之前已被删除的 TomCat。
I cleared the 'target' folder in IntellJ and redeployed.
我清除了 IntellJ 中的“目标”文件夹并重新部署。
回答by sasanka
Note that a WebApplicationInitializer is only needed if you are building a war file and deploying it. If you prefer to run an embedded container (we do) then you won't need this at all. docs SpringBootServletInitializer
请注意,只有在构建 war 文件并部署它时才需要 WebApplicationInitializer。如果您更喜欢运行嵌入式容器(我们这样做),那么您根本不需要它。 文档 SpringBootServletInitializer
try this code
试试这个代码
@SpringBootApplication
@PropertySource("classpath:application.properties")
public class Application{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
回答by Coos
I had the same problem, turned out I put the properties file in /resources/META-INF. Changed the package to /resources/<default-package>
and it was fixed.
我遇到了同样的问题,结果我将属性文件放在 /resources/META-INF 中。将包更改为 /resources/<default-package>
并已修复。