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
Related
Since my project has multiple environments and multiple small project groups, I need to handle the corresponding business logic according to different property names, but I can't find any parameter in bootstrap.yml that can set the custom configuration file path.
After I googled, I only found a way to modify the path of the custom configuration file through the startup class
Is there any other better way for me to configure it? Please help me!
Spring Boot look for your externalized configuration file in four predetermined
locations :
in classpath root,
in the package /config in classpath,
in the current directory
in /config subdirectory of the current directory.
i think you want to load configuration file from /config folder.
You can programmatically tell Spring Boot to load your configuration files from
custom location as below :
ConfigurableApplicationContext applicationContext = new SpringApplicationBuilder(Application.class)
.properties("spring.config.name:application,conf",
"spring.config.location:classpath:/your/location/of/config/folder,classpath:/another/location/of/congig/folder")
.build().run(args);
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.
Now I want to load applicationContext.xml by ClassPathXmlApplicationContext, my working folder structure is src/main/java; src/main/resources/applicationContext.xml;
My code is
ApplicationContext ctx =
new ClassPathXmlApplicationContext("src/main/resources/applicationContext.xml");
Throw cannot be opened because it does not exist,but
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
is ok, Why is it happen?
ClassPathXmlApplicationContext will look for specific file from the classpath. From the Java Build Path -> Source section, you can see the source folders: src/main/java, src/main/resources, src/test/java. They will be used as classpath by the java applicaiton.
So if you pass applicationContext.xml as parameter, jvm will look for this file from the above 3 folders. src/main/resources/beanConfigurationName.xml will lead jvm to look for "src/main/resources/beanConfigurationName.xml" from classpath.
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".
For some reason (a shiro filter) I saved my application context file in WEB-INF folder. Everything works when I run tomcat but, when I try to get an application context from a controller using :
context = new ClassPathXmlApplicationContext(fileContext);
I receive always this exception:
IOException parsing XML document from class path resource [applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [applicationContext.xml] cannot be opened because it does not exist
It seems that under ecplise I'm not able to include WEB-INF under classpath. I took a look to a lot questions here in stackoverflow but I didn't find yet a solution.
If I move the applicationContext.xml file under src/main/java folder, I'm able to get the context but, the shiro filder defined into web.xml file is not able to see the shiro bean defined under applicationContext file (I double checked and the bean is correctly worked). How can I tell to web.xml to get content from src/main/java? Or, how can I reach the applicationContext.xml
WEB-INF is not in your CLASSPATH. WEB-INF/classes is. So why dont you put it in a source folder and package the application?
Do not create an instance of ApplicationContext in your controller. The spring DispatcherServlet already creates one for you. All you need to do is access all bean declarations in you application context file using #Autowired.
use
context = new FileSystemXmlApplicationContext(fileContext);
instead of
context = new ClassPathXmlApplicationContext(fileContext);
Problem has been solved moving all configuration file under WEB-INF/classes and adding the prefix classpath:
<import resource="classpath:spring-data.xml"/>
thanks to all for the help! I really appreciate that!
cheers, Andrea