简体   繁体   English

类FIELD上的java注释不起作用

[英]java annotation on class FIELD is not working

I am checking spring dependency with custom annotation I have created a custom annotation in java, and I am applying this to a FIELD . 我正在使用自定义注释检查spring依赖性我已经在java中创建了一个自定义注释,我将其应用于FIELD Its working on METHOD but not on FEILD 它研究METHOD而不是FEILD

my custom annotation is class is 我的自定义注释是类

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Mandatory {
}

My target class is: 我的目标类是:

public class Student {  
    @Mandatory
    int salary;

    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

}

and my spring-configuration file is 我的spring-configuration文件是

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <context:annotation-config />

    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor">
    <property name="requiredAnnotationType" value="com.spring.core.annotation.Mandatory"/>
    </bean>


<bean id="student" class="com.spring.core.annotation.Student">
    <!-- <property name="salary" value="200000"></property> -->
</bean> 

</beans>

my app class is 我的app类是

public class App {

    public static void main(String[] args) {
        ApplicationContext context= new ClassPathXmlApplicationContext("annotations.xml");
        Student stud= (Student)context.getBean("student");
        System.out.println(stud);
    }
}

output is : Student [salary=0] 输出是: Student [salary=0]

expected: it should throw exception property salary required 预期:它应该抛出所需的异常财产salary

According to the JavaDoc, the RequiredAnnotationBeanPostProcessor looks at property setter methods, and not the property fields themselves. 根据JavaDoc, RequiredAnnotationBeanPostProcessor查看属性setter方法,而不是属性字段本身。

The motivation for the existence of this BeanPostProcessor is to allow developers to annotate the setter properties of their own classes with an arbitrary JDK 1.5 annotation to indicate that the container must check for the configuration of a dependency injected value. 存在此BeanPostProcessor的动机是允许开发人员使用任意JDK 1.5注释来注释其自己的类的setter属性 ,以指示容器必须检查依赖注入值的配置。

The emphasized portion is slightly confusing, so I confirmed by looking at the @Required annotations JavaDoc, which is the default annotation the RequiredAnnotationBeanPostProcessor checks for. 强调的部分有点令人困惑,所以我通过查看@Required注释JavaDoc来确认,这是RequiredAnnotationBeanPostProcessor检查的默认注释。 You should notice that the @Target meta-annotation only references METHOD (not FIELD), and the JavaDoc mentions the following: 您应该注意到@Target元注释仅引用METHOD(而不是FIELD),JavaDoc提到以下内容:

Marks a method (typically a JavaBean setter method) as being 'required': that is, the setter method must be configured to be dependency-injected with a value. 将方法(通常是JavaBean setter方法)标记为“required”:即,必须将setter方法配置为使用值依赖注入。

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

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