Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以将null设置为Spring中@value的默认值吗?_Java_Spring_Spring Annotations - Fatal编程技术网

Java 我可以将null设置为Spring中@value的默认值吗?

Java 我可以将null设置为Spring中@value的默认值吗?,java,spring,spring-annotations,Java,Spring,Spring Annotations,我目前正在使用@Value Spring 3.1.x注释,如下所示: @Value("${stuff.value:}") private String value; 如果属性不存在,则会将空字符串放入变量中。我希望将null作为默认值,而不是空字符串。当然,我还希望避免未设置stuff.value属性时出现错误。必须设置的null值。例如,我使用字符串@null,但也可以将空字符串用作null值 <bean id="propertyConfigurer" class="org.sprin

我目前正在使用@Value Spring 3.1.x注释,如下所示:

@Value("${stuff.value:}")
private String value;

如果属性不存在,则会将空字符串放入变量中。我希望将null作为默认值,而不是空字符串。当然,我还希望避免未设置stuff.value属性时出现错误。

必须设置的null值。例如,我使用字符串
@null
,但也可以将空字符串用作null值

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- config the location(s) of the properties file(s) here -->
    <property name="nullValue" value="@null" />
</bean>
请注意:上下文名称空间目前不支持空值。你不能用

<context:property-placeholder null-value="@null" ... />


使用Spring3.1.1测试时,我给@nosebrain评分,因为我不知道“null值”,但我更喜欢避免使用null值,特别是因为在属性文件中很难表示
null

但这里有一种替代方法,使用null with out
null值
,因此它将使用what-ever属性占位符

public class MyObject {

   private String value;

   @Value("${stuff.value:@null}")
   public void setValue(String value) {
      if ("@null".equals(value)) this.value = null;
      else this.value = value;
   }
}
就我个人而言,我更喜欢我的方式,因为可能以后您希望
stuff.value
是逗号分隔的值,或者枚举开关更容易。单元测试也更容易:)

编辑:基于您对使用枚举的评论和我不使用null的意见。

@Component
public class MyObject {

    @Value("${crap:NOTSET}")
    private Crap crap;

    public enum Crap {
        NOTSET,
        BLAH;
    }
}
以上对我来说很好。您可以避免null。如果您的属性文件想要显式设置它们不想处理它,那么您可以这样做(但您甚至不必指定它,因为它将默认为NOTSET)


null
非常糟糕,与
NOTSET
不同。这意味着spring或unit test没有设置它,这就是为什么存在差异的原因。我仍然可能会使用setter符号(上一个示例),因为它更容易进行单元测试(在单元测试中很难设置私有变量)。

这确实很旧,但现在可以使用Spring EL,例如

@Value(${stuff.Value:{null}”)

请参阅。

感谢@vorburger:

@Value("${email.protocol:#{null}}")
String protocol;

将字符串值设置为null,无需任何其他配置

如果需要将空(0长度)“”字符串作为@Value default注入,请使用SPEL(spring表达式语言),如下所示:

@Value("${index.suffix:#{''}}") 
private String indexSuffix;
#{'}只是将一个空字符串作为@Value的默认值


通过yl

字符串值就是一个例子,我真的需要将其用于枚举值,其中空字符串只会导致转换错误。使用@null可以将null设置为“no Enum set”,这正是我所需要的。更重要的是,我不建议使用null,而是建议使用表示not set的枚举。这需要将Enum.NOTSET放入所有属性文件(>30),这似乎是很多工作,几乎没有任何好处…我没有尝试过,但理论上不会
@Value(${stuff.Value:NOTSET}”)
工作?是的,这是一个很好的解决方案。不幸的是,我无法更改枚举(dep),所以我必须使用null。否则您是对的,NOTSET将起作用。Java配置与此等效吗?提到使用${some.param:#{null}}时,我不需要设置null值,这似乎是默认值?(在Spring Boot应用程序中)简单使用SPEL${stuff.value:#{null}}的强大功能,正如vorburger和In所建议的,ben3000和JRichardsz答案所需的配置似乎要少得多。有一种更新的方法可以将
null
设置为默认值,请参阅此处在Spring Boot上一个2019版本中使用的最新答案:2.1.7.RELEASE。。。这应该被选为问题的答案。
@Value("${email.protocol:#{null}}")
String protocol;
@Value("${index.suffix:#{''}}") 
private String indexSuffix;