简体   繁体   English

Spring Boot无法从属性文件读取

[英]Spring Boot not reading from property file

I have tried every option on web but not able to set the values in following method: 我尝试了Web上的每个选项,但无法通过以下方法设置值:

@Configuration
@PropertySource("classpath:application.properties")
public class MyDataSource {
    @Value("${db.driver}") 
    private String DB_DRIVER;

    @Value("${db.url}")
    private String DB_URL;

    @Value("${db.username}")
    private String DB_USERNAME;

    @Value("${db.password}")
    private String DB_PASSWORD;


    @Bean
    public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean
    public DataSource getDataSource() {
         DriverManagerDataSource dataSource = new DriverManagerDataSource();
         dataSource.setDriverClassName(DB_DRIVER);
         dataSource.setUrl(DB_URL);
         dataSource.setUsername(DB_USERNAME);
         dataSource.setPassword(DB_PASSWORD);

         return dataSource;
     }
}

My application.properties is in main/resources folder and values can be seen in variables in debug mode. 我的application.properties位于main/resources文件夹中,并且在调试模式下的变量中可以看到值。 But on running app, it shows Property ' ' must not be empty. 但是在运行的应用程序上,它显示Property ' ' must not be empty.

EDIT: I am not sure what can be the issue in first case? 编辑:我不确定在第一种情况下可能是什么问题? So changed the application.property file as suggested and code as below : 因此,按建议更改了application.property文件,并更改了以下代码:

@Autowired
protected JdbcTemplate jdbcTemp;

public List<> getData(String id) {

    return jdbcTemp.query("SELECT ........,new RowMapper());
}

But getting java.lang.NullPointerException: 但是获取java.lang.NullPointerException:

If you're using Spring Boot, you can leverage application.properties file by declaring some entries: 如果您使用的是Spring Boot,则可以通过声明一些条目来利用application.properties文件:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass

In this way there is no need to implement a @Configuration class to setup database connection in Spring Boot. 这样,在Spring Boot中无需实现@Configuration类来设置数据库连接。

You can deepen more here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html 您可以在此处进一步了解: https//docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

By the way, take a look at spring.io 顺便看看spring.io

For the java configuration, using Environment instance to obtain the properties seems to be the preferred way, as by default ${..} placeholders are not resolved. 对于Java配置,使用Environment实例获取属性似乎是首选方法,因为默认情况下,不解析${..}占位符。

You may use something like this: 您可以使用以下方式:

@Autowired
private Environment env;

@Bean
public DataSource getDataSource() {
     DriverManagerDataSource dataSource = new DriverManagerDataSource();
     dataSource.setDriverClassName(env.getProperty("db.driver");

     .....

     return dataSource;
 }

Reasons from the Spring Jira : 春天吉拉的原因:

  1. it's inconsistent. 这是不一致的。 @PropertySource is the declarative counterpart to ConfigurableEnvironment#addPropertySource. @PropertySource是ConfigurableEnvironment#addPropertySource的声明性副本。 We do not add a PropertySourcesPlaceholderConfigurer in the latter case, and it would be inconsistent to do so in the former. 在后一种情况下,我们不添加PropertySourcesPlaceholderConfigurer,在前一种情况下这样做是不一致的。 it will not be what the user intended in every (or even most) cases. 在所有(甚至大多数)情况下,这都不是用户想要的。
  2. It is entirely possible, and even recommended that @Configuration class users forego $ {...} property replacement entirely, in favor of Environment#getProperty lookups within @Bean methods. 完全有可能,甚至建议@Configuration类用户完全放弃$ {...}属性替换,而在@Bean方法中使用Environment#getProperty查找。 For users following this recommendation, the automatic registration of a PropertySorucesPlaceholderConfigurer would be confusing when noticed, and generally undesirable as it's one more moving part. 对于遵循此建议的用户,PropertySorucesPlaceholderConfigurer的自动注册在被注意到时会造成混乱,并且通常是不受欢迎的,因为它是一个更动人的部分。 Yes, it's presence is benign, but not cost-free. 是的,它的存在是良性的,但不是免费的。 a PSPC must visit every bean definition in the container to interrogate PropertyValues, only to do nothing in cases where users are going with the Environment#getProperty approach. PSPC必须访问容器中的每个bean定义以查询PropertyValues,仅在用户使用Environment#getProperty方法时不执行任何操作。
  3. it is solvable (and already solved) by documentation. 它可以通过文档解决(并且已经解决)。 Proper use of @PropertySource, PropertySourcesPlaceholderConfigurer and other components is pretty comprehensively documented in the Javadoc for @Configuration already, and reference documentation is soon to follow. 正确使用@ PropertySource,PropertySourcesPlaceholderConfigurer和其他组件已经在Javadoc中针对@Configuration进行了全面的文档记录,并且很快就会有参考文档。

Me too was getting the error when tried to switch from MySQL to MSSQL. 尝试从MySQL切换到MSSQL时,我也遇到了错误。 The actual issue was I forgot to put the MSSQL dependency in the service. 实际的问题是我忘记将MSSQL依赖项放入服务中。 I used mssql-jdbc 我用mssql-jdbc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM