我的問題很簡單。我試圖將一個屬性值注入過濾器。在我的控制器類,我用下面的代碼,我可以成功地注入屬性:如何將屬性值注入過濾器?
@Controller
public class StatementController {
@Value("${xapi.version}")
private String version;
...
}
,但是當我在自定義過濾器使用相同的代碼,我得到的版本屬性爲空值。這是代碼:
public class HeaderFilter extends OncePerRequestFilter {
@Value("${xapi.version}")
private String version;
private Logger logger = Logger.getLogger(this.getClass());
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
logger.error(version==null);
...
我使用springframework的4.0.9,這是我的過濾器配置的web.xml中的片段:
<filter>
<filter-name>header</filter-name>
<filter-class>com.application.HeaderFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>header</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>