org.springframework.context.ApplicationContextException类的使用及代码示例

x33g5p2x  于11个月前 转载在 其他  
字(15.4k)|赞(0)|评价(0)|浏览(45)

本文整理了Java中org.springframework.context.ApplicationContextException类的一些代码示例,展示了ApplicationContextException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ApplicationContextException类的具体详情如下:
包路径:org.springframework.context.ApplicationContextException
类名称:ApplicationContextException

ApplicationContextException介绍

[英]Exception thrown during application context initialization.
[中]应用程序上下文初始化期间引发异常。

代码示例

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private Class<ApplicationContextInitializer<ConfigurableApplicationContext>> loadInitializerClass(String className) {
  try {
    Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
    if (!ApplicationContextInitializer.class.isAssignableFrom(clazz)) {
      throw new ApplicationContextException(
          "Initializer class does not implement ApplicationContextInitializer interface: " + clazz);
    }
    return (Class<ApplicationContextInitializer<ConfigurableApplicationContext>>) clazz;
  }
  catch (ClassNotFoundException ex) {
    throw new ApplicationContextException("Failed to load context initializer class [" + className + "]", ex);
  }
}

代码示例来源:origin: spring-projects/spring-amqp

@Test
public void testNoAdmin() throws Exception {
  try {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config0.class);
    context.close();
    fail("expected initialization failure");
  }
  catch (ApplicationContextException e) {
    assertThat(e.getCause().getCause(), instanceOf(IllegalStateException.class));
    assertThat(e.getMessage(), containsString("When 'mismatchedQueuesFatal' is 'true', there must be "
        + "exactly one AmqpAdmin in the context or you must inject one into this container; found: 0"));
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Test
public void testContextLoaderWithInvalidContext() throws Exception {
  MockServletContext sc = new MockServletContext("");
  sc.addInitParameter(ContextLoader.CONTEXT_CLASS_PARAM,
      "org.springframework.web.context.support.InvalidWebApplicationContext");
  ServletContextListener listener = new ContextLoaderListener();
  ServletContextEvent event = new ServletContextEvent(sc);
  try {
    listener.contextInitialized(event);
    fail("Should have thrown ApplicationContextException");
  }
  catch (ApplicationContextException ex) {
    // expected
    assertTrue(ex.getCause() instanceof ClassNotFoundException);
  }
}

代码示例来源:origin: spring-projects/spring-amqp

@Test
public void testMismatchedQueue() {
  try {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config1.class);
    context.close();
    fail("expected initialization failure");
  }
  catch (ApplicationContextException e) {
    assertThat(e.getCause(), instanceOf(FatalListenerStartupException.class));
  }
}

代码示例来源:origin: spring-projects/spring-framework

public void doTest() throws Exception {
    try {
      throw new ApplicationContextException("foo");
    }
    catch (Exception ex) {
      catchInvoked = true;
      throw ex;
    }
    finally {
      finallyInvoked = true;
    }
  }
}

代码示例来源:origin: org.springframework/spring-web

