简体   繁体   English

无法在Spring Boot服务中从application.properties加载值

[英]Cannot load values from application.properties in a spring boot service

I have a spring boot REST service with the following structure: 我有一个具有以下结构的spring boot REST服务:

├── src
│   ├── main
│   │   ├── java
│   │   │   ├── daos
│   │   │   ├── db
│   │   │   │   └── DBConnector.java
│   │   │   ├── serviceComponents
│   │   │   │   └── serviceResources
│   │   │   │       ├── Application.java
│   │   │   │       ├── UserController.java
│   │   │   │       └── UserDAO.java
│   │   │   └── serviceRepresentations
│   │   │       └── User.java
│   │   └── resources
│   │       └── application.properties

The main files related to this question are: 与该问题有关的主要文件是:

Application.java : Application.java

package serviceComponents.serviceResources;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})
@ComponentScan
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

DBConnector.java : DBConnector.java

package db;

import java.sql.Connection;
import java.sql.SQLException;

import oracle.jdbc.pool.OracleDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


@Component
public class DBConnector {
    @Value("${current.app.url}")
    String dbUrl;

    @Value("${current.app.sid}")
    String dbSid;

    @Value("${current.app.port}")
    String dbPort;

    @Value("${current.app.user}")
    String dbUser;

    @Value("${current.app.password}")
    String dbPassword;

    String jdbcUrl = String.format("jdbc:oracle:thin:@%s:%s:%s", dbUrl, dbPort, dbSid);
    Connection conn;

    int count = 0;

    public DBConnector() {}

    @PostConstruct
    private void init() {

        try {
            Class.forName("oracle.jdbc.OracleDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        OracleDataSource ds = null;

        try {
            ds = new OracleDataSource();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        ds.setURL(jdbcUrl);

        try {
            conn = ds.getConnection(dbUser, dbPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Connection getDBConnection() throws SQLException {
        if (count == 0) {
            init();
            count++;
        }

        return conn;
    }
}

And the properties file: properties文件:

server.port = 8089

test.app.url = 127.0.0.1
test.app.sid = XE
test.app.port = 1521
test.app.user = foouser
test.app.password = barbazpass

current.app.url = test.app.url
current.app.sid = test.app.sid
current.app.port = test.app.port
current.app.user = test.app.user
current.app.password = test.app.password

And there is the relevant piece of code from the controller: 并且有来自控制器的相关代码段:

@RestController
@RequestMapping("/v1/user/")
public class UserController {

    DBConnector dbConnector = new DBConnector();
    UserDAO userDAO = new UserDAO(dbConnector);

However when I run the application, the fields from DBConnector.java annotated with @Value are not populated with the expected values from the configuration file. 但是,当我运行该应用程序时, DBConnector.java中用@Value注释的字段未填充配置文件中的期望值。 I know that spring boot detects application.properties as the default configuration file for a project. 我知道Spring Boot将application.properties检测为项目的默认配置文件。 What am I doing wrong? 我究竟做错了什么?

Perhaps try it with @Autowired: 也许用@Autowired尝试一下:

@Autowired DBConnector dbConnector = new DBConnector();

I've tried replacing parts of my current Spring app with some of your annotations where we differ, but the only thing that caused a problem that I've yet found (a NullPointerException, though, so perhaps not the same issue as you are experiencing) was when I removed the @Autowired annotation in my controller. 我尝试用一​​些不同的注释来替换当前Spring应用程序的某些部分,但这是唯一导致我发现的问题的原因(不过,是NullPointerException,所以可能与您遇到的问题不一样) )是在控制器中删除@Autowired注释时。

EDIT: I didn't notice at first, but my NPE is obviously caused because I'm just autowiring a reference, not instantiating. 编辑:起初我没有注意到,但是我的NPE很明显是因为我只是自动装配一个引用,而不是实例化。 For you, the equivalent would be: 对您来说,等效项是:

@Autowired DBConnector dbConnector;

EDIT 2: My application isn't instantiating exactly like yours, so this may not work, but I have a class like this: 编辑2:我的应用程序不完全像您的实例化,所以这可能无法正常工作,但我有一个这样的类:

@SpringBootApplication
public class ParameterClassConfigurator
{
    @Value("${my.first.property}") String property1;
    @Value("${my.second.property}") String property2;

    @Bean
    public ParameterClass parameterClass()
    {
        ParameterClass params = new ParameterClass(property1, property2);
        return params;
    }
}

The key seems to be that @Bean annotation. 关键似乎是@Bean批注。 Without it, my Spring boot app will not start, throwing org.springframework.beans.factory.UnsatisfiedDependencyException. 没有它,我的Spring启动应用程序将无法启动,并抛出org.springframework.beans.factory.UnsatisfiedDependencyException。

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

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