嗨,我想从属性文件autowire布尔值已引用以下链接与地图网址
Spring属性(property-placeholder)自动装配
但我想自动连线一个布尔属性,也有提问题 Spring Autowire原始布尔值 Spring Autowire原始布尔值
但那是bean值,在我的情况下,我想使用点分隔的属性值来做同样的事情。
${does.it.allow}
//失败并给出String不能转换为boolean
#{does.it.allow}
//这不会给出使用name定义的bean / property 不 但我有正确的属性文件,它证明容器能够加载它因为第一个错误。
它对我的原始布尔值不起作用。但它与布尔类型有关。
这是我的属性文件的spring配置声明:
<context:property-placeholder location="classpath:path/to/file/configuracion.properties" />
这是我在属性文件中的内容:
my.property=false
这是我成功的服务类:
...
@Service
public class MyServiceImpl implements MyService{
...
@Value("${my.property}")
private Boolean nameOfProperty;
...
至少从Spring 5开始(我没有测试以前的版本),你可以自动装配boolean原语。
在应用程序属性中
my.property=true
在你的班上:
@Value("${my.property}")
private boolean myProperty;