--Create your own marker annotation. --Add a condition depending on the presence or absence of annotations given to class, interface, and method.
Reference article: Slightly special Java annotation
Target
--Target class / interface and method.
Select ** TYPE, METHOD ** from java.lang.annotation.ElementType To do.
-When selecting multiple ElementType **, use the arc {}
and separate them with commas ,
.
SampleAnnotation.java
/**
*Annotations that can be added to class / interface and method
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@interface SampleAnnotation {
}
Retention
--Specify to be loaded into the VM at run time. Select ** RUNTIME ** from java.lang.annotation.RetentionPolicy.
SampleAnnotation.java
/**
*Annotation representing VM loading at runtime
*/
@Retention(RetentionPolicy.RUNTIME)
@interface SampleAnnotation {
}
[Usage example]
The class interface or method to which this annotation @IgnoreSession
is attached is
** Annotation for a marker indicating that it is not a process that handles a session **.
IgnoreSession.java
/**
*Annotation indicating that the process does not handle sessions
*Granted to processes that do not handle sessions
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreSession {
}
Describe the process to determine whether or not this annotation @ IgnoreSession
has been added.
bean
: represents the substance of class / interface
Normal case
//Verification for class / interface
if (targetClass.isAnnotationPresent(IgnoreSession.class)
//Processing when class / interface does not handle session
}
When using fw such as spring In the springAOP process, the entity of the bean to be verified may be the Proxy class. (2) Before "Verify class / interface", (1) "Verify whether it is a Proxy class for bean" is required.
Reference article: I was addicted to metaprogramming using beans with Spring AOP applied.
```java:Verify class / interface
//(1) Verify whether it is a Proxy class for the bean
Class<?> targetClass = null;
if (AopUtils.isAopProxy(bean)) {
targetClass = AopUtils.getTargetClass(bean);
} else {
targetClass = bean.getClass();
}
//② Verification for class / interface
if (targetClass.isAnnotationPresent(IgnoreSession.class)
//Processing when class / interface does not handle session
}
```
method
: Represents the substance of method
Validate method
//Verification for method
if (method.isAnnotationPresent(IgnoreSession.class)) {
//Processing when the method does not handle sessions
}
-Basic knowledge about Java annotation -Slightly special Java annotation -Matters that I was addicted to in metaprogramming using beans to which Spring AOP was applied