I made a simple test and it dosen't work.
@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;
}
}
The proplem is somwhere here. Caze this.getApplicationContext() is 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);
}
}
Error log:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'errorMessageExceptionTypeRouter' defined in integration.exceptiondemo.configuration.router.ExceptionTypeRouter: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework. integration.router.ErrorMessageExceptionTypeRouter]: Factory method 'errorMessageExceptionTypeRouter' threw exception; nested exception is java.lang.NullPointerException