AspectJ获取Annotation自定义注解的方法内容
2016-01-22 15:39
706 查看
自定义注解为:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tag {
public String tag();
}
需要自定义注解处理的Spring Bean对象:
@Service("fService")
public class FruitService implements IFruitService{
@Tag(tag="甜甜的")
public void eat(String fruitName){
System.out.println("======吃的水果是:"+fruitName);
}
}
现需要通过AspectJ对自定义注解进行处理,获取自定义注解Tag的方法tag()的内容:
String tag = (String)this.quietGetFromAnnotation("tag", annotation);
获取方法如下:
private Object quietGetFromAnnotation(String methodName, Annotation annotation) {
if (annotation == null) {
return null;
}
try {
// return annotation.annotationType().getDeclaredMethod(methodName).invoke(annotation);
Class<?> clazz = annotation.annotationType();
Method m = clazz.getDeclaredMethod(methodName);
return m.invoke(annotation);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
成功获取tag()的内容为: “甜甜的”
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Tag {
public String tag();
}
需要自定义注解处理的Spring Bean对象:
@Service("fService")
public class FruitService implements IFruitService{
@Tag(tag="甜甜的")
public void eat(String fruitName){
System.out.println("======吃的水果是:"+fruitName);
}
}
现需要通过AspectJ对自定义注解进行处理,获取自定义注解Tag的方法tag()的内容:
String tag = (String)this.quietGetFromAnnotation("tag", annotation);
获取方法如下:
private Object quietGetFromAnnotation(String methodName, Annotation annotation) {
if (annotation == null) {
return null;
}
try {
// return annotation.annotationType().getDeclaredMethod(methodName).invoke(annotation);
Class<?> clazz = annotation.annotationType();
Method m = clazz.getDeclaredMethod(methodName);
return m.invoke(annotation);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
成功获取tag()的内容为: “甜甜的”
相关文章推荐
- ASP.NET MVC从视图传递多个模型到Controller
- js 下载图片与下载文件的方式一样;保存至本地 ASP.NET 方式
- 在ASP.NET MVC 中获取当前URL、controller、action
- ASP.NET记录错误日志的实现方法
- 在ASP.Net中写系统日志
- Spring AspectJ AOP 完整示例
- ASP.Net中Session失效的一种编程思路
- 架构系列:ASP.NET 项目结构搭建
- 坎坷路:ASP.NET Core 1.0 Identity 身份验证(中集)
- Asp.Net中动态页面转静态页面
- ASP.NET 4.0尚未在 Web 服务器上注册 解决方法
- ASP.NET Core 1.0基础之静态文件处理
- ASP.NET 写错误日志以及错误引导页
- 让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求
- kali 2.0 启动metasploit服务
- Asp.Net WebAPI 中Cookie 获取操作方式
- ASP.NET Web API实现缓存的2种方式
- ASP.NET十分有用的页面间传值方法
- ASP.NET Web API中通过URI显示实体中的部分字段
- JSP与ASP.PHP的比較