Spring Boot: Relocation of a configuration file at run time - java

Hy, I'm working with springboot and I have a problem.
I have a file myFile.yml that is loaded from the classpath: myApp/myFile.yml.
The problem is that there is another project, over which I have no control, reads the file from classpath:myFile.yml
The idea is to copy the file in classpath:myApp/myFile.yml so the external jar can also read it.
Is there any way to copy the file, while the beans are loaded, from /myApp/myFile.yml
to /myFile.yml?
Thank you in advance

Related

Supply properties file for jar file in Websphere

I have deployed a Spring Web based application in WAS. So WEB-INF/lib contains lot of jar files. One of the jar file trying to load properties file. Means that java file loading test.properties file by using getClassLoader().getResource().
How to supply that test.properties to that jar file ?
Please help me.
Thank you.

Edit Java Configuration File inside WAR in Tomcat

I compiled a Java application into a WAR file, there is a configuration file inside the WAR file. The configuration file is required to change something after deployed to the production server, because it still contains the UAT server parameters.
However, I don't know where to edit the configuration file after deployed in Tomcat. Please help. Thanks.
It doesn't sound like a correct design. You should -
Load configuration file based on some System parameter (e.g. -Denvironment=UAT or PROD). This will be the decision factor for loading the right configuration file.
Do not package the file inside war itself, if possible externalize it to some other directory where amending is lot easier.

META-INF/spring.factories file missing from Spring Boot application

I am fairly new to Spring and Spring Boot, and was asked to work on a legacy Spring Boot project. I am supposed to include in the project some FailureAnalyzers provided by Spring Boot. According to tutorials I came across (like here), all that needs to be done is registering the several FailureAnalyzer classes in the META-INF/spring.factories file.
But when I build the project (using Maven), I don't see a spring.factories file inside the target/META-INF directory. I tried adding one myself but it doesn't seem to be read by the project. What am I missing? What should I be doing to register these FailureAnalyzers?
In case you need it, the spring.factories file looks like this:
org.springframework.boot.diagnostics.FailureAnalyzer=\
org.springframework.boot.diagnostics.analyzer.AbstractInjectionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BeanNotOfRequiredTypeFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.BindFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ConnectorStartFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.NoUniqueBeanDefinitionFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.PortInUseFailureAnalyzer,\
org.springframework.boot.diagnostics.analyzer.ValidationExceptionFailureAnalyzer
I think you have mistaken the location of spring.factories file. This file will not be present in the project resources folder. Go to Maven dependencies and look into each Jar META-INF file and you will find spring.factories file in most of the jars.

netbeans and hibernate configuration

How can I load the configuration information for hibernate dynamically from a config file. Netbeans currently hard codes that information into an xml file that is then compiled into the jar. I'm a newbie to Java/Netbeans coming from PHP land and am use to a central bootstrap that pulls from a .ini or something similar, but netbeans tends to hardcode this information upon generation of the models,etc in an xml file that is then compiled in the jar. I'm looking for conventional methods of setting up configuration for various client machines using various database configurations. I don't want to have to compile the app on each machine it must be installed on.
The configuration file is read using the Configuration class. By default, it uses the hibernate.cfg.xml file found in the classpath, but you can use the configure method taking a file as parameter, and store the config file on the file system rather than in the jar.
You can also put the static mapping, which never changes between configs, in a file inside the jar, and put the varying config inside an external file. Look at the javadoc for Configuration to know how to add resources and config files to the configuration.

Java: Accessing properties file inside a war

I already searched StackOverflow for "properties inside war", but none of the results worked for my case.
I am using Eclipse Galileo and GlassFish v3 to develop a set of web services. I am using a "dynamic web project" with the following structure
Src
-java_code_pkg_1
-java_code_pkg_2
-com.company.config
--configfile.properties WebContent
-META-INF
-WEB-INF
--log4jProperties
--web.xml
--applicationContext.xml
--app-servlet.xml
I want to access the "configfile.properties" inside one of the source files in "java_code_pkg1". I am using the Spring Framework and this file will be instantiated once the application starts on the server.
I have tried the following with no luck
getResourceAsStream("/com.company.config/configfile.properties");
getResourceAsStream("/com/company/config/configfile.properties");
getResourceAsStream("com/company/config/configfile.properties");
getResourceAsStream("/configfile.properties");
getResourceAsStream("configfile.properties");
getResourceBundle(..) didn't work either.
Is it possible to access a file when it's not under the WEB-INF/classes path? if so then how?
Properties props = new Properties();
props.load(this.getClass().getResourceAsStream("/com/company/config/file.properties"));
works when I'm in debug mode. I can see the values in the debugger, but I get a NullPointerException right after executing the "props.load" line and before going into the light below it.
That's a different issue. At least now I know this is the way to access the config file.
Thank you for your help.
If you are in a war, your classpath "current directory" is "WEB-INF/classes". Simply go up two levels.
getResourceAsStream("../../com/company/config/configfile.properties");
It is horrible but it works. At least, it works under tomcat, jboss and geronimo and It works today.
P.S. Your directory structure is not very clear. Perhaps it is:
getResourceAsStream("../../com.company.config/configfile.properties");
Check the location of the properties file in WAR file.
If it is in WEB-INF/classes directory under com/company/config directory
getResourceAsStream("com/company/config/configfile.properties") should work
or getResourceAsStream(" This should work if the config file is not under WEB-INF/classes directoy
Also try using getClass().getClassLoader().getResourceAsStream.
Are you sure the file is being included in your war file? A lot of times, the war build process will filter out non .class files.
What is the path once it is deployed to the server? It's possible to use Scanner to manually read in the resource. From a java file within a package, creating a new File("../applications/") will get you a file pointed at {glassfish install}\domains\{domain name}\applications. Maybe you could alter that file path to direct you to where you need to go?
Since you are using Spring, then use the Resource support in Spring to inject the properties files directly.
see http://static.springsource.org/spring/docs/3.0.x/reference/resources.html
Even if the class that requires the properties file is not Spring managed, you can still get access to the ApplicationContext and use it to load the resource
resource would be something like, classpath:settings.properties, presuming that your properties file got picked up by your build and dropped in the war file.
You can also inject directly, from the docs:
<property name="template" value="classpath:some/resource/path/myTemplate.txt">

Categories