如何从此配置文件中获取属性到另一个java类?

时间:2016-11-03 16:05:31

标签: java spring spring-boot

我能够从application.properties文件中提取属性。但我无法在另一堂课中使用它。

ConfigFile实现

package com.springweb.springApplication.config;    
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;    

@Component
public class AppConfig {

private final String App;    
private final String servers;    

@Autowired
public AppConfig(@Value("${Application}") String App,
                @Value("${APP_SERVERS}") String servers) {
    this.App = App;
    this.servers = servers;

    System.out.println("================ " + servers + "================ ");
}    

public String getApp() {
    return App;
}

public String getServers() {
    return servers;
}

}

类代码SNippet

public class MyFirstClass{

private String RED = "RED";
private String GREEN = "GREEN";
private String YELLOW = "YELLOW";   


public List<ModelSet> findall() {


    Properties prop = new Properties();
    InputStream input = null;

            try {
                input = new
                FileInputStream("C:/Users/Jackie/workspace/springApplication/lib/myConfig.properties");
                prop.load(input);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }                           


        String servers = prop.getProperty("APP_SERVERS");
        String Application = prop.getProperty("Application");
}

我想替换inputSteam并在内部使用属性文件。而不是有流来查找文件。我想从配置文件中获取这些变量。我试着看http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html,但我找不到如何将变量调用到另一个类中。我对如何应对这一点只有一点点线索。现在我相信我必须做类似.getproperty或env属性的事情。

4 个答案:

答案 0 :(得分:0)

AppConfig注入MyFirstClass课程。

编辑:我可能不明白你在问什么。

以下是我认为你想要的选项1: 总结为&#34;我在AppConfig中注入了东西,并希望在MyFirstClas&#34;中引用注入的东西。

  1. 保留AppConfig类的配置。
  2. AppConfig类型的@Inject(或@Autowired)成员添加到MyFirstClass类。
  3. 调用AppConfig getters以检索配置值。
  4. 以下是我认为你想要的选项2: 总结为&#34;我在某些类中注入了属性,并希望在其他类中注入更多属性&#34;

    1. 在Spring配置中创建PropertyPlaceholderConfigurer(在Spring Reference或Google上搜索PropertyPlaceholderConfigurer)
    2. 将属性注入您的&#34;其他一些类&#34;使用与此类似的配置:<property name="memberNameInYourClass" value="${propertyName.from.properties.file}"/>

答案 1 :(得分:0)

  

我能够从application.properties中提取属性   文件。但我无法在另一堂课中使用它。

您无法使用@Value获取MyFirstClass中的属性,因为MyFirstClass不是Spring组件,因此忽略了@Value。

将MyFirstClass注释为Spring组件并使用@Value注入属性应解决问题

更新:在调用构造函数后完成注入

@Component
public class MyFirstClass{

    private String app;

    private String servers;

    @Autowired
    public MyFirstClass(@Value("${Application}") String App,
            @Value("${APP_SERVERS}") String servers) {
        this.App = App;
        this.servers = servers;

        System.out.println("================ " + servers + "================ ");
    } 

    private String RED = "RED";
    private String GREEN = "GREEN";
    private String YELLOW = "YELLOW";   


    public List<ModelSet> findall() {
        // process findall Spring will resolve properties for you!
    }
}

答案 2 :(得分:0)

您可以通过向班级注入环境来获取属性。

public class MyFirstClass{

@Autowired
private Environment env;

private String RED = "RED";
private String GREEN = "GREEN";
private String YELLOW = "YELLOW";   


public List<ModelSet> findall() {


Properties prop = new Properties();
InputStream input = null;

        try {
            input = new
            FileInputStream(env.getProperty("a.b.c.path"));
            prop.load(input);
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }                           


    String servers = prop.getProperty("APP_SERVERS");
    String Application = prop.getProperty("Application");
}
在您的application.properties文件中

,定义类似

的路径
a.b.c.path = /home/jackie/asd

答案 3 :(得分:0)

班级

@Component
public class MyFirstClass{


@Value("${Application}")
private String Application;
@Value("${APP_SERVERS}")
private String servers; 


private String RED = "RED";
private String GREEN = "GREEN";
private String YELLOW = "YELLOW";   


@PostConstruct
public List<ModelSet> findall() {

//normal code that called the properties variables will be read automatically.
System.out.println(Application + "this is application from properties");
}

<强>输出

Hellosir this is application from properties

<强> Application.properties

Application = Hellosir
Servers = server1,server2,server3

这适合我和我需要的东西。我在类上面添加了@Component,并在下面添加了@PostConstruct。现在它将打印属性值。