基于Spring进行注解开发
2018-01-03 13:52
459 查看
基于Spring进行注解开发
Spring的BeanPostProcessor接口
BeanPostProcessor的作用
Spring容器bean实例化、配置以及其他初始化方法前后,进行一些逻辑处理.就需要实现BeanPostProcessor接口public interface BeanPostProcessor { //实例化、依赖注入完毕,在调用显示的初始化之前完成一些定制的初始化任务 Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException; //实例化、依赖注入、初始化完毕时执行 Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException; }
声明SpringAnnotationScanner实现BeanPostProcessor接口
public class AutomaticSpringAnnotationsScanner implements BeanPostProcessor { private static final Logger log = LoggerFactory.getLogger(AutomaticSpringAnnotationsScanner.class); //注解的集合 private final List<Class<? extends Annotation>> annotations = Arrays.asList(Config.class,Custom.class); private Class originalBeanClass; public AutomaticSpringAnnotationsScanner() { } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (this.originalBeanClass != null) { //得到被注解的对象,保存。执行一些操作 log.info("{} bean listeners added", beanName); this.originalBeanClass = null; } return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { final AtomicBoolean add = new AtomicBoolean(); ReflectionUtils.doWithMethods(bean.getClass(), new ReflectionUtils.MethodCallb 4000 ack() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { add.set(true); } }, new ReflectionUtils.MethodFilter() { public boolean matches(Method method) { Iterator i$ = AutomaticSpringAnnotationsScanner.this.annotations.iterator(); Class annotationClass; do { if (!i$.hasNext()) { return false; } annotationClass = (Class)i$.next(); } while(!method.isAnnotationPresent(annotationClass)); return true; } }); if (add.get()) { this.originalBeanClass = bean.getClass(); } return bean; } }
自定义的一些注解
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Config { //自定义的注解,作用在方法上 String value() default "" ; }
@Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Custom {//自定义的注解,作用在方法上 String value() default ""; }
使用
//Springboot的注入方式:在启动的Application类中添加 @Bean public AutomaticSpringAnnotationsScanner automaticSpringAnnotationsScanner (){ return new SpringAnnotationScanner(); } //springMvc的注入方式 @Component //声明式注解 public class AutomaticSpringAnnotationsScanner implements BeanPostProcessor{ } //* 通过配置,或者注解都可以,方式很多
相关文章推荐
- SpringMVC经典系列-02基于Spring2.5注解的方式进行项目开发---【LinusZhu】
- SpringMVC经典系列-04基于Spring3.1注解的方式进行项目开发---【LinusZhu】
- springboot 整合mybatis 基于注解开发 传入对象
- spring-redis缓存方案学习二:基于spring的@cache注解开发
- 重温Spring之旅6——基于XML配置方式进行AOP开发
- 基于Spring的可扩展Schema进行开发自定义配置标签支持
- SpringBoot 使用@Aspect进行日志管理(基于反射代理模式+注解Log)
- 【web开发】基于注解spring的多方法请求的一种controller实现
- Spring使用AspectJ进行AOP的开发:注解方式
- 基于AspectJ的注解方式进行AOP开发
- (转)Spring使用AspectJ进行AOP的开发:注解方式
- hessian基于spring的自定义注解配置拓展(高效率开发hessian)
- 基于AspectJ的注解方式进行AOP开发
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
- Spring 基于注解方式进行事务管理
- 基于Spring+Hibernate+Eclipse进行敏捷Java开发
- [置顶] 基于注解的Spring整合SpringMVC开发流程?
- Spring 基于注解的开发
- spring-redis缓存方案学习三:基于aop的自定义注解开发
- 基于Spring注解进行事务管理