java Spring Boot 不从属性文件中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37144482/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:14:31  来源:igfitidea点击:

Spring Boot not reading from property file

javaspringspring-boot

提问by Sam

I have tried every option on web but not able to set the values in following method:

我已经尝试了网络上的每个选项,但无法在以下方法中设置值:

@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.propertiesis in main/resourcesfolder and values can be seen in variables in debug mode. But on running app, it shows Property ' ' must not be empty.

Myapplication.propertiesmain/resources文件夹中,值可以在调试模式下的变量中看到。但是在运行应用程序时,它显示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:

回答by Fernando Aspiazu

If you're using Spring Boot, you can leverage application.propertiesfile 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 @Configurationclass to setup database connection in Spring Boot.

这样就不需要@Configuration在 Spring Boot 中实现一个类来设置数据库连接。

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

回答by David Siro

For the java configuration, using Environmentinstance 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:

来自Spring Jira 的原因:

  1. it's inconsistent. @PropertySource is the declarative counterpart to ConfigurableEnvironment#addPropertySource. We do not add a PropertySourcesPlaceholderConfigurer in the latter case, and it would be inconsistent to do so in the former. 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. 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. 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.
  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.
  1. 这是不一致的。@PropertySource 是 ConfigurableEnvironment#addPropertySource 的声明对应物。在后一种情况下我们不添加 PropertySourcesPlaceholderConfigurer,在前一种情况下这样做会不一致。在每种(甚至大多数)情况下,这都不是用户想要的。
  2. 完全有可能,甚至建议@Configuration 类用户完全放弃 $ {...} 属性替换,转而使用 @Bean 方法中的 Environment#getProperty 查找。对于遵循此建议的用户,PropertySorucesPlaceholderConfigurer 的自动注册在注意到时会令人困惑,并且通常是不可取的,因为它是另一个移动部分。是的,它的存在是良性的,但不是免费的。PSPC 必须访问容器中的每个 bean 定义以询问 PropertyValues,只有在用户使用 Environment#getProperty 方法的情况下什么都不做。
  3. 它可以通过文档解决(并且已经解决)。@PropertySource、PropertySourcesPlaceholderConfigurer 和其他组件的正确使用已经在@Configuration 的Javadoc 中进行了相当全面的记录,参考文档很快就会跟进。

回答by Arun K

Me too was getting the error when tried to switch from MySQL to MSSQL. The actual issue was I forgot to put the MSSQL dependency in the service. I used mssql-jdbc

尝试从 MySQL 切换到 MSSQL 时,我也遇到了错误。实际问题是我忘记将 MSSQL 依赖项放在服务中。我使用了 mssql-jdbc