我想映射特定类型以触发Spring方法,
我通过键保存功能接口映射,这些函数将调用Spring services方法,但是我有一个问题,它必须是静态的,例如:
private Map<Pair<Type,Boolean>,Function<User,Boolean>> functionInterfaces = new HashMap<>();
{
functionInterfaces .put(Pair.of(Type.MY_TYPE,Boolean.TRUE),MySpringService::myTypeMethod);
}
所以我的方法必须是静态的
public static boolean myTypeMethod(User user)
我应该静态加载Spring bean以便调用静态方法:
private static final MySpringService mySpringService = ApplicationInitializer.getAppContext().getBean(MySpringService.class);
还是没有静态初始化Spring Bean会更好?
最佳答案
我会在定义地图的Bean上使用Spring的InitializingBean接口.
然后,您@Autowire在bean中使用MySpringService.
然后,您@Autowire在bean中使用MySpringService.
最后,在afterPropertiesSet()方法中,放置Map初始化代码,但是使用Autowired MySpringService来注册您的方法调用,因此您不需要从静态上下文中调用Spring bean.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。