Spring static initialization of a bean

懵懂的女人 提交于 2019-12-01 23:42:32

Having static dependencies on other beans is not a Spring way.

However, if you want to keep it static, you can initialize it lazily - in that case depends-on can enforce proper initialization order.

EDIT: By lazy loading I mean something like this (I use lazy initialization with holder class idiom here, other lazy initialization idioms can be used instead):

private static class ExceptionMappingHolder {
    private static final Map<String, String> exceptionMapping = 
        ErrorExceptionMapping.getExceptionMapping(); 
}

and use ExceptionMappingHolder.exceptionMapping instead of exceptionMapping.

You should be able to mark the class with the @Component annotation, then add a non static setter with @Autowired(required=true) annotation for setting the static variable.

Here's a link for more info.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!