jsp 中spring组合注解与元注解实例详解
摘要: 注解(annotation),也叫元数据。一种代码级别的说明。它与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明
1. 可以注解到别的注解上的注解称为元注解,被注解的注解称为组合注解,通过组合注解可以很好的简化好多重复性的注解操作
2. 示例组合注解
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;
import java.lang.annotation.*;
@target(elementtype.type)
@retention(retentionpolicy.runtime)
@documented
@configuration
@componentscan
public @interface groupannotation {
string[] value() default {};
}
代码解释:组合@configuration 与 @componentscan 元注解,并覆盖value参数
3. 编写普通bean
@servicepublic class demoservice
{
public void sys()
{ system.out.println("组合注解示例");
}
}
4. 使用组合注解的配置类
@groupannotation("com.xuanwu.annotation")
public class democonfig {
}
5. 运行
public class main {
public static void main(string[] args) {
annotationconfigapplicationcontext context = new
annotationconfigapplicationcontext(democonfig.class);
demoservice demoservice = context.getbean(demoservice.class);
demoservice.sys();
}
}
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!