簡體   English   中英

Spring PropertySource返回null

[英]Spring PropertySource returns null

您好,我目前正在獲取.properties文件上的值。 我遇到了一個問題。 我正在使用Spring Boot。 請參閱下面的示例源;

Browser.java

@Controller
public class Browser {

    @Autowired
    private BrowserConfiguration conf;

    public Browser(){
        System.out.println("I am initializing...");
        System.out.println("Reading configuration files..."+conf);
        System.out.println("Starting selected browser...");
        System.out.println("Waiting for command execution...");
    }

}

BrowserConfiguration.java

@Configuration
@PropertySource("file:./properties/test-config.properties")
public class BrowserConfiguration {

    @Value( "${browser.target}" )
    private String browser;

    public String getBrowser() {
        return browser;
    }

    public void setBrowser(String browser) {
        this.browser = browser;
    }

    @Override
    public String toString(){
        return "Target Browser: "+getBrowser();
    }

}

Demo1Application.java

@SpringBootApplication
public class DemoApplication implements CommandLineRunner  {

    @Autowired
    private Browser browser;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.setBannerMode(Mode.OFF);
        app.run(args);
    }

    @Override
    public void run(String... arg0) throws Exception {
        // TODO Auto-generated method stub
        browser.runTest("test1");
    }
}

test-config.properties

browser.target=ie

控制台日志

2017-04-29 23:51:10.537  INFO 6912 --- [           main] com.example.Demo1Application             : Starting Demo1Application on Mikram-PC with PID 6912 (C:\Users\Mikram\sts-workspace\demo-1\target\classes started by Mikram in C:\Users\Mikram\sts-workspace\demo-1)
2017-04-29 23:51:10.540  INFO 6912 --- [           main] com.example.Demo1Application             : No active profile set, falling back to default profiles: default
2017-04-29 23:51:10.576  INFO 6912 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Sat Apr 29 23:51:10 SGT 2017]; root of context hierarchy
I am initializing...
Reading configuration files...null
Starting selected browser...
Waiting for command execution...
2017-04-29 23:51:11.051  INFO 6912 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
I am search now!
2017-04-29 23:51:11.062  INFO 6912 --- [           main] com.example.Demo1Application             : Started Demo1Application in 0.722 seconds (JVM running for 1.02)
2017-04-29 23:51:11.063  INFO 6912 --- [       Thread-2] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@67b467e9: startup date [Sat Apr 29 23:51:10 SGT 2017]; root of context hierarchy
2017-04-29 23:51:11.064  INFO 6912 --- [       Thread-2] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown

問題:

我似乎無法在Browser類中連接BrowserConfiguration。 我錯過了什么? 請為我指明正確的方向。 我現在迷路了。

您正在使用字段注入,但是在構造函數中訪問變量。 因為創建對象時會調用構造函數,所以Spring還沒有機會設置變量。 在這種情況下,您必須使用基於構造函數的注入:

@Autowired
public Browser(BrowserConfiguration conf) {
    this.conf = conf;

    //Use the variable here
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM