What does context annotation do in Spring?

点点圈 提交于 2019-12-08 18:58:11

问题


In Rest API design, I am wondering what the exact purpose of the context annotation is?

private HttpServletRequest request;

 @Context
 public void setRequest(final HttpServletRequest req) {
        request = req;
 }

回答1:


The purpose is to indicate that the request property should be set from the context.

@Context is used to inject various HTTP-ish contextual data, from here:

In general @Context can be used to obtain contextual Java types related to the request or response.

API docs (Not horribly useful IMO. Or, perhaps more accurately, horribly-useful.)




回答2:


This annotation is used to inject information into a class field, bean property or method parameter.

JAX-RS @Context to get the ServletContext, and WebApplicationContextUtils to get the Spring application context, with this Spring application context, you are able to access and get beans from Spring container



来源:https://stackoverflow.com/questions/10962696/what-does-context-annotation-do-in-spring

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!