在spring 中使用自定义注解
2017-10-24 23:08
218 查看
spring加载自定义注解的条件
只要注解实现@Componentspring 中的源码如下:
protected void registerDefaultFilters() { //只扫描含有Component注解的类 this.includeFilters.add(new AnnotationTypeFilter(Component.class)); ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader(); try { this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false)); }catch (ClassNotFoundException ex) { } try { this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false)); logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning"); } catch (ClassNotFoundException ex) { // JSR-330 API not available - simply skip. } }
从上面的源码中可以看到,只要一个注解类实现@Componet,Spring就可以扫描到容器中。
spring ApplicationContextAware接口
当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。@Component public class SpringContextHelper implements ApplicationContextAware { private static ApplicationContext context = null; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } public static Object getBean(String name){ return context.getBean(name); } }
相关文章推荐
- J2EE项目使用自定义注解实现基于SpringMVC + Mybatis + Mysql的读写分离
- 深入Spring:自定义注解加载和使用
- 深入Spring:自定义注解加载和使用
- 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法
- 使用Spring自定义注解实现任务路由的方法
- 深入Spring:自定义注解加载和使用
- SpringAOP使用入门,如何对自定义的注解进行切面编程
- Spring Boot项目中自定义注解的使用
- spring 自定义注解及使用
- 深入Spring:自定义注解加载和使用
- SpringBoot使用自定义注解+拦截器 实现日志记录
- 使用Spring自定义注解生产Http接口描述信息
- SpringBoot使用自定义注解实现权限拦截的示例
- 使用Spring自定义注解实现任务路由
- 使用Spring获得包含自定义注解的对象
- 使用Spring注解AOP(基于自定义注解和包下拦截方法)
- Spring自定义注解加载和使用
- 深入Spring:自定义注解加载和使用 144 作者 wcong 关注 2016.03.23 13:41* 字数 1573 阅读 7651评论 7喜欢 22 前言 在工作中经常使用Spring的相
- Spring中使用自定义的注解校验器的实现
- Spring Boot项目中自定义注解的使用