我正在使用JADE,JASON (代理框架)和Spring Boot开发一个应用程序。基本上,我有一个JADE Container,Jade和Jason代理都在其中注册。因为我使用的是Spring,所以我倾向于自动连接服务。在这种情况下,我需要访问一些服务,在一些Jason内部操作(我自定义编写的扩展DefaultInternalAction类)中。这似乎不起作用。我知道如何自动连接,以及Beans是如何工作的。我怀疑这些内部操作是否在spring上下文中。我想他们不是。这就是为什么Autowire的事情不能正常工作。有没有人能给我解释一下jade容器中的真实动作和内部动作,这样我就可以从不同的角度考虑在jason内部动作中使用Autowire了。
发布于 2018-07-09 18:17:46
据我所知,内部操作是由jason创建的,而不是spring,这就是为什么你不能自动布线服务。就个人而言,我创建了工厂并使用它来获取服务的实例。如下所示:
public class SpringPluginFactory {
private static final SpringPluginFactory INSTANCE = new SpringPluginFactory();
private ApplicationContext applicationContext;
private SpringPluginFactory(){}
private <T> T createPlugin(Class<T> iface) {
if(applicationContext == null){
throw new IllegalStateException("applicationContext cannot be null");
}
try {
return applicationContext.getBean(iface);
} catch (Exception e) {
throw new RuntimeException("factory unable to construct instance of " + iface.getName());
}
}
public static <T> T getPlugin(Class<T> iface){
return INSTANCE.createPlugin(iface);
}
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}}
然后我创建bean来设置aplicationContext:
@Bean
public SpringPluginFactory pluginFactory(ApplicationContext applicationContext){
SpringPluginFactory pluginFactory = SpringPluginFactory.INSTANCE;
pluginFactory.setApplicationContext(applicationContext);
return pluginFactory;
}并在任何行为或内部操作中使用工厂
SpringPluginFactory.getPlugin(YouService.class).doSomething();也许这会有所帮助。
https://stackoverflow.com/questions/51147778
复制相似问题