簡體   English   中英

如何在bean初始化時隱藏異常

[英]How hide exception while bean initializing

我正在創建一些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所說:“隱藏”異常的最佳方法是實際修復它!

但是如果你真的想要隱藏一些異常,最好不要這樣做,你可以使用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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM