Ich überprüfe Federabhängigkeit mit benutzerdefinierten Annotation Ich habe eine benutzerdefinierte Annotation in Java erstellt, und ich wende dies an eine FIELD
. Seine Arbeit an METHOD
aber nicht auf FEILD
Java Annotation auf Klasse FIELD funktioniert nicht
meine benutzerdefinierte Anmerkung ist Klasse ist
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 {
}
Meine Zielklasse ist:
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;
}
}
und meine Feder-Konfigurationsdatei
<?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>
ist
Meine App-Klasse ist
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);
}
}
Ausgabe lautet: Student [salary=0]
erwartet: Es sollte Ausnahme Eigentum werfen salary