java-调用Spring bean的函数接口

我想映射特定类型以触发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.

最后,在afterPropertiesSet()方法中,放置Map初始化代码,但是使用Autowired MySpringService来注册您的方法调用,因此您不需要从静态上下文中调用Spring bean.

相关文章

这篇文章主要介绍了spring的事务传播属性REQUIRED_NESTED的原...
今天小编给大家分享的是一文解析spring中事务的传播机制,相...
这篇文章主要介绍了SpringCloudAlibaba和SpringCloud有什么区...
本篇文章和大家了解一下SpringCloud整合XXL-Job的几个步骤。...
本篇文章和大家了解一下Spring延迟初始化会遇到什么问题。有...
这篇文章主要介绍了怎么使用Spring提供的不同缓存注解实现缓...