如何在没有属性文件的情况下使用 Spring 的 @Value?
How to use Spring's @Value without a properties file?
我目前有以下代码:
int port = System.getProperty("port") != null ?
Integer.parseInt(System.getProperty("port")) :
8080;
我不喜欢这个,想用 Spring 替换它。所以,我想我应该使用 @Value
注释。我不想为此创建一个属性文件。但是,我想通过注释获得一个默认值。
有没有一种方法可以在没有属性文件的情况下执行此操作,正确的代码实现是什么?我还需要 PropertySourcesPlaceholderConfigurer
吗?你能告诉我一个如何做到这一点的工作示例吗?
我没试过,但你可以使用任何 SpEL expression。您的代码可以重写为:
@Value("#{systemProperties['port'] ?: 8080}")
private int port;
请注意,我使用的是 safe navigation operator。
关于 PropertySourcesPalceholderConfigurer
,我认为您不需要,因为您的类路径中有 Spring 表达式语言依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
假设您使用的是基于 java 的配置。
@Bean
public static PropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
然后用@Value
注释一个字段
@Value("${port:8080}")
private int port;
这将检查给定 属性 port
的系统属性和环境。当启用 JNDI 时将被检查,当具有基于 servlet 的环境时,您也可以将其作为 servlet 变量。
PropertySourcesPlaceholderConfigurer
的使用不需要 属性 个文件,它需要 PropertySource
个文件,其中有几个不同的实现。
如果您不想注册 PropertySourcesPlaceholderConfigurer
,您可以恢复到 SpEL,但这会使它变得有点复杂(恕我直言,而且很难看)。
已测试并正常工作。
- 是的,您需要
PropertyPlaceholderConfigurer
但它不需要 属性 文件
- 像这样引用您的系统变量:
@Value("#{systemEnvironment['YOUR_VARIABLE_NAME'] ?: 'DEFAULT_VALUE'}"
代码示例:
package abc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Test {
public final String javaPath;
@Autowired
public Test(@Value("#{systemEnvironment['Path']}") String javaPath) {
this.javaPath = javaPath;
}
}
配置:
@Configuration
@ComponentScan("abc")
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
运行全部样本:
public class Runner {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Test bean = context.getBean(Test.class);
System.out.println(bean.javaPath);
}
}
希望对您有所帮助。
我目前有以下代码:
int port = System.getProperty("port") != null ?
Integer.parseInt(System.getProperty("port")) :
8080;
我不喜欢这个,想用 Spring 替换它。所以,我想我应该使用 @Value
注释。我不想为此创建一个属性文件。但是,我想通过注释获得一个默认值。
有没有一种方法可以在没有属性文件的情况下执行此操作,正确的代码实现是什么?我还需要 PropertySourcesPlaceholderConfigurer
吗?你能告诉我一个如何做到这一点的工作示例吗?
我没试过,但你可以使用任何 SpEL expression。您的代码可以重写为:
@Value("#{systemProperties['port'] ?: 8080}")
private int port;
请注意,我使用的是 safe navigation operator。
关于 PropertySourcesPalceholderConfigurer
,我认为您不需要,因为您的类路径中有 Spring 表达式语言依赖项:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
假设您使用的是基于 java 的配置。
@Bean
public static PropertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
然后用@Value
注释一个字段
@Value("${port:8080}")
private int port;
这将检查给定 属性 port
的系统属性和环境。当启用 JNDI 时将被检查,当具有基于 servlet 的环境时,您也可以将其作为 servlet 变量。
PropertySourcesPlaceholderConfigurer
的使用不需要 属性 个文件,它需要 PropertySource
个文件,其中有几个不同的实现。
如果您不想注册 PropertySourcesPlaceholderConfigurer
,您可以恢复到 SpEL,但这会使它变得有点复杂(恕我直言,而且很难看)。
已测试并正常工作。
- 是的,您需要
PropertyPlaceholderConfigurer
但它不需要 属性 文件 - 像这样引用您的系统变量:
@Value("#{systemEnvironment['YOUR_VARIABLE_NAME'] ?: 'DEFAULT_VALUE'}"
代码示例:
package abc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Test {
public final String javaPath;
@Autowired
public Test(@Value("#{systemEnvironment['Path']}") String javaPath) {
this.javaPath = javaPath;
}
}
配置:
@Configuration
@ComponentScan("abc")
public class Config {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
运行全部样本:
public class Runner {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
Test bean = context.getBean(Test.class);
System.out.println(bean.javaPath);
}
}
希望对您有所帮助。