为什么spring@autowired为空?
我正在尝试在我的rest控制器中自动连接服务,如下所示: 休息控制器:为什么spring@autowired为空?,spring,Spring,我正在尝试在我的rest控制器中自动连接服务,如下所示: 休息控制器: @ApplicationPath("/greetings") @Component(immediate = true, service = Application.class) public class RestControllerApplication extends Application { @Autowired private MyService myService; public
@ApplicationPath("/greetings")
@Component(immediate = true, service = Application.class)
public class RestControllerApplication extends Application {
@Autowired
private MyService myService;
public Set<Object> getSingletons() {
return Collections.<Object>singleton(this);
}
@POST
@Path("/getUploadType")
@Produces("application/json")
public JsonObject getUploadType() {
...
myService.findUploadTypes();
...
}
}
但在我的rest控制器中,上传服务是空的。为什么?Spring使用自己的注释集。您应该使用
@RequestMapping
,而不是@Path
加@[HTTP方法]
你可以找到一个例子
还有一个扩展示例我可以通过以下几行代码访问我的bean:
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyService myService = context.getBean(MyService.class);
您正在将
UploadService
声明为@Component
,但试图在控制器中自动连接MyService
实例
有两个选项:你可以在你的控制器中声明正确的服务类型,或者你可以使
UploadService
继承自MyService
的组件扫描问题无法检测到UploadService?看起来你把jax-rs注释和Spring注释混在一起了。也许JavaEE容器为您提供了rest控制器,而不是Spring@Pelocho是的,这就是问题所在…如何将SpringBean自动连接到jax-rs组件中?问题在于我使用的是希望使用jax-rs构建rest服务的liferay 7,但是我的所有应用程序都是用spring开发的,我需要将Springbean注入这个jax rs rest控制器
WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
MyService myService = context.getBean(MyService.class);