18点睛Spring4.1-Meta Annotation
18.1 Meta Annotation
- 元注解:顾名思义,就是注解的注解
- 当我们某几个注解要在多个地方重复使用的时候,写起来比较麻烦,定义一个元注解可以包含多个注解的含义,从而简化代码
- 下面我们用<<02点睛Spring4.1-Java Config>>里的源码进行元注解的改造
18.2 示例
18.2.1 spring注解分析
我们看看spring的@Service的源码:可看出@Service注解是由几个注解组合的包含@Component;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
String value() default "";
}
18.2.2 自定义元注解
下面的例子不见得举得合适,但是可简单演示元注解的作用
18.2.2.1 新建元注解
组合@Configuration,@PropertySource注解为一个注解@WiselyMetaAnnotation
package com.wisely.meta; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; @Configuration
@PropertySource("com/wisely/javaconfig/test.properties")
public @interface WiselyMetaAnnotation { }
18.2.2.2 去除已有配置
去除DemoConfig上的配置,使用新定义的组合元注解
package com.wisely.meta; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment; //@Configuration //声明是一个配置类
//@PropertySource("com/wisely/javaconfig/test.properties")
@WiselyMetaAnnotation
public class DemoConfig { @Bean //声明是一个bean
public DemoService demoBean(Environment environment){
DemoService demoService = new DemoService();
demoService.setWord(environment.getProperty("wisely.word"));
return demoService;
}
}
18.2.2.3 测试
package com.wisely.meta;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.meta");
DemoService demoService = context.getBean(DemoService.class);
System.out.println(demoService.sayHello());
context.close();
}
}
输出结果
Hello World
与<<02点睛Spring4.1-Java Config>>结果保持一致
18点睛Spring4.1-Meta Annotation的更多相关文章
- Spring4.0之四:Meta Annotation(元注解)
Spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持.注解为依赖注入,AOP(如事务)提供了更强大和简便的方式.这也导致你要是用一个相同的注解到许多不同的类中去.这篇文章介 ...
- 04点睛Spring4.1-资源调用
转发:https://www.iteye.com/blog/wiselyman-2210666 4.1 Resource spring用来调用外部资源数据的方式 支持调用文件或者是网址 在系统中调用p ...
- 14点睛Spring4.1-脚本编程
转发:https://www.iteye.com/blog/wiselyman-2212678 14.1 Scripting脚本编程 脚本语言和java这类静态的语言的主要区别是:脚本语言无需编译,源 ...
- 17点睛Spring4.1-@Conditional
17.1 @Conditional @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean; 应用场景 当某一个jar包在classpath中 ...
- 16点睛Spring4.1-TaskScheduler
转发:https://www.iteye.com/blog/wiselyman-2213049 16.1 TaskScheduler 提供对计划任务提供支持; 使用@EnableScheduling开 ...
- 15点睛Spring4.1-TaskExecutor
转发:https://www.iteye.com/blog/wiselyman-2212679 15.1 TaskExecutor spring的TaskExecutor为在spring环境下进行并发 ...
- 13点睛Spring4.1-Spring EL
13.1 Spring EL Spring EL-Spring表达式语言,支持在xml和注解中使用表达式,类似jsp的EL表达式语言; 本教程关注于在注解中使用Spring EL; Spring EL ...
- 12点睛Spring4.1-Spring Aware
12.1 Aware 我们设计的准则是解耦,这就意味着我们不能对Spring的IoC容器有直接的依赖,但是我们还是想我们的bean能识别容器的资源; 使用aware能让我们在应用的任意位置获得spri ...
- 11点睛Spring4.1-Property Editor
11.1 Propert Editor property editor是JavaBeans API的一项特性,用来字符和属性值之间的互相转换(如2014-03-02和Date类型的互相转换) spri ...
随机推荐
- js原型和原型链的问题
<script> //js原型和原型链的概念 functionperson(name){ this.name=name; } person.prototype.age=18; person ...
- MySQL 必会知识
一.为什么用自增列作为主键 1.如果我们定义了主键(PRIMARY KEY),那么InnoDB会选择主键作为聚集索引. 如果没有显式定义主键,则InnoDB会选择第一个不包含有NULL值的唯一索引作为 ...
- 在新浪SAE上搭建微信公众号的python应用
微信公众平台的开发者文档https://www.w3cschool.cn/weixinkaifawendang/ python,flask,SAE(新浪云),搭建开发微信公众账号http://www. ...
- Qt文件读写操作
原文地址:https://www.cnblogs.com/flowingwind/p/8336159.html QFile Class 1.read读文件 加载文件对象 QFile file(&qu ...
- [分享]Hidden Start - NTWind Software
https://bbs.pediy.com/thread-229336.htm [[other]] [分享]Hidden Start - NTWind Software 2018-6-29 09: ...
- 【一起来烧脑】读懂JQuery知识体系
背景 在现在就业的过程中,会运用JQuery是你的加分项,那么什么是JQuery,嗯,jquery是JavaScript的函数库,是一种轻量级的JavaScript库,写得少,做的多,导致jQuery ...
- LOJ6041. 「雅礼集训 2017 Day7」事情的相似度 [后缀树,LCT]
LOJ 思路 建出反串的后缀树,发现询问就是问一个区间的点的\(lca\)的深度最大值. 一种做法是dfs的时候从下往上合并\(endpos\)集合,发现插入一个点的时候只需要把与前驱后继的贡献算进去 ...
- win10系统中对本地端口进行简单分析
突然有事情涉及到本地端口,对相关内容进行了了解,这部分知识应该偏向运维,有些不好理解,查起来也零零散散的,理解的可能也有误……只记录一部分东西 想要查看本地端口的情况,在cmd下使用 netstat ...
- Codeforces 1239E. Turtle 折半
原文链接www.cnblogs.com/zhouzhendong/p/CF1239E.html 前言 咕了这么久之后,我的博客复活了! 题解 结论1 存在一个最优解\(A\)数组,满足\(\foral ...
- Android中进度条
<ProgressBar android:id="@+id/progress_bar" android:layout_width="match_parent&quo ...