Java Code Examples for org.springframework.beans.factory.config.ConfigurableListableBeanFactory#registerResolvableDependency()

The following examples show how to use org.springframework.beans.factory.config.ConfigurableListableBeanFactory#registerResolvableDependency() . 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: WebApplicationContextUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public static void registerFacesDependencies(ConfigurableListableBeanFactory beanFactory) {
	beanFactory.registerResolvableDependency(FacesContext.class, new ObjectFactory<FacesContext>() {
		@Override
		public FacesContext getObject() {
			return FacesContext.getCurrentInstance();
		}
		@Override
		public String toString() {
			return "Current JSF FacesContext";
		}
	});
	beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
		@Override
		public ExternalContext getObject() {
			return FacesContext.getCurrentInstance().getExternalContext();
		}
		@Override
		public String toString() {
			return "Current JSF ExternalContext";
		}
	});
}
 
Example 2
Source File: WebApplicationContextUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebApplicationContext.
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
		@Nullable ServletContext sc) {

	beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
	beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
	if (sc != null) {
		ServletContextScope appScope = new ServletContextScope(sc);
		beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// Register as ServletContext attribute, for ContextCleanupListener to detect it.
		sc.setAttribute(ServletContextScope.class.getName(), appScope);
	}

	beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
	beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
	beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
	beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
	if (jsfPresent) {
		FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
	}
}
 
Example 3
Source File: WebApplicationContextUtils.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public static void registerFacesDependencies(ConfigurableListableBeanFactory beanFactory) {
	beanFactory.registerResolvableDependency(FacesContext.class, new ObjectFactory<FacesContext>() {
		@Override
		public FacesContext getObject() {
			return FacesContext.getCurrentInstance();
		}
		@Override
		public String toString() {
			return "Current JSF FacesContext";
		}
	});
	beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
		@Override
		public ExternalContext getObject() {
			return FacesContext.getCurrentInstance().getExternalContext();
		}
		@Override
		public String toString() {
			return "Current JSF ExternalContext";
		}
	});
}
 
Example 4
Source File: WebApplicationContextUtils.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebApplicationContext.
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc) {
	beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
	beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false));
	beanFactory.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION, new SessionScope(true));
	if (sc != null) {
		ServletContextScope appScope = new ServletContextScope(sc);
		beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// Register as ServletContext attribute, for ContextCleanupListener to detect it.
		sc.setAttribute(ServletContextScope.class.getName(), appScope);
	}

	beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
	beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
	beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
	beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
	if (jsfPresent) {
		FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
	}
}
 
Example 5
Source File: WebApplicationContextUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebApplicationContext.
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory,
		@Nullable ServletContext sc) {

	beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
	beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope());
	if (sc != null) {
		ServletContextScope appScope = new ServletContextScope(sc);
		beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// Register as ServletContext attribute, for ContextCleanupListener to detect it.
		sc.setAttribute(ServletContextScope.class.getName(), appScope);
	}

	beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
	beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
	beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
	beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
	if (jsfPresent) {
		FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
	}
}
 
Example 6
Source File: WebApplicationContextUtils.java    From java-technology-stack with MIT License 6 votes vote down vote up
public static void registerFacesDependencies(ConfigurableListableBeanFactory beanFactory) {
	beanFactory.registerResolvableDependency(FacesContext.class, new ObjectFactory<FacesContext>() {
		@Override
		public FacesContext getObject() {
			return FacesContext.getCurrentInstance();
		}
		@Override
		public String toString() {
			return "Current JSF FacesContext";
		}
	});
	beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
		@Override
		public ExternalContext getObject() {
			return FacesContext.getCurrentInstance().getExternalContext();
		}
		@Override
		public String toString() {
			return "Current JSF ExternalContext";
		}
	});
}
 
Example 7
Source File: ProcessScope.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

		beanFactory.registerScope(ProcessScope.PROCESS_SCOPE_NAME, this);

		Assert.isInstanceOf(BeanDefinitionRegistry.class, beanFactory, "BeanFactory was not a BeanDefinitionRegistry, so ProcessScope cannot be used.");

		BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

		for (String beanName : beanFactory.getBeanDefinitionNames()) {
			BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
			// Replace this or any of its inner beans with scoped proxy if it has this scope
			boolean scoped = PROCESS_SCOPE_NAME.equals(definition.getScope());
			Scopifier scopifier = new Scopifier(registry, PROCESS_SCOPE_NAME, proxyTargetClass, scoped);
			scopifier.visitBeanDefinition(definition);
			if (scoped) {
				Scopifier.createScopedProxy(beanName, definition, registry, proxyTargetClass);
			}
		}

		beanFactory.registerSingleton(ProcessScope.PROCESS_SCOPE_PROCESS_VARIABLES_SINGLETON, this.processVariablesMap);
		beanFactory.registerResolvableDependency(ProcessInstance.class,  createSharedProcessInstance());
	}
 
