【问题标题】:How hide exception while bean initializingbean初始化时如何隐藏异常
【发布时间】:2019-04-26 19:31:14
【问题描述】:

我正在创建一些 Spring 服务类。一种方法返回

com.mxgraph.view.mxGraph

在静态上下文中加载一些资源包,特别是对于当前语言环境,但源中没有俄语语言环境,我不会添加它们,因为它们在项目中没用。我想处理这个类的创建并隐藏异常

java.util.MissingResourceException.

我尝试编写自定义 bean 后处理器(因为调试器说它发生在这个阶段),但是 id 不起作用。

这是服务接口

public interface GraphService
{
    String saveGraph( mxGraphModel graphModel );

    void updateGraph( String id , mxGraphModel graphModel );

    mxGraph getGraph( String id );

    mxGraphModel xmlToGraph( String xml ) throws IOException, SAXException;

    String graphToXml( mxGraph mxGraph );
}

这是日志:

java.util.MissingResourceException: Can't find bundle for base name com.mxgraph.resources.graph, locale ru_RU
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1573)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1396)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:782)
    at com.mxgraph.util.mxResources.add(mxResources.java:55)
    at com.mxgraph.view.mxGraph.<clinit>(mxGraph.java:191)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.sun.proxy.$Proxy578.<clinit>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739)
    at org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:121)
    at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:109)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:447)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:333)
    at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5157)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5680)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)

我想隐藏它只是因为它是无用的例外。

【问题讨论】:

  • “隐藏”异常的最佳方法是实际修复它。
  • 另外,发布您阅读属性的方式。
  • @Nikolas,mxGraph 库不是我的项目,通常我无法修复那里的代码。也许我可以添加俄语语言环境属性文件,但是 a) 它没用,b) 我们不知道它是如何工作的,如果没有任何属性。并通过代码收集所有强制性值......我想你明白了。这就是我在这里发布问题的原因:)
  • 它看起来像您的自定义代码。这就是为什么我有兴趣了解读取属性的方式。有时,我们不得不接受 3rd 方库的不足。您是否在项目 GitHub 上发布过问题?大多数作者阅读并尝试提供帮助:) 祝你好运

标签: java spring-4


【解决方案1】:

正如@Nikolas 所说:“隐藏”异常的最佳方法是实际修复它!

但如果你真的想隐藏一些异常,最好不要,你可以使用Thread.UncaughtExceptionHandler做类似的事情:

public class TestThread implements Runnable {
    @Override
    public void run() {
        throw new RuntimeException();
    }
}

public class ThreadDemo {
    public void startTest() {
        Thread testThread = new Thread(new TestThread());
        testThread.setUncaughtExceptionHandler((t, e) -> {
            // Commenting this line the exception will be not notified
            System.out.println(t + " throws exception: " + e);

            // or filter the exceptions using reflection
            // in your case MissingResourceException.class
            if (e.getClass() == RuntimeException.class) {
                System.out.println("Exception filtered successfully");
            }
        });

        testThread.start();
    }
}

public class Main {
    public static void main(String[] args) {
        ThreadDemo testThread = new ThreadDemo();
        testThread.startTest();
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多