简体   繁体   English

如何通过@Value注释强制进行属性注入?

[英]How to make a property injection via @Value annotation mandatory?

I have the following Test.java POJO class being populated from a property file using the @ConfigurationProperties annotation. 我使用@ConfigurationProperties批注从属性文件填充以下Test.java POJO类。 I have seen the usage of @Required annotation to make it a mandatory. 我已经看到使用@Required注释使其成为必需的。

Rather than defining annotations at the setter method level, are there any annotations or options within the @Value annotation that I can use for defining conditions like Mandatory, NotNull, etc? 而不是在setter方法级别定义注释,在@Value注释中是否有任何注释或选项可用于定义Mandatory,NotNull等条件?

@Component
@ConfigurationProperties(prefix = "com.test")
public class Test {
  private String name;

  @Required
  public void setName(String name) {
    name = name;
  }

  public String getName(String name) {
    name = name;
  }
}

Is this the right and only way for making a particular attribute mandatory? 这是使特定属性成为强制性的唯一方法吗? What are the other such annotations I could use for such conditions or validations purpose? 我可以用于此类条件或验证目的的其他此类注释是什么?

You can use validation for the properties, just not in the way you envisaged. 您可以对属性使用验证,而不是按照您设想的方式。 What you can do is use standard validation annotations (like @NotNull etc.) on the fields (or setters) themselves. 您可以做的是在字段(或设置器)本身上使用标准验证注释(如@NotNull等)。

For example 例如

@NotNull
@Size(min=2, max=10)
private String name;

Check out this part of the documentation 查看文档的这一部分

What the documentation essentially says, is that you simply have to have a compatible JSR303 validator implementation on the classpath, and use the relevant annotations. 文档基本上说的是,您只需在类路径上具有兼容的JSR303验证器实现,并使用相关的注释。 Spring Boot will take care of the rest Spring Boot将负责其余部分

According to the Spring docs (currently 4.1.6.RELEASE), the Value annotation only has a single property, value , containing the value of the property. 根据Spring 文档 (目前为4.1.6.RELEASE), Value注释只有一个属性value ,包含属性的值。 You can put a Spring EL expression in this, but that won't let you explicitly express notions like non-nullity. 您可以在其中放置一个Spring EL表达式,但这不会让您明确表达非归零等概念。

Further, in your code snippet you're using @ConfigurationProperties which is an alternative approach to configuring property values, compared to the @Value annotation. 此外,在您的代码片段中,您使用@ConfigurationProperties ,这是一种配置属性值的替代方法,与@Value注释相比。

The way you're doing it, your Java getter/setter names need to map to the property names, ie prefix "com.test" + getName() / setName() matches property com.test.name=... So, you don't need the @Value annotation to tell Spring what property to use. 你的方式,你的Java getter / setter名称需要映射到属性名称,即前缀“com.test”+ getName() / setName()匹配属性com.test.name=...所以,你不需要@Value注释来告诉Spring要使用什么属性。

With the @Value approach, your getters/setters don't have to match the property names, but you do have to annotate each property eg @Value("${com.test.name}") and on the class, a @PropertySource annotation pointing to the properties file that contains com.test.name=... 使用@Value方法,您的getter / setter不必匹配属性名称,但您必须注释每个属性,例如@Value("${com.test.name}")和类, @PropertySource注释指向包含com.test.name=...的属性文件

I found a couple of blog posts with code examples that use the 2 different ways to inject the same properties: http://blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.html and http://blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html 我发现了一些带有代码示例的博客文章,它们使用两种不同的方式来注入相同的属性: http//blog.codeleak.pl/2014/09/using-configurationproperties-in-spring.htmlhttp:// blog.codeleak.pl/2014/09/testing-mail-code-in-spring-boot.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM