java - 有关 @Value 错误行为的文档

标签 java spring dependency-injection annotations

任何人都可以向我指出 Spring 文档,该文档说明了当 @Value 注释中的表达式时会发生什么

  • 抛出异常
  • 返回空
  • 缺失

我需要这份行为的书面文档,以便能够自信地使用注释。

谢谢。

编辑:

注释的 JavaDoc 仅告诉您注释工作时会发生什么。没有任何关于错误行为的内容。

最佳答案

唯一清楚地(取决于您想要了解的深度)描述行为的文档是源代码本身。 @Value注释由 AutowiredAnnotationBeanPostProcessor 处理。

让我们一次看一个案例:

what will happen when the expression in a @Value annotation

Throws exception

例如,

public class CustomBean {
    private String value;

    public String getValue() {
        throw new NullPointerException();
    }
    public void setValue(String value) {
        this.value = value;
    }
}

@Component
public class MyComponent {
    private static final String a = "";
    @Value("#{customBean.value}")
    public String value;
}

<context:component-scan base-package="com.spring"></context:component-scan>
<context:property-placeholder location="classpath:values.properties"/> 
<bean id="customBean" class="com.spring.CustomBean" >
    <property name="value" value="bomb"></property>
</bean>

Spring 将尝试解决 @Value通过调用 getValue() 的值CustomBean 类型 bean 的 getter 。它在 org.springframework.expression.spel.support.ReflectivePropertyAccessor$OptimalPropertyAccessor#read(..) 中执行此操作(在通过 SpEL 堆栈解析 bean 和属性名称之后)。该方法有一个catch (Exception e)将捕获任何 Exception 的 block 并抛出 AccessException包裹捕获的东西Exception

what will happen when the expression in a @Value annotation

Returns null

鉴于此

public class CustomBean {
    private String value;

    public String getValue() {
        return null;
    }
    public void setValue(String value) {
        this.value = value;
    }
}

带注释的字段的值为 null因为这是已解析的值并且它是有效的。如果带注释的字段是原始类型,您将得到 NullPointerException当试图打开它时。

what will happen when the expression in a @Value annotation

Is missing

它可能会以两种方式丢失。首先,如果我们尝试引用一个不存在的 bean 属性。例如,

@Value("#{customBean.nonExistent}")
public String value;

在注入(inject)过程中,SpEL 评估将失败,因为属性 nonExistent在 bean 的类类型上找不到。对于好奇的人来说,这发生在 org.springframework.expression.spel.ast.PropertyOrFieldReference#readProperty() 。 Spring 尝试检查任何可能能够解决该问题的访问器。当它遍历所有这些而没有找到一个时,它会抛出一个 SpelEvaluationException 。 。

其次,该属性可能不存在

@Value("${properties.nonExistent}") // note $ vs #
public String value;

如果您没有包含此类属性的属性源,则可能会发生这种情况。已注册PropertyPlaceholderConfigurer (YMMV 与其他解决属性的策略)将通过您的PropertySource对象并尝试。如果没有找到相应的属性,您将得到 IllegalArgumentException它无法解析占位符。这发生在 org.springframework.util.PropertyPlaceholderHelper#parseStringValue(..) .

关于java - 有关 @Value 错误行为的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20271736/

相关文章:

java - 使用另一个 java 文件中的变量

java - 按特殊按钮退出

java - nGrinder WAR 抛出异常

php - Laravel setter 注入(inject)

java - 将 GWT 与持久对象一起使用的正确方法是什么?

java - 如何配置 eclipse 以使用额外参数运行我的 java 程序?

java - 我怎么知道是什么阻止了 spring webapp 的关闭

java - 找不到适合响应类型 [class org.springframework.http.ResponseEntity] 和内容类型 [application/octet-stream] 的 HttpMessageConverter

dependency-injection - InvalidOperationException : A circular dependency was detected for the service of type 'Microsoft. AspNetCore.Identity.UserManager

design-patterns - 直接在方法代码中调用 DI 容器(MVC Actions)