开发者

How to get display-name from web.xml in a Spring IoC XML Configuration

开发者 https://www.devze.com 2023-02-08 03:03 出处:网络
To be exact, how can I get the value of the <display-name> tag under the <web-app> tag stored in an application\'s web.xml in a Spring application context configuration XML file.

To be exact, how can I get the value of the <display-name> tag under the <web-app> tag stored in an application's web.xml in a Spring application context configuration XML file.

Ideally I would like something like the following...

<bean><property value="${servletContext.servletContextName}/></bean>

It seem like ServletContext.getServletContextName() does what I want but I can't seem to figure ou开发者_Python百科t how to get a handle on that in the Spring application context file.


Ok, the answer is trivial in Spring 3.0.x. Per the documentation for ServletContextFactory

Deprecated. as of Spring 3.0, since "servletContext" is now available as a default bean in every WebApplicationContext

So I decided to try the following and it worked!

<bean><property value="#{servletContext.servletContextName}/></bean>

Since servletContext object is implicitly defined we can access it via Spring EL using the #{} syntax.


I don't think you can do this via the XML config.

You can autowire a bean to receive the ServletContext object (or implement ServletContextAware), and fetch it from that programmatically, but I don't think the XML expressions have any visibility of it.


Maybe try the Expression Language?

<bean>
   <property value="#{T(javax.servlet.ServletContext).getServletContextName()}"/>
</bean>

I suspect that would print null if it works though, since there is no context yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号