Get annotations in spring in custom annotations (annotation) and AOP

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.