org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter Java Examples

The following examples show how to use org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DruidAndMybatisApplicationContextInitializer.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    try {
        ClassUtils.forName("com.alibaba.druid.pool.DruidDataSource",this.getClass().getClassLoader());
    } catch (ClassNotFoundException e) {
        return;
    }
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof DataSourceProperties) {
                DataSourceProperties dataSourceProperties = (DataSourceProperties)bean;
                DruidAndMybatisApplicationContextInitializer.this.rewirteDataSourceProperties(dataSourceProperties);
            } else if (bean instanceof MybatisProperties) {
                MybatisProperties mybatisProperties = (MybatisProperties)bean;
                DruidAndMybatisApplicationContextInitializer.this.rewriteMybatisProperties(mybatisProperties);
            }
            return bean;
        }
    });

}
 
Example #2
Source File: WebApiApplictionInitalizer.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof HandlerMethodResolver) {
                HandlerMethodResolver handlerMethodResolver = (HandlerMethodResolver)bean;
                Field typeResolverField =
                    ReflectionUtils.findField(HandlerMethodResolver.class, "typeResolver", TypeResolver.class);
                ReflectionUtils.makeAccessible(typeResolverField);
                TypeResolver typeResolver =
                    (TypeResolver)ReflectionUtils.getField(typeResolverField, handlerMethodResolver);
                return new HandlerMethodResolverWrapper(typeResolver);
            } else {
                return bean;
            }

        }
    });

}
 
Example #3
Source File: DataRedisContextInitializer.java    From summerframework with Apache License 2.0 6 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    Environment env = applicationContext.getEnvironment();
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof RedisTemplate) {
                RedisTemplate redisTemplate = (RedisTemplate)bean;
                // do cache
                redisTemplate.opsForValue();
                redisTemplate.opsForList();
                redisTemplate.opsForSet();
                redisTemplate.opsForZSet();
                redisTemplate.opsForGeo();
                redisTemplate.opsForHash();
                redisTemplate.opsForHyperLogLog();
                createProxyHandlers(redisTemplate);
            }
            return bean;
        }
    });
}
 
Example #4
Source File: SpringContextHolder.java    From dts with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            Environment enviroment = applicationContext.getEnvironment();
            String appName = enviroment.getProperty("spring.application.name");
            if (bean instanceof DataSource) {
                DataSource datasource = (DataSource)bean;
                try {
                    DataSourceAdapter dataSourceAdapter = new DataSourceAdapter(appName, datasource);
                    return dataSourceAdapter;
                } catch (SQLException e) {
                    throw new BeanInitializationException("cant not adapter datasource", e);
                }
            }
            if (bean instanceof RestTemplate) {
                RestTemplate restTemplate = (RestTemplate)bean;
                ArrayList<ClientHttpRequestInterceptor> interceptors = new ArrayList<>();
                interceptors.add(new DtsRemoteInterceptor());
                interceptors.addAll(restTemplate.getInterceptors());
                restTemplate.setInterceptors(interceptors);
                return restTemplate;
            }
            return bean;
        }
    });
    applicationContext.addApplicationListener(new ApplicationListener<ApplicationEvent>() {

        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            if (event instanceof EurekaClientLocalCacheRefreshedEvent) {
                eurekaLocalCacheRefreshed = true;
            }
        }

    });
    SpringContextHolder.applicationContext = applicationContext;
}
 
Example #5
Source File: OpenFeignApplictionInitalizer.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    applicationContext.getBeanFactory().registerSingleton("summerErrorAdapterApiResultHandler",
        new SummerErrorAdapterApiResultHandler());
    applicationContext.getBeanFactory().registerSingleton("summer2ErrorApiResultHandler",
        new Summer2ErrorApiResultHandler());
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof Decoder && !(bean instanceof ApiResultDecoder)) {
                Decoder decode = (Decoder)bean;
                return new ApiResultDecoder(decode, applicationContext.getBeansOfType(ApiResultHandler.class)
                    .values().stream().collect(Collectors.toList()));
            } else if (bean instanceof RequestParamMethodArgumentResolver) {
                RequestParamMethodArgumentResolver requestParamMethodArgumentResolver =
                    (RequestParamMethodArgumentResolver)bean;
                Field useDefaultResolution = ReflectionUtils.findField(RequestParamMethodArgumentResolver.class,
                    "useDefaultResolution", RequestParamMethodArgumentResolver.class);
                ReflectionUtils.makeAccessible(useDefaultResolution);
                Boolean useDefaultResolutionValue =
                    (Boolean)ReflectionUtils.getField(useDefaultResolution, requestParamMethodArgumentResolver);
                return new RequestParamMethodArgumentResolverExt(useDefaultResolutionValue);
            } else {
                return bean;
            }

        }
    });

}
 
Example #6
Source File: EurekaApplicationContextInitializer.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
    ConfigurableEnvironment environment = applicationContext.getEnvironment();
    applicationContext.getBeanFactory().addBeanPostProcessor(new InstantiationAwareBeanPostProcessorAdapter() {

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof CloudEurekaClient) {
                CloudEurekaClient eurekaClient = (CloudEurekaClient)bean;
                try {
                    Field filedPublisher = ReflectionUtils.findField(CloudEurekaClient.class, "publisher",
                        ApplicationEventPublisher.class);
                    ReflectionUtils.makeAccessible(filedPublisher);
                    ApplicationEventPublisher publisher =
                        (ApplicationEventPublisher)ReflectionUtils.getField(filedPublisher, eurekaClient);
                    return new EurekaClientWrapper(eurekaClient, environment, publisher);
                } finally {
                    Method method = ReflectionUtils.findMethod(CloudEurekaClient.class, "cancelScheduledTasks");
                    ReflectionUtils.makeAccessible(method);
                    ReflectionUtils.invokeMethod(method, eurekaClient);
                    eurekaClient = null;
                }
            } else if (bean instanceof EurekaServiceRegistry) {
                EurekaServiceRegistry eurekaServiceRegistry = (EurekaServiceRegistry)bean;
                return new EurekaServiceRegistryWrapper(eurekaServiceRegistry, environment);
            } else if (bean instanceof EurekaInstanceConfigBean) {
                EurekaInstanceConfigBean instanceConfig = (EurekaInstanceConfigBean)bean;
                instanceConfig.setPreferIpAddress(true);
                return bean;
            } else {
                return bean;
            }
        }
    });
}