Example 8
Source File: WebApplicationContextUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Register web-specific scopes ("request", "session", "globalSession", "application")
 * with the given BeanFactory, as used by the WebApplicationContext.
 * @param beanFactory the BeanFactory to configure
 * @param sc the ServletContext that we're running within
 */
public static void registerWebApplicationScopes(ConfigurableListableBeanFactory beanFactory, ServletContext sc) {
	beanFactory.registerScope(WebApplicationContext.SCOPE_REQUEST, new RequestScope());
	beanFactory.registerScope(WebApplicationContext.SCOPE_SESSION, new SessionScope(false));
	beanFactory.registerScope(WebApplicationContext.SCOPE_GLOBAL_SESSION, new SessionScope(true));
	if (sc != null) {
		ServletContextScope appScope = new ServletContextScope(sc);
		beanFactory.registerScope(WebApplicationContext.SCOPE_APPLICATION, appScope);
		// Register as ServletContext attribute, for ContextCleanupListener to detect it.
		sc.setAttribute(ServletContextScope.class.getName(), appScope);
	}

	beanFactory.registerResolvableDependency(ServletRequest.class, new RequestObjectFactory());
	beanFactory.registerResolvableDependency(ServletResponse.class, new ResponseObjectFactory());
	beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());
	beanFactory.registerResolvableDependency(WebRequest.class, new WebRequestObjectFactory());
	if (jsfPresent) {
		FacesDependencyRegistrar.registerFacesDependencies(beanFactory);
	}
}
 
Example 9
Source File: WebApplicationContextUtils.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static void registerFacesDependencies(ConfigurableListableBeanFactory beanFactory) {
	beanFactory.registerResolvableDependency(FacesContext.class, new ObjectFactory<FacesContext>() {
		@Override
		public FacesContext getObject() {
			return FacesContext.getCurrentInstance();
		}
		@Override
		public String toString() {
			return "Current JSF FacesContext";
		}
	});
	beanFactory.registerResolvableDependency(ExternalContext.class, new ObjectFactory<ExternalContext>() {
		@Override
		public ExternalContext getObject() {
			return FacesContext.getCurrentInstance().getExternalContext();
		}
		@Override
		public String toString() {
			return "Current JSF ExternalContext";
		}
	});
}
 
Example 10
Source File: ResourceAdapterApplicationContext.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	beanFactory.addBeanPostProcessor(new BootstrapContextAwareProcessor(this.bootstrapContext));
	beanFactory.ignoreDependencyInterface(BootstrapContextAware.class);
	beanFactory.registerResolvableDependency(BootstrapContext.class, this.bootstrapContext);

	// JCA WorkManager resolved lazily - may not be available.
	beanFactory.registerResolvableDependency(WorkManager.class,
			(ObjectFactory<WorkManager>) this.bootstrapContext::getWorkManager);
}
 
Example 11
Source File: ResourceAdapterApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	beanFactory.addBeanPostProcessor(new BootstrapContextAwareProcessor(this.bootstrapContext));
	beanFactory.ignoreDependencyInterface(BootstrapContextAware.class);
	beanFactory.registerResolvableDependency(BootstrapContext.class, this.bootstrapContext);

	// JCA WorkManager resolved lazily - may not be available.
	beanFactory.registerResolvableDependency(WorkManager.class, new ObjectFactory<WorkManager>() {
		@Override
		public WorkManager getObject() {
			return bootstrapContext.getWorkManager();
		}
	});
}
 
Example 12
Source File: AbstractApplicationContext.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
	}
}
 
Example 13
Source File: ServletTestExecutionListener.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (notAnnotatedWithWebAppConfiguration(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
		return;
	}

	ApplicationContext context = testContext.getApplicationContext();

	if (context instanceof WebApplicationContext) {
		WebApplicationContext wac = (WebApplicationContext) context;
		ServletContext servletContext = wac.getServletContext();
		Assert.state(servletContext instanceof MockServletContext, String.format(
			"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
			testContext));

		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
				"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
				testContext));
		}

		MockServletContext mockServletContext = (MockServletContext) servletContext;
		MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
		request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
		MockHttpServletResponse response = new MockHttpServletResponse();
		ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

		RequestContextHolder.setRequestAttributes(servletWebRequest);
		testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
		testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

		if (wac instanceof ConfigurableApplicationContext) {
			@SuppressWarnings("resource")
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
			ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
			bf.registerResolvableDependency(MockHttpServletResponse.class, response);
			bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
		}
	}
}
 
