加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java-调用Spring bean的函数接口

发布时间:2020-12-15 01:17:38 所属栏目:大数据 来源:网络整理
导读:我想映射特定类型以触发Spring方法, 我通过键保存功能接口映射,这些函数将调用Spring services方法,但是我有一个问题,它必须是静态的,例如: private MapPairType,Boolean,FunctionUser,Boolean functionInterfaces = new HashMap(); { functionInterfaces .

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读