[英]Jersey Test + Spring - not injecting beans into Controllers
Could someone tell me please how to get spring injection working on Controller classes registered in resource config: 有人可以告诉我如何在资源配置中注册的Controller类上进行spring注入:
ResourceConfig resourceConfig = new ResourceConfig(controllerClasses);
I include the spring context like this: 我包括这样的spring上下文:
private void mergeSpringContext(ResourceConfig resourceConfig) {
ContextConfiguration annotation = getClass().getAnnotation(ContextConfiguration.class);
if(annotation != null) {
Class<?>[] contextClasses = annotation.classes();
// ApplicationContext context = new AnnotationConfigApplicationContext(contextClasses);
// resourceConfig.property("contextConfig", context);
resourceConfig.registerClasses(contextClasses);
}
}
You can see two approaches there but neither works. 您可以在此处看到两种方法,但两种方法均无效。 Jersey-test always creates a new instance of the controller and spring can't know about it. Jersey-test总是创建控制器的新实例,而spring对此一无所知。 Is there a way to make jersey use spring-instantiated controller (which is managed and will have all fields set) bean? 有没有一种方法可以使jersey使用spring实例化的控制器(受管理并将设置所有字段)bean? Or do I have to get myself into trouble of using some AOP (@Configurable on the controller class). 还是我必须让自己陷入使用某些AOP的麻烦(控制器类上的@Configurable)。
There is something called 有一种叫做
jersey-spring3
and it's supposed to do it. 它应该做到的。 And I think it does but not in the way I'd like it to - it searches for applicationContext.xml on my classpath. 而且我认为它确实可以,但不是我想要的方式-它在类路径上搜索applicationContext.xml。 And this is not what I want because it is the test case which specifies which spring contexts to load. 这不是我想要的,因为它是指定要加载哪些spring上下文的测试用例。
Thanks! 谢谢!
// EDIT //编辑
I managed to use spring-instantiated controllers in my application like this: 我设法在我的应用程序中使用spring实例化的控制器,如下所示:
private void mergeSpringContext(ResourceConfig resourceConfig) {
ContextConfiguration annotation = getClass().getAnnotation(ContextConfiguration.class);
if(annotation != null) {
Class<?>[] contextClasses = annotation.classes();
ApplicationContext context = new AnnotationConfigApplicationContext(contextClasses);
Map<String, Object> beansOfType = context.getBeansWithAnnotation(Controller.class);
Collection<Object> controllers = beansOfType.values();
resourceConfig.registerInstances(controllers.toArray());
}
}
The problem with it is that I still don't have a clue how to put spring security in there... 问题是我仍然不知道如何在其中安装弹簧安全性...
We do a little differently. 我们做的有些不同。
In our WebAppInit, we have: 在我们的WebAppInit中,我们有:
ResourceConfig resourceConfig = new ResourceConfig()
.packages(
'foo.package1',
'foo.package2'
)
Then a RootConfig 然后是RootConfig
@Configuration
@EnableCaching
@ComponentScan(basePackages = [
Then each injectable is annotated with @Named and each usage with @Inject 然后,每个可注入对象都用@Named注释,每个用法都使用@Inject注释。
Good luck 祝好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.