⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 contextloaderplugin.java

📁 一个关于Spring框架的示例应用程序,简单使用,可以参考.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	 */
	public void setContextConfigLocation(String contextConfigLocation) {
		this.contextConfigLocation = contextConfigLocation;
	}

	/**
	 * Return the explicit context config location, if any.
	 */
	public String getContextConfigLocation() {
		return contextConfigLocation;
	}


	/**
	 * Create the ActionServlet's WebApplicationContext.
	 */
	public final void init(ActionServlet actionServlet, ModuleConfig moduleConfig) throws ServletException {
		long startTime = System.currentTimeMillis();
		if (logger.isInfoEnabled()) {
			logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() +
					", module '" + moduleConfig.getPrefix() + "': initialization started");
		}

		this.actionServlet = actionServlet;
		this.moduleConfig = moduleConfig;
		try {
			this.webApplicationContext = initWebApplicationContext();
			onInit();
		}
		catch (RuntimeException ex) {
			logger.error("Context initialization failed", ex);
			throw ex;
		}

		if (logger.isInfoEnabled()) {
			long elapsedTime = System.currentTimeMillis() - startTime;
			logger.info("ContextLoaderPlugIn for Struts ActionServlet '" + actionServlet.getServletName() +
					"', module '" + moduleConfig.getPrefix() + "': initialization completed in " + elapsedTime + " ms");
		}
	}

	/**
	 * Return the Struts ActionServlet that this PlugIn is associated with.
	 */
	public final ActionServlet getActionServlet() {
		return actionServlet;
	}

	/**
	 * Return the name of the ActionServlet that this PlugIn is associated with.
	 */
	public final String getServletName() {
		return this.actionServlet.getServletName();
	}

	/**
	 * Return the ServletContext that this PlugIn is associated with.
	 */
	public final ServletContext getServletContext() {
		return this.actionServlet.getServletContext();
	}

	/**
	 * Return the Struts ModuleConfig that this PlugIn is associated with.
	 */
	public final ModuleConfig getModuleConfig() {
		return moduleConfig;
	}

	/**
	 * Return the prefix of the ModuleConfig that this PlugIn is associated with.
	 * @see org.apache.struts.config.ModuleConfig#getPrefix
	 */
	public final String getModulePrefix() {
		return this.moduleConfig.getPrefix();
	}

	/**
	 * Initialize and publish the WebApplicationContext for the ActionServlet.
	 * Delegates to createWebApplicationContext for actual creation.
	 * <p>Can be overridden in subclasses. Call <code>getActionServlet</code>
	 * and/or <code>getModuleConfig</code> to access the Struts configuration
	 * that this PlugIn is associated with.
	 * @throws org.springframework.beans.BeansException if the context couldn't be initialized
	 * @throws IllegalStateException if there is already a context for the Struts ActionServlet
	 * @see #createWebApplicationContext
	 * @see #getActionServlet
	 * @see #getServletName
	 * @see #getServletContext
	 * @see #getModuleConfig
	 * @see #getModulePrefix
	 */
	protected WebApplicationContext initWebApplicationContext() throws BeansException, IllegalStateException {
		getServletContext().log("Initializing WebApplicationContext for Struts ActionServlet '" +
				getServletName() + "', module '" + getModulePrefix() + "'");
		WebApplicationContext parent = WebApplicationContextUtils.getWebApplicationContext(getServletContext());

		WebApplicationContext wac = createWebApplicationContext(parent);
		if (logger.isInfoEnabled()) {
			logger.info("Using context class '" + wac.getClass().getName() + "' for servlet '" + getServletName() + "'");
		}

		// Publish the context as a servlet context attribute.
		String attrName = getServletContextAttributeName();
		getServletContext().setAttribute(attrName, wac);
		if (logger.isDebugEnabled()) {
			logger.debug("Published WebApplicationContext of Struts ActionServlet '" + getServletName() +
					"', module '" + getModulePrefix() + "' as ServletContext attribute with name [" + attrName + "]");
		}
		
		return wac;
	}

	/**
	 * Instantiate the WebApplicationContext for the ActionServlet, either a default
	 * XmlWebApplicationContext or a custom context class if set. This implementation
	 * expects custom contexts to implement ConfigurableWebApplicationContext.
	 * Can be overridden in subclasses.
	 * @throws org.springframework.beans.BeansException if the context couldn't be initialized
	 * @see #setContextClass
	 * @see org.springframework.web.context.support.XmlWebApplicationContext
	 */
	protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
			throws BeansException {

		if (logger.isDebugEnabled()) {
			logger.debug("ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() +
					"', module '" + getModulePrefix() + "' will try to create custom WebApplicationContext " +
					"context of class '" + getContextClass().getName() + "', using parent context [" + parent + "]");
		}
		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(getContextClass())) {
			throw new ApplicationContextException(
					"Fatal initialization error in ContextLoaderPlugIn for Struts ActionServlet '" + getServletName() +
					"', module '" + getModulePrefix() + "': custom WebApplicationContext class [" +
					getContextClass().getName() + "] is not of type ConfigurableWebApplicationContext");
		}

		ConfigurableWebApplicationContext wac =
				(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(getContextClass());
		wac.setParent(parent);
		wac.setServletContext(getServletContext());
		wac.setNamespace(getNamespace());
		if (getContextConfigLocation() != null) {
			wac.setConfigLocations(
			    StringUtils.tokenizeToStringArray(
							getContextConfigLocation(), ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
		}
		wac.addBeanFactoryPostProcessor(
				new BeanFactoryPostProcessor() {
					public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
						beanFactory.addBeanPostProcessor(new ActionServletAwareProcessor(getActionServlet()));
					}
				}
		);
		wac.refresh();
		return wac;
	}

	/**
	 * Return the ServletContext attribute name for this PlugIn's WebApplicationContext.
	 * Default implementation returns SERVLET_CONTEXT_PREFIX + module prefix.
	 * @see #SERVLET_CONTEXT_PREFIX
	 * @see #getModulePrefix
	 */
	public String getServletContextAttributeName() {
		return SERVLET_CONTEXT_PREFIX + getModulePrefix();
	}

	/**
	 * Return this PlugIn's WebApplicationContext.
	 */
	public final WebApplicationContext getWebApplicationContext() {
		return webApplicationContext;
	}

	/**
	 * Callback for custom initialization after the context has been set up.
	 * @throws ServletException if initialization failed
	 */
	protected void onInit() throws ServletException {
	}


	/**
	 * Close the WebApplicationContext of the ActionServlet.
	 * @see org.springframework.context.ConfigurableApplicationContext#close
	 */
	public void destroy() {
		getServletContext().log("Closing WebApplicationContext of Struts ActionServlet '" +
				getServletName() + "', module '" + getModulePrefix() + "'");
		if (getWebApplicationContext() instanceof ConfigurableApplicationContext) {
			((ConfigurableApplicationContext) getWebApplicationContext()).close();
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -