One, custom annotations (annotation)
The purpose of custom annotations is to get annotations in reflection to obtain an explanation of the class, method, or property that the annotation is decorated with.
Package me.lichunlong.spring.annotation;
Import java.lang.annotation.Documented;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
customizing annotation-Related Settings
@Target ({Elementtype.method})
@Retention (Retentionpolicy.runtime)
@Documented
Public @interface Logannotation {
Custom annotation properties, default is set defaults
String desc () Default "no description information";
}
Second, the use of custom annotations
Package me.lichunlong.spring.service;
Import me.lichunlong.spring.annotation.LogAnnotation;
Import Me.lichunlong.spring.jdbc.JdbcUtil;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Service;
@Service
public class UserService {
Use the same as other annotations
@LogAnnotation (desc= "This is UserService")
public void Add () {
System.out.println ("UserService add ...");
}
}
Iii. obtaining annotations in AOP
Surround notification: The whole process of similar and dynamic agents
carries the parameter proceedingjoinpoint, and must have a return value, that is, the return of the target method
@Around (value = "Execution (* me.lichunlong.spring.service.*.* (..)) &&@annotation (log)")
Public Object Aroundmethod (Proceedingjoinpoint PJD,logannotation Log) {
Object result = null;
System.out.println (Log.desc ());
try {
System.out.println ("Pre-notification");
result = Pjd.proceed ();
System.out.println ("Post-notification");
} catch (Throwable e) {
SYSTEM.OUT.PRINTLN ("exception notification");
}
SYSTEM.OUT.PRINTLN ("return notification");
return result;
}
Get annotations in spring in custom annotations (annotation) and AOP