简体   繁体   English

使用spring查找和使用XML配置文件

[英]Find and use XML Configuration file with spring

So I have a requirement for a spring filter where there are some urls that do not need to be redirected, because we just introduced responsive design. 因此,我需要一个spring过滤器,该过滤器中的某些URL不需要重定向,因为我们只是引入了响应式设计。

I want to have an xml config like this: 我想要一个这样的xml配置:

<config>
   <pages>
       <url>/some/responsive/url/1</url>
       <url>/some/responsive/url/2</url>
       ...
   </pages>
</config>

And my filter to look like this: 我的过滤器看起来像这样:

protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        if(!isResponsiveUrl(request.getServletPath())) {
            response.sendRedirect(externalUrl); //redirecto to a 3th party which provide us responsiveness
            return;
        } 
        filterChain.doFilter(request, response);
}

I have a resources folder where I want to put my xml, can someone help me find a way of reading this xml and getting the url list to implement the isResponsiveUrl ? 我有一个要放置xml的资源文件夹,有人可以帮我找到一种读取xml并获取实现isResponsiveUrl的url列表的方法吗?

Can spring find this config file automatically, and how? spring可以自动找到此配置文件吗?

I don't mind if the file isn't XML, it could be a .properties file! 我不介意该文件不是XML,它可能是.properties文件!

The goal with this, is that if we need to add more pages, we can just update the file without restarting the app. 这样做的目的是,如果我们需要添加更多页面,则只需更新文件即可,而无需重新启动应用程序。

Thanks for your help :) 谢谢你的帮助 :)

You can create a util:list and use @Resource to inject on your attribute: 您可以创建一个util:list并使用@Resource注入您的属性:

XML will look like this: XML将如下所示:

<util:list id="pages">
   <value>/some/responsive/url/1</value>
   <value>/some/responsive/url/2</value>
</util:list>

On Java, simply annotate the attribute: 在Java上,只需注释属性:

@Resource(name="pages")
private List<String> pages;

OBS: Don't forget to include spring-util.xsd to your bean definitions. OBS:不要忘记在您的bean定义中包含spring-util.xsd。

You can take more information on Spring doc site . 您可以在Spring doc网站上获取更多信息。

I hope I´ve helped 我希望我有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM