读取静态方法中的 application.properties

huangapple go评论90阅读模式
英文:

Reading application.properties in static method

问题

我在`SpringApplicationBuilder`中静态地放置了一些属性的代码如下

    @SpringBootApplication(scanBasePackages = { "com" })
    public class Application extends SpringBootServletInitializer {
    	public static void main(String[] args) {
    		SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
    		parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
    				.properties("server.port:9093")).web(WebApplicationType.SERVLET).run(args);
    	}
    }

我想将这些属性移动到`application.properties`文件中

    application.port.query=9093

我使用了`@Value`从应用程序文件中读取但是我得到了null在静态方法中读取数据还有其他方法吗
英文:

I have the below code when i put some properties statically in SpringApplicationBuilder

@SpringBootApplication(scanBasePackages = { "com" })
public class Application extends SpringBootServletInitializer {
	public static void main(String[] args) {
		SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
		parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
				.properties("server.port:9093")).web(WebApplicationType.SERVLET).run(args);
	}
}

I want to move the properties to the file application.properties

application.port.query=9093

I used @value to read from the application file, but i get null. Is there another way to read data in a static method?

答案1

得分: 1

尝试类似这样的代码

@Configuration
@ConfigurationProperties("application")
class A {
     public static int queryPort;
     @Value("${port.query:9093}")
     public void setQueryPort(final int portQuery){
         A.queryPort = portQuery;
     }
 }

public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
    parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
    .properties(A.queryPort)).web(WebApplicationType.SERVLET).run(args);
    }
}
英文:

try something like this

@Configuration
@ConfigurationProperties("application")
class A {
     public static int queryPort;
     @Value("${port.query:9093}")
     public void setQueryPort(final int portQuery){
         A.queryPort = portQuery;
     }
 }

public class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder = new SpringApplicationBuilder(Application.class);
    parentBuilder.child(RestConfiguration.class, SwaggerConfig.class)
    .properties(A.queryPort)).web(WebApplicationType.SERVLET).run(args);
    }
}

答案2

得分: 0

以下是翻译好的部分:

你正在尝试实现的首选方法是使用 Spring Bean 本身,它是你所有配置的父级 org.springframework.core.env.Environment

@SpringBootApplication(scanBasePackages = {"com"})
public class Application {

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

        Environment environment = applicationContext.getBean(Environment.class);
        String propertyValue = environment.getProperty("any.property.from.configuration");
    }
}

在你的用例中,只需获取 Environment 的 Bean,然后从 Environment 对象中获取任何属性值。

英文:

The preferable way to what you are trying to achieve is to use the spring bean itself which is the parent of your all configurations org.springframework.core.env.Environment

@SpringBootApplication(scanBasePackages = {"com"})
public class Application {

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

        Environment environment = applicationContext.getBean(Environment.class);
        String propertyValue = environment.getProperty("any.property.from.configuration")
    }
}

In your use-case, just get the bean of Environment and from Environment object get any property value.

huangapple
  • 本文由 发表于 2020年10月26日 22:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64538829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定