如何在bean初始化时隐藏异常

问题描述 投票:0回答:1

我正在创建一些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)

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

java spring-4
1个回答
0
投票

正如@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();
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.