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>>结果保持一致
新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。
京东地址:http://item.jd.com/11894632.html
当当地址:http://product.dangdang.com/23926195.html
亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6
淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail
或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。
相关推荐
Spring4.1-HibernateAnnotation >记住在配置事务管理器时,要设置sessionFactory
这里zip压缩包囊括了学习Spring过程中用到的所有的jar包; 有: commons-logging-1.2.jar spring-beans-4.0.4.RELEASE.jar spring-context-4.0.4.RELEASE.jar spring-core-4.0.4.RELEASE.jar spring-expression-...
spring-security-helloworld-annotation
point-cloud-annotation-tool win10解压直接运行
github链接链接:https://github.com/springzfx/point-cloud-annotation-tool 在windows平台编译完成后的exe和dll文件,可以直接打开使用,并对原项目进行了优化,增加了列表和标注文件的显示功能,标注起来更快,...
因为 simple-spring-memcached 暂时和 MyBatis3 没办法直接整合(MyBatis3 不再需要 DAO 实现类),simple-spring-memcached annotation 在 interface 方法里不起作用。见我提交的 Bug。 ssm3-springcache-mybatis3-...
赠送jar包:mybatis-plus-annotation-3.2.0.jar; 赠送原API文档:mybatis-plus-annotation-3.2.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.2.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:geronimo-annotation_1.0_spec-1.1.1.jar; 赠送原API文档:geronimo-annotation_1.0_spec-1.1.1-javadoc.jar; 赠送源代码:geronimo-annotation_1.0_spec-1.1.1-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
Spring5.0.2 + Maven + IDEA + MySQL的完全注解的事务实现demo,供初学者学习和研究,不做商业用途。
赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
赠送jar包:mybatis-plus-annotation-3.5.1.jar; 赠送原API文档:mybatis-plus-annotation-3.5.1-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.5.1-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
赠送jar包:mybatis-plus-annotation-3.1.0.jar; 赠送原API文档:mybatis-plus-annotation-3.1.0-javadoc.jar; 赠送源代码:mybatis-plus-annotation-3.1.0-sources.jar; 赠送Maven依赖信息文件:mybatis-plus-...
spring-boot2.0全新教程实例20例.zip - [spring-boot-helloWorld](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-helloWorld):Spring Boot 的 hello World 版本 - [spring-boot-...
spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决方案 spring-boot-mybatis-annotation-mulidatasource:...
mybatis-plus-annotation-3.4.0.jar
NULL 博文链接:https://tonl.iteye.com/blog/2093289
NULL 博文链接:https://tonl.iteye.com/blog/1966822
Spring boot使用的各种示例,以最简单、最实用为标准 spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-...