@SuppressWarnings("unchecked")
private Class<ApplicationContextInitializer<ConfigurableApplicationContext>> loadInitializerClass(String className) {
  try {
    Class<?> clazz = ClassUtils.forName(className, ClassUtils.getDefaultClassLoader());
    if (!ApplicationContextInitializer.class.isAssignableFrom(clazz)) {
      throw new ApplicationContextException(
          "Initializer class does not implement ApplicationContextInitializer interface: " + clazz);
    }
    return (Class<ApplicationContextInitializer<ConfigurableApplicationContext>>) clazz;
  }
  catch (ClassNotFoundException ex) {
    throw new ApplicationContextException("Failed to load context initializer class [" + className + "]", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Return the WebApplicationContext implementation class to use, either the
 * default XmlWebApplicationContext or a custom context class if specified.
 * @param servletContext current servlet context
 * @return the WebApplicationContext implementation class to use
 * @see #CONTEXT_CLASS_PARAM
 * @see org.springframework.web.context.support.XmlWebApplicationContext
 */
protected Class<?> determineContextClass(ServletContext servletContext) {
  String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
  if (contextClassName != null) {
    try {
      return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
    }
    catch (ClassNotFoundException ex) {
      throw new ApplicationContextException(
          "Failed to load custom context class [" + contextClassName + "]", ex);
    }
  }
  else {
    contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
    try {
      return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
    }
    catch (ClassNotFoundException ex) {
      throw new ApplicationContextException(
          "Failed to load default context class [" + contextClassName + "]", ex);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Instantiate the root WebApplicationContext for this loader, either the
 * default context class or a custom context class if specified.
 * <p>This implementation expects custom contexts to implement the
 * {@link ConfigurableWebApplicationContext} interface.
 * Can be overridden in subclasses.
 * <p>In addition, {@link #customizeContext} gets called prior to refreshing the
 * context, allowing subclasses to perform custom modifications to the context.
 * @param sc current servlet context
 * @return the root WebApplicationContext
 * @see ConfigurableWebApplicationContext
 */
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
  Class<?> contextClass = determineContextClass(sc);
  if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
    throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
        "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
  }
  return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private ApplicationContextInitializer<ConfigurableApplicationContext> loadInitializer(
    String className, ConfigurableApplicationContext wac) {
  try {
    Class<?> initializerClass = ClassUtils.forName(className, wac.getClassLoader());
    Class<?> initializerContextClass =
        GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class);
    if (initializerContextClass != null && !initializerContextClass.isInstance(wac)) {
      throw new ApplicationContextException(String.format(
          "Could not apply context initializer [%s] since its generic parameter [%s] " +
          "is not assignable from the type of application context used by this " +
          "framework servlet: [%s]", initializerClass.getName(), initializerContextClass.getName(),
          wac.getClass().getName()));
    }
    return BeanUtils.instantiateClass(initializerClass, ApplicationContextInitializer.class);
  }
  catch (ClassNotFoundException ex) {
    throw new ApplicationContextException(String.format("Could not load class [%s] specified " +
        "via 'contextInitializerClasses' init-param", className), ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check that the FreeMarker template used for this view exists and is valid.
 * <p>Can be overridden to customize the behavior, for example in case of
 * multiple templates to be rendered into a single view.
 */
@Override
public boolean checkResource(Locale locale) throws Exception {
  String url = getUrl();
  Assert.state(url != null, "'url' not set");
  try {
    // Check that we can get the template, even if we might subsequently get it again.
    getTemplate(url, locale);
    return true;
  }
  catch (FileNotFoundException ex) {
    // Allow for ViewResolver chaining...
    return false;
  }
  catch (ParseException ex) {
    throw new ApplicationContextException("Failed to parse [" + url + "]", ex);
  }
  catch (IOException ex) {
    throw new ApplicationContextException("Failed to load [" + url + "]", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

static void unregisterApplicationContext(ConfigurableApplicationContext applicationContext) {
  synchronized (applicationContexts) {
    if (applicationContexts.remove(applicationContext) && applicationContexts.isEmpty()) {
      try {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        String mbeanDomain = applicationContext.getEnvironment().getProperty(MBEAN_DOMAIN_PROPERTY_NAME);
        if (mbeanDomain != null) {
          server.unregisterMBean(new ObjectName(mbeanDomain, MBEAN_APPLICATION_KEY, applicationName));
        }
      }
      catch (Throwable ex) {
        throw new ApplicationContextException("Failed to unregister LiveBeansView MBean", ex);
      }
      finally {
        applicationName = null;
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
  try {
    return BeanFactoryUtils.beanOfTypeIncludingAncestors(
        obtainApplicationContext(), ScriptTemplateConfig.class, true, false);
  }
  catch (NoSuchBeanDefinitionException ex) {
    throw new ApplicationContextException("Expected a single ScriptTemplateConfig bean in the current " +
        "web application context or the parent root context: ScriptTemplateConfigurer is " +
        "the usual implementation. This bean may have any name.", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

protected ScriptTemplateConfig autodetectViewConfig() throws BeansException {
  try {
    return BeanFactoryUtils.beanOfTypeIncludingAncestors(
        obtainApplicationContext(), ScriptTemplateConfig.class, true, false);
  }
  catch (NoSuchBeanDefinitionException ex) {
    throw new ApplicationContextException("Expected a single ScriptTemplateConfig bean in the current " +
        "Servlet web application context or the parent root context: ScriptTemplateConfigurer is " +
        "the usual implementation. This bean may have any name.", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Get the XSLT {@link Source} for the XSLT template under the {@link #setUrl configured URL}.
 * @return the Source object
 */
protected Source getStylesheetSource() {
  String url = getUrl();
  Assert.state(url != null, "'url' not set");
  if (logger.isDebugEnabled()) {
    logger.debug("Applying stylesheet [" + url + "]");
  }
  try {
    Resource resource = obtainApplicationContext().getResource(url);
    return new StreamSource(resource.getInputStream(), resource.getURI().toASCIIString());
  }
  catch (IOException ex) {
    throw new ApplicationContextException("Can't load XSLT stylesheet from '" + url + "'", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Check that the FreeMarker template used for this view exists and is valid.
 * <p>Can be overridden to customize the behavior, for example in case of
 * multiple templates to be rendered into a single view.
 */
@Override
public boolean checkResourceExists(Locale locale) throws Exception {
  try {
    // Check that we can get the template, even if we might subsequently get it again.
    getTemplate(locale);
    return true;
  }
  catch (FileNotFoundException ex) {
    // Allow for ViewResolver chaining...
    return false;
  }
  catch (ParseException ex) {
    throw new ApplicationContextException(
        "Failed to parse FreeMarker template for URL [" +  getUrl() + "]", ex);
  }
  catch (IOException ex) {
    throw new ApplicationContextException(
        "Could not load FreeMarker template for URL [" + getUrl() + "]", ex);
  }
}

代码示例来源:origin: org.springframework/spring-web

/**
 * Return the WebApplicationContext implementation class to use, either the
 * default XmlWebApplicationContext or a custom context class if specified.
 * @param servletContext current servlet context
 * @return the WebApplicationContext implementation class to use
 * @see #CONTEXT_CLASS_PARAM
 * @see org.springframework.web.context.support.XmlWebApplicationContext
 */
protected Class<?> determineContextClass(ServletContext servletContext) {
  String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
  if (contextClassName != null) {
    try {
      return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
    }
    catch (ClassNotFoundException ex) {
      throw new ApplicationContextException(
          "Failed to load custom context class [" + contextClassName + "]", ex);
    }
  }
  else {
    contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
    try {
      return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
    }
    catch (ClassNotFoundException ex) {
      throw new ApplicationContextException(
          "Failed to load default context class [" + contextClassName + "]", ex);
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

@SuppressWarnings("unchecked")
private void invokeApplicationContextInitializers(ConfigurableApplicationContext context,
    MergedContextConfiguration mergedConfig) {
  Set<Class<? extends ApplicationContextInitializer<?>>> initializerClasses =
      mergedConfig.getContextInitializerClasses();
  if (initializerClasses.isEmpty()) {
    // no ApplicationContextInitializers have been declared -> nothing to do
    return;
  }
  List<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerInstances = new ArrayList<>();
  Class<?> contextClass = context.getClass();
  for (Class<? extends ApplicationContextInitializer<?>> initializerClass : initializerClasses) {
    Class<?> initializerContextClass =
        GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class);
    if (initializerContextClass != null && !initializerContextClass.isInstance(context)) {
      throw new ApplicationContextException(String.format(
          "Could not apply context initializer [%s] since its generic parameter [%s] " +
          "is not assignable from the type of application context used by this " +
          "context loader: [%s]", initializerClass.getName(), initializerContextClass.getName(),
          contextClass.getName()));
    }
    initializerInstances.add((ApplicationContextInitializer<ConfigurableApplicationContext>) BeanUtils.instantiateClass(initializerClass));
  }
  AnnotationAwareOrderComparator.sort(initializerInstances);
  for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : initializerInstances) {
    initializer.initialize(context);
  }
}

代码示例来源:origin: spring-projects/spring-framework

static void registerApplicationContext(ConfigurableApplicationContext applicationContext) {
  String mbeanDomain = applicationContext.getEnvironment().getProperty(MBEAN_DOMAIN_PROPERTY_NAME);
  if (mbeanDomain != null) {
    synchronized (applicationContexts) {
      if (applicationContexts.isEmpty()) {
        try {
          MBeanServer server = ManagementFactory.getPlatformMBeanServer();
          applicationName = applicationContext.getApplicationName();
          server.registerMBean(new LiveBeansView(),
              new ObjectName(mbeanDomain, MBEAN_APPLICATION_KEY, applicationName));
        }
        catch (Throwable ex) {
          throw new ApplicationContextException("Failed to register LiveBeansView MBean", ex);
        }
      }
      applicationContexts.add(applicationContext);
    }
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

private Class<?> getInitializerClass(String className) throws LinkageError {
  try {
    Class<?> initializerClass = ClassUtils.forName(className,
        ClassUtils.getDefaultClassLoader());
    Assert.isAssignable(ApplicationContextInitializer.class, initializerClass);
    return initializerClass;
  }
  catch (ClassNotFoundException ex) {
    throw new ApplicationContextException(
        "Failed to load context initializer class [" + className + "]", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Autodetect a {@link FreeMarkerConfig} object via the ApplicationContext.
 * @return the Configuration instance to use for FreeMarkerViews
 * @throws BeansException if no Configuration instance could be found
 * @see #setConfiguration
 */
protected FreeMarkerConfig autodetectConfiguration() throws BeansException {
  try {
    return BeanFactoryUtils.beanOfTypeIncludingAncestors(
        obtainApplicationContext(), FreeMarkerConfig.class, true, false);
  }
  catch (NoSuchBeanDefinitionException ex) {
    throw new ApplicationContextException(
        "Must define a single FreeMarkerConfig bean in this web application context " +
            "(may be inherited): FreeMarkerConfigurer is the usual implementation. " +
            "This bean may be given any name.", ex);
  }
}

相关文章