My requirement is similar to that of Reading reloadable xml file from external Tomcat directory
I need to place my hibernate xml and property file outside of the exploded war file ie, inside tomcat/my_folder
I am importing this xml and the property file through applicationcontext.xml using the below code.
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:D:/my_folder/jdbc.properties" />
</bean>
<!-- Load Hibernate related configuration -->
<import resource="file:D:/my_folder/hibernate-context.xml" />
The import of these files works fine but i when i update these files, the changes are not reflecting unless i restart Tomcat.
I also made the reloadabale property as true in tomcat server's
context.xml. Also i added entries for these files in
WatchedResource tag. But nothing works as this checks for the files only within the exploded war.
Please help me resolving this issue.
Related
I have a Spring Project where I am using bean configuration file
beans.xml.Inside the bean Configuration file, i have defined some properties for a PlaceHolder which refers to classPath...While the application is running, the properties are getting loaded from /unknownPath/Dev/Loc1/System.properties
Where
${BUS_ENV}=Dev
${LOCATION1}=Loc1
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
</list>
</property>
<!-- Force system properties to override any deployed runtime properties -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
I didn't specify classpath while running my project in IDE
I don't have those files in my resource folder
There are around 65 such files exists(for various reasons) as Dev/Loc1/System.properties
I am not able to find from which location the properties are getting referred. Even after debugging, I couldn't find out what classpath refers to. Please help me with figuring out
If you are using eclipse IDE right click on your project select properties then select Java Build Path. On first tab Source there is one input named Default Output folder that value is your classpath. Check all your properties files are there in that path.
Referring to your point 2 problems might be in these line
<value>classpath:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>classpath:${BUS_ENV}/lbsprocessor.properties</value>
You are using classpath for file location which means these properties file have to be in the .war file at /Dev/Loc1/System.properties
If properties files are outside of project may be at system level you can access them like this
<value>file:${BUS_ENV}/${LOCATION1}/system.properties</value>
<value>file:${BUS_ENV}/lbsprocessor.properties</value>
eg:
<value>file:/home/testuser/system.properties</value>
I am using Mac OS ,in this we are storing the Configurations as a jar file under
/Library/Java/Extension.So java is directly referring classpath to that location by default.
I have the following problem. I have a jar archive with a spring application inside (pure spring with xml configuration, without spring-boot). Someone decided to move some properties to external file. Unfortunately, the path is hardcoded in xml context file, so it looks like this:
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:hardcoded/path/props.properties</value>
</list>
</property>
</bean>
Both xml context file and property file are inside the archive. The application works fine, however, I need to change (override) some properties from the hardcoded file. Unfortunately, I can't modify or compile new jar. I tried adding files with overriden properties to the classpath (with the same name) and also passing some properties as jvm args, but it didn't work. How can I replace/override this file from classpath without modifying the original java archive?
I would take a look at the PropertyOverrideConfigurer.
Property resource configurer that overrides bean property values in an
application context definition. It pushes values from a properties
file into bean definitions.
That should allow you to "push" your own values on top of the prior-loaded set.
I currently have a file, {project-name}/WebContent/WEB-INF/applicationContext.xml, that sets a sub directory into a bean file for further use.
<bean ....>
<property ...>
<value>WEB-INF/utilities</value>
</property>
</bean>
Changes are being made to have these files normally located in this sub directory added to the Java resource folder when the war is build (done with the pom file). I have tried setting the bean to use this path, but failing when trying to access files inside of it.
<bean ....>
<property ...>
<value>{project-name}/Java Resources/src</value>
</property>
</bean>
Is there a better option to reference the project path so applicationContext.xml can have visibility to the source folder?
Can the pom declare an alternate destination for resource files when building? If I can have the pom set the files to "WEB-INF/utilities" instead of defaulting to "Java Resources/src" that should be able to resolve my issue.
I have read questions regarding accessing files from the java resource directory, but those solutions require implementing java classes to read values from the included files; not setting the actual path location in a bean for use by other beans. Not sure the benefit of having a new class just to retrieve a directory name.
I have found many posts on here about using Spring to externalize the Hibernate settings so it's not in the hibernate.cfg.xml config file. However, my question revolves around having a development environment that uses Eclipse and then deploying the generated WAR file out on a separate server.
I am currently using Eclipse Luna and deploying my Spring MVC web application to a local install of Tomcat 8. The hibernate.cfg.xml file currently resides in the resources folder. When I deploy it locally, it works great. However, the config uses my personal login credentials, which isn't good to have on the server for everyone to see.
What I need is to figure out a design to have the application work locally and when I deploy on the server.
I was thinking about creating a folder called conf and placing the hibernate.cfg.xml file in here and through the Eclipse classpath (Debug As configuration...) define it as a folder. That should allow the local version to still read in the proper files and work. But I am stuck on how to setup the server side of this. Since we just drop in the WAR file, it will auto deploy. I can use a PropertyPlaceholderConfigurer, but that will throw errors on my local copy.
The only other thing I can think of is to create a process where I create the folder for the application, unpack the WAR file into this folder each time minus the conf folder. And only update the conf folder on the server.
Is this a good design or is there a better way? I appreciate all the help and guidance on this!
I got this to work and figured I would share how I did it
I went ahead and created the "conf" folder in the root folder. In here I placed a hibernate.cfg.xml file, log4j.properties and messages.properties file. I then created a subfolder called "db" and placed db.properties file in there.
So the structure is:
ApplicationName
--> conf
--> db
db.properties
hibernate.cfg.xml
log4j.properties
messages.properties
db.properties
In this file, I placed the hibernate connection information (and other information)
db.driver=oracle.jdbc.driver.OracleDriver
db.dialect=org.hibernate.dialect.Oracle10gDialect
db.default_schema=<schema>
hibernate.session_context=thread
hibernate.max_fetch_depth=20
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.use_sql_comments=false
hibernate.generate_statistics=false
hibernate.c3p0.min_size=1
hibernate.c3p0.max_size=20
hibernate.c3p0.timeout=3000
hibernate.c3p0.max_statements=50
hibernate.c3p0.idle_test_period=300
hibernate.c3p0.preferredTestQuery=SELECT 1 from DUAL
# DEV
db.url=jdbc:oracle:thin:#<url>
# Need to be changed
db.username=<username>
db.password=<password>
The hibernate.cfg.xml file just had the Entity mappings in it
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Entity mapping -->
<mapping class="com.mine.Entity1"></mapping>
<mapping class="com.mine.Entity2"></mapping>
</session-factory>
</hibernate-configuration>
In the HibernateUtil.java file, I added the following lines:
Configuration configuration = new Configuration();
configuration.configure();
PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle("db/db");
// Basic connection information
configuration.setProperty("hibernate.connection.username", prop.getString("db.username"));
configuration.setProperty("hibernate.connection.password", prop.getString("db.password"));
configuration.setProperty("hibernate.connection.url", prop.getString("db.url"));
configuration.setProperty("hibernate.connection.driver_class", prop.getString("db.driver"));
configuration.setProperty("hibernate.dialect", prop.getString("db.dialect"));
configuration.setProperty("hibernate.default_schema", prop.getString("db.default_schema"));
configuration.setProperty("hibernate.current_session_context_class", prop.getString("hibernate.session_context"));
// Handling SQL statements in the logs
configuration.setProperty("show_sql", prop.getString("hibernate.show_sql"));
configuration.setProperty("format_sql", prop.getString("hibernate.format_sql"));
configuration.setProperty("use_sql_comments", prop.getString("hibernate.use_sql_comments"));
// C3P0 Settings
configuration.setProperty("hibernate.c3p0.min_size", prop.getString("hibernate.c3p0.min_size"));
configuration.setProperty("hibernate.c3p0.max_size", prop.getString("hibernate.c3p0.max_size"));
configuration.setProperty("hibernate.c3p0.timeout", prop.getString("hibernate.c3p0.timeout"));
configuration.setProperty("hibernate.c3p0.max_statements", prop.getString("hibernate.c3p0.max_statements"));
configuration.setProperty("hibernate.c3p0.idle_test_period", prop.getString("hibernate.c3p0.idle_test_period"));
configuration.setProperty("hibernate.c3p0.preferredTestQuery", prop.getString("hibernate.c3p0.preferredTestQuery"));
In the *-servlet.xml file:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db/db.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false"/>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:messages" />
<property name="cacheSeconds" value="60"/>
</bean>
In Eclipse, when I go to run the application, I must first go to "Debug As... => Debug Configuration...". Under Classpath, I add the conf folder under the user entries section.
That's it for Eclipse. When it runs, it will pull in the configs from the conf folder and work properly.
TOMCAT
To have this run on the server, I first created a folder to store the contents of conf. I created the folder /app/properties. I checked out the project from SVN and copied the contents of the conf folder into the newly created folder.
Lastly, I went into $TOMCAT_HOME/conf/catalina.properties and added the new folder into the classpath by adding it to the shared.loader property.
That's it! When the WAR is deployed, it pulls the information from the /app/properties location and the application works.
Any more information needed, please let me know!
Here is a way to do that. First you need to use JNDI to access the datasource. There are some examples how to that and you can probably find more.
One of the thing that need to be done is to configure datasource as resource in tomcat context. And the trick is to have different context configurations for your local deployment from eclipse and on production.
In local deployment pack context.xml as part of the war in META-INF folder but in production deployment create context.xml in $CATALINA_BASE/conf/[enginename]/[hostname]/ which would override context.xml supplied in war.
Another option is to use PropertyPlaceholderConfigurer with option to override settings from environment variables. This will require that you specify database credentials in environment variables in tomcat server launch configuration.
As far as I have found, Maven cannot change the current working directory for every module build in a reactor build (to the module that is being built) because it is not possible in standard Java to change the current working directory. First of all, is this correct?
If yes, how can I make the following scenario work?
UPDATE: I have a some.properties file that is supposed to be on the classpath of the module being built (e.g. in src/main/resources) and it is being referred to in a Spring context file of a Maven Plugin, like this:
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<array>
<value>classpath:some.properties</value>
</array>
</property>
</bean>
The Maven Plugin is being executed in "process-test-resources" phase. Also, some.properties file has a property that refers to an external relative configurable path:
some.spring.config.dir=target/test-classes
Then, a few config files from this path are loaded in spring context files in the module being built, such as:
<property name="configLocation" value="file:${some.spring.config.dir}/cache.xml" />
This scenario works when I build the project from its pom.xml directory but not when I build it as part of a reactor build from a top level directory. Is there a way to get it working when running maven from any directory and also keep the path in my properties file (e.g. "target/test-classes") relative?
Two steps required:
Include resource filtering for configuration file: http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
in configuration file add basedir:
some.spring.config.dir=${basedir}/target/test-classes
In "target" directory "some.spring.config.dir" will be filled with full path.