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.
Related
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
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
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
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.
I'm using Spring's dependency injection but I'm running into difficulty loading a resource in my Spring config file.
The resource is an XML file and is in a JAR file on my classpath. I try to access it as follows:
<import resource="classpath:com/config/resources.xml" />
however I keep getting encountering the following error:
Failed to import bean definitions from URL location [classpath:com/config/resources.xml]
The JAR file is on the classpath of a Java project, which is in turn used by my web app. Should I really be doing my Spring configuration in the web project as opposed the Java project, or does that matter?
If it needs to be in the classpath of your webapp, then you should stick the JAR containing the config file into your WEB-INF/lib directory.
If you're using a webapp, then the common convention is use a ContextLoaderListener to ensure a WebApplicationContext is inserted into a standard place in the ServletContext:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/com/config/resources.xml</param-value>
</context-param>
Then use WebApplicationContextUtils to fish the application context out of the servlet context using:
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
I ran into a similar issue with a red5 plugin. I resolved it like so:
try {
subContext = new FileSystemXmlApplicationContext(new String[] { "classpath*:/myconfig.xml" }, true, context);
} catch (Exception fnfe) {
subContext = new FileSystemXmlApplicationContext(new String[] { "plugins/myconfig.xml" }, true, context);
}
This will look anywhere on the classpath first, including within the jar that contains my code. If an exception occurs the plugin directory is checked. It may not be the best solution but it works.
I don't really recall why this matters, but try putting an asterisk () in front of the colon (:) classpath:/ If this doesn't work, try the asterisk after the colon (classpath:*), although I think it was before the colon.
I've only used the <import> directive in J2SE and it works without the classpath: prefix, simply as <import resource="config/resources.xml" />. But in J2EE if all your files are inside WEB-INF, it should be similar, just import resource="bla.xml" and it should find it, although in J2EE you don't need to do this because in web.xml you can define several files in the contextConfigLocation context parameter inside web.xml, just separate them with spaces or newlines.