Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么spring@autowired为空?_Spring - Fatal编程技术网

为什么spring@autowired为空?

为什么spring@autowired为空?,spring,Spring,我正在尝试在我的rest控制器中自动连接服务,如下所示: 休息控制器: @ApplicationPath("/greetings") @Component(immediate = true, service = Application.class) public class RestControllerApplication extends Application { @Autowired private MyService myService; public

我正在尝试在我的rest控制器中自动连接服务,如下所示:

休息控制器:

@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);