@Override public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null; if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { invokeAwareInterfaces(bean); return null; } }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver( new EmbeddedValueResolver(this.applicationContext.getBeanFactory())); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
/**��beanʵ�ֵ�aware�������лص� * @param bean */ private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver( new EmbeddedValueResolver(this.applicationContext.getBeanFactory())); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
@Override protected void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) { if (handlerExceptionResolvers == null) { return; } for (HandlerExceptionResolver resolver : handlerExceptionResolvers) { if (resolver instanceof ApplicationContextAware) { ((ApplicationContextAware) resolver).setApplicationContext(getApplicationContext()); } if (resolver instanceof InitializingBean) { try { ((InitializingBean) resolver).afterPropertiesSet(); } catch (Exception ex) { throw new IllegalStateException("Failure from afterPropertiesSet", ex); } } exceptionResolvers.add(resolver); } }
public static <T> T createBean(Class<T> clazz, Settings settings, ApplicationContext applicationContext) throws Exception { if (clazz == null || clazz.isInterface() || Modifier.isAbstract(clazz.getModifiers())) { return null; } T bean = clazz.newInstance(); if (bean instanceof SettingsAware) { ((SettingsAware) bean).setSettings(settings); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(applicationContext); } if (bean instanceof InitializingBean) { ((InitializingBean) bean).afterPropertiesSet(); } return bean; }
private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware) bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(this.applicationContext); } } }
protected Object createInstance() throws Exception { if(guzzContext == null){ guzzContext = (GuzzContext) this.getBeanFactory().getBean("guzzContext") ; } if(guzzContext == null){ throw new GuzzException("guzzContext not found. put guzzContext bean in front of this bean.") ; } service = guzzContext.getService(serviceName) ; if(service == null){ throw new GuzzException("service not found. service name is:" + this.serviceName) ; } if(service instanceof ApplicationContextAware){ ((ApplicationContextAware) service).setApplicationContext(applicationContext) ; } return service; }
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException { AccessControlContext acc = null; if (System.getSecurityManager() != null && (bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware || bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware || bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) { acc = this.applicationContext.getBeanFactory().getAccessControlContext(); } if (acc != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { public Object run() { invokeAwareInterfaces(bean); return null; } }, acc); } else { invokeAwareInterfaces(bean); } return bean; }
public static void inject(ApplicationContext appCtx) { // Iterate through all registered tasks for (DiagnosticTask each : SelfDiagnose.getTasks()) { // First, custom tasks are a special case: we extract its task (this may be null), and then inject the Spring bean from the reference (if available:). // As for a custom task either its task or its reference is set (never both), 'each' will be null if we injected a Spring bean (for which the next step is not needed). if (each instanceof CustomDiagnosticTask) { CustomDiagnosticTask custom = (CustomDiagnosticTask) each; each = custom.getTask(); injectTask(custom, appCtx); } // Then, if the task is (not null and) ApplicationContextAware, we inject the Spring context. if (each instanceof ApplicationContextAware) { ApplicationContextAware eachAware = (ApplicationContextAware) each; eachAware.setApplicationContext(appCtx); } } }
/** * 从spring容器获取执行类型bean或者获取默认实现 * * @param clz bean class 类型 * @param <T> * @return bean */ private <T> T getBean(Class<T> clz) { T t = null; try { if (this.applicationContext != null) { t = this.applicationContext.getBean(clz); } if (t != null) { log.info("{}使用Spring注入:{}", clz.getSimpleName(), t.getClass()); } else { t = DefaultRestyPassFactory.getDefaultBean(clz); log.info("{}使用默认配置:{}", clz.getSimpleName(), t.getClass()); if (t == null) { throw new IllegalArgumentException("无法获取Bean:" + clz); } if (t instanceof ApplicationContextAware) { ApplicationContextAware contextAware = (ApplicationContextAware) serverContext; contextAware.setApplicationContext(this.applicationContext); } } } catch (BeansException ex) { t = DefaultRestyPassFactory.getDefaultBean(clz); log.info("{}使用默认配置:{}", clz.getSimpleName(), t.getClass()); } return t; }
@Override protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception { Object job = super.createJobInstance(bundle); if(job instanceof ApplicationContextAware) { ((ApplicationContextAware)job).setApplicationContext(context); } return job; }
/** * Configure the factory's standard context characteristics, * such as the context's ClassLoader and post-processors. * @param beanFactory the BeanFactory to configure */ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // Tell the internal bean factory to use the context's class loader etc. beanFactory.setBeanClassLoader(getClassLoader()); beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver()); beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); beanFactory.ignoreDependencyInterface(EnvironmentAware.class); // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); // Detect a LoadTimeWeaver and prepare for weaving, if found. if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); // Set a temporary ClassLoader for type matching. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } // Register default environment beans. if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); } }
/** * Configure the factory's standard context characteristics, * such as the context's ClassLoader and post-processors. * @param beanFactory the BeanFactory to configure */ protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // Tell the internal bean factory to use the context's class loader etc. beanFactory.setBeanClassLoader(getClassLoader()); beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); beanFactory.ignoreDependencyInterface(EnvironmentAware.class); // BeanFactory interface not registered as resolvable type in a plain factory. // MessageSource registered (and found for autowiring) as a bean. beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory); beanFactory.registerResolvableDependency(ResourceLoader.class, this); beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this); beanFactory.registerResolvableDependency(ApplicationContext.class, this); // Detect a LoadTimeWeaver and prepare for weaving, if found. if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); // Set a temporary ClassLoader for type matching. beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } // Register default environment beans. if (!beanFactory.containsLocalBean(ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(ENVIRONMENT_BEAN_NAME, getEnvironment()); } if (!beanFactory.containsLocalBean(SYSTEM_PROPERTIES_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, getEnvironment().getSystemProperties()); } if (!beanFactory.containsLocalBean(SYSTEM_ENVIRONMENT_BEAN_NAME)) { beanFactory.registerSingleton(SYSTEM_ENVIRONMENT_BEAN_NAME, getEnvironment().getSystemEnvironment()); } }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { for (ViewResolver viewResolver : this.viewResolvers) { if (viewResolver instanceof ApplicationContextAware) { ((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext); } } }
@Override public Object initializeBean(Object existingBean, String beanName) throws BeansException { if (existingBean instanceof ApplicationContextAware) { ((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this); } return existingBean; }
private void injectComponentAware(Object instance){ if(instance instanceof Aware){ if(instance instanceof PluginContextAware){ ((PluginContextAware)instance).setPluginContext(pluginContext); } if(instance instanceof ApplicationContextAware){ ((ApplicationContextAware)instance).setApplicationContext(pluginContext.getApplicationContext()); } } }
public static void inject(ApplicationContext applicationContext, Object target) { if (applicationContext != null && target != null) { applicationContext.getAutowireCapableBeanFactory().autowireBeanProperties(target, 0, false); if (target instanceof ApplicationContextAware) { ((ApplicationContextAware) target).setApplicationContext(applicationContext); } } else { throw new IllegalArgumentException( "Got invalid arguments applicationContext:" + applicationContext + " target:" + target); } }
public Parser getParser(String contentType) { Class<? extends Parser> clazz = parsers.get(contentType); if (clazz == null) { return dummyParser; } Parser parser; try { parser = clazz.newInstance(); } catch (Exception e) { return dummyParser; } if (parser instanceof ApplicationContextAware) { ((ApplicationContextAware) parser).setApplicationContext(applicationContext); } if (parser instanceof InitializingBean) { try { ((InitializingBean) parser).afterPropertiesSet(); } catch (Exception ignored) { } } return parser; }
@Override public void contextLoaded(ConfigurableApplicationContext context) { for (ApplicationListener<?> listener : this.application.getListeners()) { if (listener instanceof ApplicationContextAware) { ((ApplicationContextAware) listener).setApplicationContext(context); } context.addApplicationListener(listener); } this.initialMulticaster.multicastEvent(new ApplicationPreparedEvent( this.application, this.args, context)); }
/** * Invoke callbacks on the object, as though it were configured in the factory. If appropriate, * the object may be wrapped before being returned. For this reason, it is recommended to always * respect the return value when using this method. * * @param object object to configure * * @return either the original object or a wrapped one after callbacks called on it. */ protected <T> T getConfigured(T object) { if (this.autowireCapableBeanFactory == null) { throw new UnsupportedOperationException( "Cannot configure object - not running in an AutowireCapableBeanFactory"); } @SuppressWarnings("unchecked") // See SPR-4955 T configuredObject = (T) autowireCapableBeanFactory.initializeBean(object, null); // this block copied from ApplicationContextAwareProcessor. See SJC-149. if (this.applicationContext != null) { if (configuredObject instanceof ResourceLoaderAware) { ((ResourceLoaderAware) configuredObject).setResourceLoader(this.applicationContext); } if (configuredObject instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware) configuredObject).setApplicationEventPublisher(this.applicationContext); } if (configuredObject instanceof MessageSourceAware) { ((MessageSourceAware) configuredObject).setMessageSource(this.applicationContext); } if (configuredObject instanceof ApplicationContextAware) { ((ApplicationContextAware) configuredObject).setApplicationContext(this.applicationContext); } } return configuredObject; }
protected void initBeanPostProcessor(SpringCamelContext context) { if (beanPostProcessor != null) { if (beanPostProcessor instanceof ApplicationContextAware) { ((ApplicationContextAware) beanPostProcessor).setApplicationContext(applicationContext); } if (beanPostProcessor instanceof CamelBeanPostProcessor) { ((CamelBeanPostProcessor) beanPostProcessor).setCamelContext(getContext()); } } }
@Override public void setApplicationContext( final ApplicationContext applicationContext ) throws BeansException { if ( this.applicationBootstrap instanceof ApplicationContextAware ) { // propagate application context to the application bootstrap ApplicationContextAware aware = (ApplicationContextAware) this.applicationBootstrap; aware.setApplicationContext( applicationContext ); } }
@Override public void contextLoaded(ConfigurableApplicationContext context) { for (ApplicationListener<?> listener : this.application.getListeners()) { if (listener instanceof ApplicationContextAware) { ((ApplicationContextAware) listener).setApplicationContext(context); } context.addApplicationListener(listener); } publishEvent(new ApplicationPreparedEvent(this.application, this.args, context)); }
private Object generateCustomImplementation() { EntityManagerFactory emf = context.getBean(entityManagerFactoryRef, EntityManagerFactory.class); for (Class<?> clz : repositoryInterface.getInterfaces()) { if (Repository.class.isAssignableFrom(clz)) { continue; } else if (clz.getAnnotation(RepositoryDefinition.class) != null) { continue; } String implementation=clz.getName() + StringUtils.trimToEmpty(repositoryImplementationPostfix); ClassEx implClz = ClassEx.forName(implementation); if (implClz == null) { log.error("Lack of implementation class: " + clz.getName()); throw new IllegalArgumentException("Lack of implementation class: " + implementation); } try { Object obj = implClz.newInstance(); for (FieldEx field : implClz.getDeclaredFields()) { if (field.getAnnotation(PersistenceContext.class) != null) { field.set(obj, QueryUtils.getEntityManager((JefEntityManagerFactory) emf)); } } if (obj instanceof ApplicationContextAware) { ((ApplicationContextAware) obj).setApplicationContext(context); } if (obj instanceof InitializingBean) { ((InitializingBean) obj).afterPropertiesSet(); } return obj; } catch (Exception ex) { log.error("", ex); return null; } } return null; }
public Object autoWireBean(Object bean, AutowireCapableBeanFactory autoWiringFactory) { autoWiringFactory = findAutoWiringBeanFactory(Tavern.getCurrentApplication(bean.getClass()) .getApplicationContext()); if (autoWiringFactory != null) { autoWiringFactory.autowireBeanProperties(bean, autowireStrategy, false); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(appContext); } injectInternalBeans(bean); return bean; }
@Override public void setApplicationContext(ApplicationContext context) throws BeansException { if (elasticsearchConverter instanceof ApplicationContextAware) { ((ApplicationContextAware) elasticsearchConverter).setApplicationContext(context); } }
private void injectApplicationContext(Object bean) { if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(appContext); } }
private void initialize() { ResourcePluginManager rpm = applicationContext.getBean(ResourcePluginManager.class); ResourceClassLoaderFactory rclf = applicationContext.getBean(ResourceClassLoaderFactory.class); ResourceConfigurationFactory rcf = applicationContext.getBean(ResourceConfigurationFactory.class); ReactorHandler rh = applicationContext.getBean(ReactorHandler.class); Reactable reactable = rh.reactable(); Set<Resource> resourceDeps = reactable.dependencies(Resource.class); resourceDeps.forEach(resource -> { final ResourcePlugin resourcePlugin = rpm.get(resource.getType()); if (resourcePlugin == null) { logger.error("Resource [{}] can not be found in plugin registry", resource.getType()); throw new IllegalStateException("Resource ["+resource.getType()+"] can not be found in plugin registry"); } PluginClassLoader resourceClassLoader = rclf.getOrCreateClassLoader(resourcePlugin, rh.classloader()); logger.debug("Loading resource {} for {}", resource.getName(), rh); try { Class<? extends io.gravitee.resource.api.Resource> resourceClass = (Class<? extends io.gravitee.resource.api.Resource>) ClassUtils.forName(resourcePlugin.resource().getName(), resourceClassLoader); Map<Class<?>, Object> injectables = new HashMap<>(); if (resourcePlugin.configuration() != null) { Class<? extends ResourceConfiguration> resourceConfigurationClass = (Class<? extends ResourceConfiguration>) ClassUtils.forName(resourcePlugin.configuration().getName(), resourceClassLoader); injectables.put(resourceConfigurationClass, rcf.create(resourceConfigurationClass, resource.getConfiguration())); } io.gravitee.resource.api.Resource resourceInstance = new ResourceFactory().create(resourceClass, injectables); if (resourceInstance instanceof ApplicationContextAware) { ((ApplicationContextAware) resourceInstance).setApplicationContext(applicationContext); } resources.put(resource.getName(), resourceInstance); } catch (Exception ex) { logger.error("Unable to create resource", ex); if (resourceClassLoader != null) { try { resourceClassLoader.close(); } catch (IOException ioe) { logger.error("Unable to close classloader for resource", ioe); } } } }); }
public Object initializeBean(Object existingBean, String beanName) throws BeansException { if (existingBean instanceof ApplicationContextAware) { ((ApplicationContextAware) existingBean).setApplicationContext(StubWebApplicationContext.this); } return existingBean; }