[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]Filed上注解,不能自定义一个组合注解?

上一篇:[备忘]spring里@Lazy(true)懒加载的bean,@Scheduled注解的手动调度实现
下一篇:[备忘]java将16进制字符串写入文件,再读出的代码

添加日期:2024/8/28 18:13:12 快速返回   返回列表 阅读192次
想写一个注解,组合
@Resource 
@Lazy
为一个新注解,结果证实无效,
这个变量不给注入了。

搜了半天:
@Resource注解是CommonAnnotationBeanPostProcessor.java处理的。

判断字段上是否存在@Resource注解
field.isAnnotationPresent(Resource.class)

field对象继承AccessibleObject.java,内有
    /**
     * @since 1.5
     */
    public Annotation[] getAnnotations() {
        return getDeclaredAnnotations();
    }

顶层接口中,注释说,getAnnotations应该返回所有注解,包括继承的。
getDeclaredAnnotations返回直接写的注解。
但是,这里getAnnotations调的getDeclaredAnnotations()方法,所以一码事。

所以,应该@Resouce注解只能直接写上,不能放到新注解里了。
----------
@Autowired注解是AutowiredAnnotationBeanPostProcessor.java处理的,应该类似。
------------------


public class AAA {

    @LazyResource
    @Autowired
    String aaa="1";

    public static void main(String[] args) throws NoSuchFieldException {
        Field field = AAA.class.getDeclaredField("aaa");
        Annotation[] an = field.getAnnotations();
        for(Annotation a:an){
            System.out.println(a);
        }
        System.out.println(field.isAnnotationPresent(Resource.class));
        System.out.println(field.isAnnotationPresent(LazyResource.class));
    }
}


输出:
@cn.com.tcsl.framework.spring.annotation.LazyResource(name=)
@org.springframework.beans.factory.annotation.Autowired(required=true)
false
true
--------------------------------
发现个文章:https://hankmo.com/posts/tech/annotated-element/
可以读读。
 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved