Spring中ApplicationContext.xml和属性文件的位置 [英] Location of ApplicationContext.xml and properties file in spring

查看:183
本文介绍了Spring中ApplicationContext.xml和属性文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我已将ApplicationContext xml文件放置在src中,并且该项目运行正常.
我们可以将ApplicationContext.xml放在我们的WebContent或Web-Inf文件夹中吗?

In my application I have placed the ApplicationContext xml file in src and the project is working fine.
Can we place the ApplicationContext.xml in our WebContent or Web-Inf folder?

我还想知道是否可以将属性文件放置在WebContent中.

Also I want to know if I can place my properties file in WebContent.

由于我已将ApplicationContext.xml放置在src中,因此将属性文件放置在src中,并且工作正常.下面是它的代码

Since I have placed my ApplicationContext.xml in src, I placed my properties file in src and that worked fine. Below is the code for it

<bean id="licenseSettings"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:LicenseSettings.properties" />
</bean>

我尝试将属性文件放在WebContent/conf中,但未成功读取属性.

I tried putting the properties file in WebContent/conf but the properties were not read succesfully.

我的问题是,我们可以将ApplicationContext.xml和属性文件放在WebContent文件夹中吗?

My question is, can we put ApplicationContext.xml and properties file somewhere inside WebContent folder?

更新:我将ApplicationContext.xml放在WEB-INF/classes中,并使用ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");

Update: I put my ApplicationContext.xml in WEB-INF/classes and it was read successfully using ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContext.xml");

我唯一未解决的问题是,我应该在哪里放置我的属性文件,什么地方适合放置它?

My only question remaining unanswered is, where shall I put my properties file and where is it appropriate to be put in?

推荐答案

您可以通过以下几种方式:

There are couple of ways you can do this:

在Spring MVC中,您在web.xml中提到dispatcher-servlet如下

In Spring MVC, You mention dispatcher-servlet in web.xml as follows

 <servlet>
          <servlet-name>mvc-dispatcher</servlet-name>
            <servlet-class>
                      org.springframework.web.servlet.DispatcherServlet
            </servlet-class>
            <load-on-startup>1</load-on-startup>
</servlet>

现在默认情况下,它将查找名为mvc-dispatcher-servlet.xml的文件.即,-servlet.xml附加的servlet名称.它将在类路径中查找此文件.

Now this by default will look for a file named mvc-dispatcher-servlet.xml. That is, the servlet name appended by -servlet.xml. And it will look for this file in class path.

或者 适合您的情况,如果您已经有xml文件并且不想重命名,请在web.xml中的以下条目中添加添加到上面的servlet条目中.

Alternatively which fits your case, if you already have xml file and don't want to rename it, add the following entry in web.xml in addition to the servlet entry above.

 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/conf/ApplicationContext.xml</param-value>
 </context-param

在这里您可以选择WEB-INF内的任何位置.将属性文件放在classes文件夹中,以便可以在classpath中找到它.由于您使用的是Eclipse嵌入式Tomcat,因此将以下内容作为属性占位符的bean配置.

Here you can choose any location inside WEB-INF. Put the properties file in classes folder so that it could be found in classpath. Since you are using Eclipse embedded Tomcat, put the following as your bean configuration for property placeholder.

 <property name="location" value="classpath:../../conf/LicenseSettings.properties" /> 

这篇关于Spring中ApplicationContext.xml和属性文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