ErrorMessageExceptionTypeRouter

ErrorMessageExceptionTypeRouter

This question already has answers here:
What is a NullPointerException, and how do I fix it?
                            
                                (12个答案)
                            
                    
                2年前关闭。
        

    

我做了一个简单的测试,但没有用。

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = {SIConfiguration.class, ExceptionTypeRouter.class})
public class ExceptionTypeRouterTest {

    @Autowired
    @Qualifier("inDefaultChannel")
    private DirectChannel in;

    @Test
    public void fistRouteTest() {

        in.send(new GenericMessage<>("ready!"));

    }
}


@Configuration
@Slf4j
public class ExceptionTypeRouter {

    @Bean
    @Qualifier("inDefaultChannel")
    public DirectChannel inDefaultChannel() {
        return MessageChannels
                .direct()
                .get();
    }

    @Bean
    @Qualifier("directErrorChannel")
    public DirectChannel directErrorChannel() {
        return MessageChannels
                .direct()
                .get();
    }

    @Bean
    public ErrorMessageExceptionTypeRouter errorMessageExceptionTypeRouter() {
        ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
        router.setChannelMapping(IOException.class.getName(), "directErrorChannel");
        router.setChannelMapping(NullPointerException.class.getName(), "directErrorChannel");
        router.setDefaultOutputChannel(outDefaultChannel());

        return router;
    }
}


问题就在这里。标记this.getApplicationContext()为null。

 private Class<?> resolveClassFromName(String className) {
        try {
            return ClassUtils.forName(className, this.getApplicationContext().getClassLoader());
        } catch (ClassNotFoundException var3) {
            throw new IllegalStateException("Cannot load class for channel mapping.", var3);
        }
    }


错误日志:


  org.springframework.beans.factory.BeanCreationException:创建名称为'errorMessageExceptionTypeRouter'的bean时出错
  在integration.exceptiondemo.configuration.router.ExceptionTypeRouter中定义的:通过工厂方法实例化Bean
   失败嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework。
  Integration.router.ErrorMessageExceptionTypeRouter]:工厂方法'errorMessageExceptionTypeRouter'抛出异常;嵌套的
   异常是java.lang.NullPointerException

最佳答案

同意这是ErrorMessageExceptionTypeRouter中的错误。

当您呼叫setChannelMapping()时,它不会检查this.initialized并在我们尚未填充resolveClassFromName()时尝试呼叫提到的ApplicationContext

请提出一个JIRA进行修复:https://jira.spring.io/projects/INT

同时,作为解决方法,您应该考虑使用setChannelMappings(Map<String, String>)代替:

@Bean
public ErrorMessageExceptionTypeRouter errorMessageExceptionTypeRouter() {
    ErrorMessageExceptionTypeRouter router = new ErrorMessageExceptionTypeRouter();
    Map<String, String> mappings = new HashMap<>();
    mappings.put(IOException.class.getName(), "directErrorChannel");
    mappings.put(NullPointerException.class.getName(), "directErrorChannel");
    router.setChannelMappings(mappings);
    router.setDefaultOutputChannel(outDefaultChannel());

    return router;
}

09-10 21:19