Location of ApplicationContext.xml and properties file in spring - java

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?
Also I want to know if I can place my properties file in WebContent.
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>
I tried putting the properties file in WebContent/conf but the properties were not read succesfully.
My question is, can we put ApplicationContext.xml and properties file somewhere inside WebContent folder?
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:
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>
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.
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
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" />

Yes you can.
If you put the files directly inside WEB-INF that should work.
If you want to put them in a folder called WEB-INF/conf you would need to change the properties location to refer to conf/LicenseSettings.properties

Related

How to specify a file path in web.xml file

i am developing an application in java and i am new to this java ee platform.I have a dataset file called "51Degrees-EnterpriseV3_2.dat" in my resource folder. how do i specify the path of this in my web.xml file and how do i retrieve the file in code?
In web.xml , you can add
<context-param>
<param-name>you can mention here localtion variable name</param-name>
<param-value>location here </param-value>
</context-param>
The other answer shows the correct way to add the parameter to your web.xml. Which for the 51Degrees V3.2 data file would look like this:
<context-param>
<description>The name of the device database.</description>
<param-name>BINARY_FILE_PATH</param-name>
<param-value>51Degrees-EnterpriseV3_2.dat</param-value>
</context-param>
As far as I know, there is not a way to get a resource which is located in the resources path (it's an XML file so the .getResource(name) method cannot be used). However, as you have a web.xml file, I assume this is a web project (i.e. .war rather than .jar)? If this is the case, then the WEB-INF directory is what you need. Files in here are also packaged up like resources, but can be more easily consumed by a .war package.
If you put your data file in src/main/webapp/WEB-INF/, which is used as the root by the web project. So the above XML example will work if the path to your data file is:
src/main/webapp/WEB-INF/51Degrees-EnterpriseV3_2.dat
As a sidenote, there is more documentation on configuring 51Degrees in a Java web app here

Accessing bean definition xml files from jar to main context file of war

i have beans.xml file inside servics.jar at META-INF\Spring location,servics.jar present inside WEB-INF\lib. now i am trying to access that beans.xml in my appcontext.xml using below import tag
<import resource="classpath*:META-INF/spring/beans.xml" />
but after starting server i am getting "No bean named 'mybean' is defined" error.
here mybean is defined in beans.xml.
i have set contextConfigLocation in web.xml as below
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/appContext.xml</param-value>
</context-param>
as per error description i am bit confused whether beans.xml get loaded or not.
if loaded why it giving "No bean defined error".
can some one explain how to access bean definition file from jar to main appContext.xml.
<import resource="classpath:/META-INF/spring/beans.xml" />
did the work for me.For difference between classpath* vs classpath please refer question Spring classpath prefix difference

ClassPathXmlApplicationContext classpath notfound

I am newbie in Spring. I have problem with classpath as parameter of ClassPathXmlApplicationContext. I use Netbeans. The class where I use ClassPathXmlApplicationContext is in package cz.ryska.helloworld in Source Packages folder. Configuration XML file, name as Beans.xml is in WEB-INF folder.
But I don't know what way I can use. I try use f.e "WEB-INF\Beans.xml" or "/WEB-INF/Beans.xml" but file is not found. What way I can use or where I must move xml file?
Thanks
The project appears to be a J2EE web application, so you shouldn't need to create a new DI container with ClassPathXmlApplicationContext. Instead import beans.xml in your applicationContext.xml file, if it is already registered in web.xml.
In web.xml include:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
In applicationContext.xml include:
<import resource="Beans.xml"/>
This will cause the Beans.xml configuration to be added to the DI container.

applicationContext.xml is being copied to /WEB-INF/classes from src/main/resources

however tomcat is throwing the error :
IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml];
Which suggests that tomcat expects the applicationContext.xml to be one directory above where it is copied to. It is being copied to the /WEB-INF/classes directory, not just the plain old /WEB-INF
I am using maven.
If you have put it under src/main/resources, you should refer to the resource as classpath:/applicationContext.xml
In your web.xml you can state where your applicationContext.xml is located. If it is in /WEB-INF/classes then you must state /WEB-INF/classes/applicationContext.xml (and not just /WEB-INF/applicationContext.xml).
Try this:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
To include more than one context files you can use the import function. To do so, write into your applicationContext.xml one line per file like this:
<import resource="myOtherSpringContext.xml"/>
Use the following syntax in your web.xml ( http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/context/ContextLoader.html ):
<!-- list of the configuration files to load -->
<context-param>
<!-- Spring -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
It will load it from the classes/* directory.
BTW, if you want to copy the applicationContext.xml directly in your WEB-INF directory, move it in /src/main/webapp
BTW, here is the way recommanded by the spring documentation: http://static.springsource.org/spring/docs/3.0.x/reference/beans.html#context-create
HIH
This is standard Maven behaviour, and is what a lot of people use with no objection. Why do you prefer it in WEB-INF?
Anyway, if you want it there, you can just put it in src/main/webapp/WEB-INF.
the standard way is to create new source dir src/main/webapp. All files from this dir will be places in the same folder as WEB-INF in resulting war. So, inside this new source dir you can create WEB-INF and place you applicationcontext.xml in it.
so you structure would look much like
+src
+---main
+---+---java
+---+---webapp
+---+------WEB-INF
check maven-war-plugin documentation

Maven copying applicationContext.xml from src/main/resources to target/myproject/WEB-INF

At the moment, the default I think, it copies to
target/myproject/WEB-INF/classes
so when deploying it does not pick up the context.
Also, i want to reference a server specific config file database.properties, I want to put it in tomcat/conf and then reference it in applicationContext.xml, how can I do this ?
Also(2), I am under the impression that this is a fairly standard and decent way to set things up - please correct me if I am wrong.
edit
for the server specific config file I user this
<context:property-placeholder
location="file:${catalina.home}/conf/database.properties"
ignore-unresolvable="true"
/>
If you need to keep applicationContext.xml as a classpath resource, you can configure ContextLoaderListener to pick it from the classpath by adding the following lines to web.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
It's much easier than configuring Maven to copy it to WEB-INF.
Regarding the second question, you can configure PropertyPlaceholderConfigurer or <context:property-placeholder> to load .properties file from a file system.
For your title question: Often in a .war maven module, you'll put web related resources under src/main/webapp instead of src/main/resources. Then the maven plugin will pick them up automatically because it matches convention. So, move your applicationContext.xml to src/main/webapp/WEB-INF
Another option is to configure the webResources as described in the documentation
For the second question you can look at a PropertyPlaceHolderConfigurer. You'll just have to get the path correct.

Categories

Resources