Example 14
Source File: ServletTestExecutionListener.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (!isActivated(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
		return;
	}

	ApplicationContext context = testContext.getApplicationContext();

	if (context instanceof WebApplicationContext) {
		WebApplicationContext wac = (WebApplicationContext) context;
		ServletContext servletContext = wac.getServletContext();
		Assert.state(servletContext instanceof MockServletContext, () -> String.format(
					"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
					testContext));

		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
					testContext));
		}

		MockServletContext mockServletContext = (MockServletContext) servletContext;
		MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
		request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
		MockHttpServletResponse response = new MockHttpServletResponse();
		ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

		RequestContextHolder.setRequestAttributes(servletWebRequest);
		testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
		testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

		if (wac instanceof ConfigurableApplicationContext) {
			@SuppressWarnings("resource")
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
			ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
			bf.registerResolvableDependency(MockHttpServletResponse.class, response);
			bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
		}
	}
}
 
Example 15
Source File: ResourceAdapterApplicationContext.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
	beanFactory.addBeanPostProcessor(new BootstrapContextAwareProcessor(this.bootstrapContext));
	beanFactory.ignoreDependencyInterface(BootstrapContextAware.class);
	beanFactory.registerResolvableDependency(BootstrapContext.class, this.bootstrapContext);

	// JCA WorkManager resolved lazily - may not be available.
	beanFactory.registerResolvableDependency(WorkManager.class,
			(ObjectFactory<WorkManager>) this.bootstrapContext::getWorkManager);
}
 
Example 16
Source File: ServletTestExecutionListener.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void setUpRequestContextIfNecessary(TestContext testContext) {
	if (!isActivated(testContext) || alreadyPopulatedRequestContextHolder(testContext)) {
		return;
	}

	ApplicationContext context = testContext.getApplicationContext();

	if (context instanceof WebApplicationContext) {
		WebApplicationContext wac = (WebApplicationContext) context;
		ServletContext servletContext = wac.getServletContext();
		Assert.state(servletContext instanceof MockServletContext, () -> String.format(
					"The WebApplicationContext for test context %s must be configured with a MockServletContext.",
					testContext));

		if (logger.isDebugEnabled()) {
			logger.debug(String.format(
					"Setting up MockHttpServletRequest, MockHttpServletResponse, ServletWebRequest, and RequestContextHolder for test context %s.",
					testContext));
		}

		MockServletContext mockServletContext = (MockServletContext) servletContext;
		MockHttpServletRequest request = new MockHttpServletRequest(mockServletContext);
		request.setAttribute(CREATED_BY_THE_TESTCONTEXT_FRAMEWORK, Boolean.TRUE);
		MockHttpServletResponse response = new MockHttpServletResponse();
		ServletWebRequest servletWebRequest = new ServletWebRequest(request, response);

		RequestContextHolder.setRequestAttributes(servletWebRequest);
		testContext.setAttribute(POPULATED_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);
		testContext.setAttribute(RESET_REQUEST_CONTEXT_HOLDER_ATTRIBUTE, Boolean.TRUE);

		if (wac instanceof ConfigurableApplicationContext) {
			@SuppressWarnings("resource")
			ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) wac;
			ConfigurableListableBeanFactory bf = configurableApplicationContext.getBeanFactory();
			bf.registerResolvableDependency(MockHttpServletResponse.class, response);
			bf.registerResolvableDependency(ServletWebRequest.class, servletWebRequest);
		}
	}
}
 
Example 17
Source File: AbstractApplicationContext.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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(EnvironmentAware.class);
	beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
	beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
	beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationContextAware.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);

	// Register early post-processor for detecting inner beans as ApplicationListeners.
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(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());
	}
}
 
Example 18
Source File: RequestContextBeanFactoryPostProcessor.java    From faster-framework-project with Apache License 2.0 4 votes vote down vote up
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerResolvableDependency(RequestContext.class, new RequestContextObjectFactory());
}
 
Example 19
Source File: AbstractApplicationContext.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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(EnvironmentAware.class);
	beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
	beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
	beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationContextAware.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);

	// Register early post-processor for detecting inner beans as ApplicationListeners.
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(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());
	}
}
 
Example 20
Source File: AbstractApplicationContext.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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 注册了个 beanPostProcessor,后处理器的类型是 ApplicationContextAwareProcessor
	beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
	beanFactory.ignoreDependencyInterface(EnvironmentAware.class);
	beanFactory.ignoreDependencyInterface(EmbeddedValueResolverAware.class);
	beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
	beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
	beanFactory.ignoreDependencyInterface(ApplicationContextAware.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);

	// Register early post-processor for detecting inner beans as ApplicationListeners.
	beanFactory.addBeanPostProcessor(new ApplicationListenerDetector(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());
	}
}