I surveyed this time. I found this one . In the end, I used the following solution.
According to 1 created by ApplicationContextProvider:
public class ApplicationContextProvider implements ApplicationContextAware { private static ApplicationContext applicationContext; public static ApplicationContext getApplicationContext() { return applicationContext; } public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { ApplicationContextProvider.applicationContext = applicationContext; } }
Then AppNameProviderFactory, which maps providers to keys. The key may be the name of the exception:
public class AppNameProviderFactory { private Map<String,AppNameProvider> map; public void setMap(Map<String, AppNameProvider> map) { this.map = map; } public AppNameProvider getAppNameProvider(String key) { return map.get(key); } }
And in xml, I define the mappings:
<bean id="appNameProviderFactory" class="some.domain.AppNameProviderFactory"> <property name="map"> <map> <entry key="MyException" value-ref="appNameProvider"/> </map> </property> </bean>
And finally, in the exception class:
public class MyException extends RuntimeException {
This way I have a configuration in xml, detachable from the business code. If necessary, I can have as many exceptions as possible with different providers.
Thanks everyone for the suggestions. PS Error handling and NPE processing are simplified.
source share