I am writing web app using spring. Which is created using standard web application.
I need ApplicationContext to be initialized using ClassPathXmlApplicationContext so i placed context.xml file in WEB-INF folder and created ApplicationContextusing following code.
ApplicationContext context = new ClassPathXmlApplicationContext("context.xml");
But I get FileNotFoundException saying context.xml is not found.
When i tried to initilize application using FileSystemXmlApplicationContext it works.
But i want ClassPathXmlApplicationContext
what can i do so that context.xml is located in classpath.
You need to place your context.xml inside classes folder or you can use
new ClassPathXmlApplicationContext("classpath:context.xml")
Read the below definition
ClassPathXmlApplicationContext will read files from your classpath. They must be in classes folder of your web application or in a jar in your libfolder.
FileSystemXmlApplicationContext can access all your file system, for example c:/config/applicationContext.xml.
XmlWebApplicationContext certainly can access to files contained in your web application.
Related
I have a war based spring web application project which internally has multiple jar files. I am using maven setup to build jars and war file. Each jar file has a set of beans that needs to be loaded and i am not able to do so.
In each of the jar file i have defined a beans.xml file . But the beans are not getting loaded automatically. I have tried loading the beans.xml file from:
a) src/main/resources
b) src/main/resources/META-INF
c) src/main/resources/META-INF/spring
It doesnt work.
My Question: How to prepare the application context for such scenarios? War based app with multiple jars.
If your are packaging your application as a webapp one, then you can simply add a file named yourservletname-servlet.xml and include all resources from your jar files using the <import /> element.
Spring, behind the scenes, will scan the file mentioned above by default including all beans declared in the files imported.
Here is how your servletname-servlet.xml should look like (xml namespace and schemas declaration are ommited for brevity sake):
<beans>
<import resource="classpath:/META-INF/beans.xml"/>
</beans>
I suggest the use of the META-INF as your context config files location.
This will scan all bean declaration files named beans.xml under META-INF folder under the root of your classpath, which assumes that those files must be under src/main/resources/META-INF/ in your project structure when using Maven as your build tool (so they can get copied directely under jar_root_path/META-INF/).
Otherwise, if you are not using the default -servlet.xml file, you can specify a custom application context descriptor using the contextConfigLocation as follows:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>application-context.xml</param-value>
</context-param>
Try simple
import resource="classpath*:/META-INF/beans.xml"/
Where each jar contains beans.xml file under META-INF folder.It will scan each jar and load beans.xml file and creates beans based on these XMLs files.
You mention beans.xml, is this a CDI project or a standard Spring project ?
Using maven, everything under src/main/resources gets packaged at the top level of your JAR. So if you had a file in src/main/resources/META-INF/beans.xml, then you should load it using "/META-INF/beans.xml" or define it in your spring context as "classpath:/META-INF/beans.xml".
How to get the full path of file located in /src/reportpackage/daysheet.jasper in the spring mvc application?. How to get the string format of the path?
You need to understand that after the build your file would end up in classpath and what you would be loading through Spring resource loading mechanism is a classpath resource. You can use spring notation for it like this
appContext.getResource("classpath:reportpackage/daysheet.jasper");
where appContext is the spring application context which can be autowired in any Spring bean
How do I reference an external beans.xml file based on its relative location to where my jar file is deployed? This is what I have now, which doesn't work:
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("../beans.xml");
I want it out of the classpath so I can change things without redeploying, but relative to the deployed location of the jar so that it will work no matter where I put the jar and the beans.xml in the file system, as long as they are correctly situated relative to each other.
This is Spring btw, if that helps.
Thanks!
An application context file must be on the classpath to allow ClassPathXmlApplicationContext to work. To use a relative path you would have to use FileSystemXmlApplicationContext.
ApplicationContext context = new FileSystemXmlApplicationContext("../beans.xml");
You can import the external beans configuration file in your spring-config.xml you can import the extends
<beans ...>
<import resource="file:/path/to/external/config.xml"/>
</beans>
The above Spring config will import /path/to/external/config.xml. Having an external file will allow you to configure beans without having to rebuilding your main war/jar.
In your Java code, you can load it as follows:
ApplicationContext context = new ClassPathXmlApplicationContext("config.xml");
and the config.xml file should be in:
project-classpath/config.xml
More about loading external configuration files, you can find here
I want to use spring for a non-web Java project. Where should the context file go? How do I identify their locations to the Spring container?
I'm sure there are various choices, but with a web application it is typical to put things in WEB-INF, and to declare the locations in the web.xml. How is this done in a stand-alone app?
location : src/main/resources/
then you can be precise by adding directories like moduleName/spring-application.xml
Put it in a resources folder. And use ClassPathXmlApplicationContext to load them. Use packages as your java classes use. E.g. src/main/resources/com/myapp/somemodule/context.xml.
I have a Java web application. Inside the WAR I have a folder containing configuration files for the application. I need to know the path of the folder in order to load the files at runtime.
I also need the solution to work in Tomcat and in WebSphere.
Thanks.
I would suggest placing the files under WEB-INF/classes and simplying loading them from the classpath, not from the filesystem. This way, the path is always the same.
You can use something like:
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("path");