自定义注释用法

时间:2017-06-17 15:57:59

标签: java annotations

我想定义一个自定义注释,并以下面的方式将它与Inject注释一起使用。如何访问注入bean中的注释值?

注释定义,

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation{
    String name();
}

访问bean内的注释属性

@Component
public class Processor {
    Would like to know the value "abc" in constructor/post-constructor. How to access name method here ?
}

测试用法(值" abc"用于加载相应的配置并使bean表现得恰当),

@Inject
@CustomAnnotation("abc")
Processor myProcessor;

public void test()
{
    myProcessor.process(); // myProcessor will behave based on value "abc"
}