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 ...
随机推荐
- 010——MATLAB运行错误跳到下一个循环
(一)MATLAB运行错误跳到下一个循环 :%文件的个数 try %运行的程序放到这里 catch continue%假如上面的没法执行则执行continue,到下个循环 end
- java文件夹上传下载组件
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- CF316G3 Good Substrings 广义后缀自动机
太累了,刷刷水~ code: #include <bits/stdc++.h> #define N 500005 #define LL long long #define setIO(s) ...
- Web API系列(二) Filter的使用以及执行顺序
在WEB Api中,引入了面向切面编程(AOP)的思想,在某些特定的位置可以插入特定的Filter进行过程拦截处理.引入了这一机制可以更好地践行DRY(Don’t Repeat Yourself)思想 ...
- 使用apache 的 ab命令压力测试nginx服务器
nginx压力测试方法: #ab命令 #安装ab #Centos系统 yum install apr-util #Ubuntu系统 sudo apt-get install apache2-utils ...
- 2017.10.7 国庆清北 D7T1 计数
题目描述 给出m个数a[1],a[2],…,a[m] 求1~n中有多少数不是a[1],a[2],…,a[m]的倍数. 输入输出格式 输入格式: 输入文件名为count.in. 第一行,包含两个整数:n ...
- CSS角度单位:deg、grad、rad、turn
1.deg 度(Degress).一个圆共360度 90deg = 100grad = 0.25turn ≍ 1.570796326794897rad -moz-transform: rotate(2 ...
- ICEM-模型导入失败的解决方法
原视频下载地址:https://yunpan.cn/cxITx5uXY6dAp 访问密码 ade8
- 读取本地word 浏览器下载(设置编码格式)
String filePath = "C:\\word\\报告.doc"; BufferedWriter bos = null; BufferedReader bis = null ...
- nodejs express cheerio request爬虫
const express = require('express') const cheerio = require('cheerio') const request = require(" ...