Freemarker template loading from different modules - java

I am trying to load Freemarker templates from multiple locations using Spring MVC. This project is developed using Intellij.
I have two maven projects:
my-website: the main maven project, contains customised templates at WEB-INF/ftl, example, WEB-INF/ftl/landing/login.ftl
generic-templates: that contains the generic freemarker templates at WEB-INF/ftl, example, WEB-INF/ftl/landing/login.ftl
The idea is for Freemarker, for example, to search for landing/login in 'my-website' and if it is not found then search it in 'generic-templates' landing/login.
According to the freemarker template loader documentation the freemarker.xml should look like:
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="generic-templates://WEB-INF/ftl/,/WEB-INF/ftl/" />
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
</bean>
but I am obviously doing something very wrong because it is not working: the files for generic-templates are not found.
Any idea how can I make this work ? (other approaches that accomplish the same goal are welcomed)
How can I debug it ?

The scheme part (the part before and including the :) is something that some integrating frameworks utilize via their custom TemplateLoader-s, while others don't. Interpreting the scheme is not done by FreeMarker itself. Furthermore, Spring has its own ideas about schemes in resource paths, which might interfere with this mechanism.
However one customizes things, the scheme shouldn't refer to a Maven module name, as Maven modules don't really exist on runtime. Instead, you should put the two group of templates under different directories under WEB-INF, and then list both paths in templateLoaderPaths, similarly as you did.

Related

${project.name} lost when using PropertyPlaceholderConfigurer

I'm using Maven to build a Spring (3.0.5.RELEASE) project. In my applicationContext.xml I use PropertyPlaceholderConfigurer (to load properties from the DB) like so:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="configurationConverter"/>
</bean>
The properties I want to access in the DB works fine, but ${project.name} and ${project.version} stops working. Where do these properties come from originally? And most importantly: how can I get them back?
If you use the Maven Resources plugin and set filtering=true, then you can replace Maven variables like 'project.version'. See https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html

Optional additional property file in spring context

I have a default properties file that my app uses but I also need to be able to allow for an additional property file which can be specified by a -D flag on startup and will be pointing to a path on the file system (not in my classpath). What's the correct syntax to do that?
I tried this but it didn't work:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"
value="classpath:/config/config.properties,file:/${additional.configs}" />
</bean>
as it complained that:
java.io.FileNotFoundException: class path resource [config/config.properties,file://var/tmp/cfg/qa.properties] cannot be opened because it does not exist
It looks like the comma delimination isn't working despite me finding some examples suggesting to do it that way. I don't think I can use the list of location beans version of it since the additional attribute file is optional. Any thoughts? I am using spring 3.1.1.
Thanks!
Update: Using the list approach works, however how to make it optional is still outstanding. For now I am using ignoreResourceNotFound=true, however that's not ideal because if someone mistypes the property then it won't fail...
I think you should be able to do this by using a wildcard on the property file you want to be optional. e.g.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/config/config.properties</value>
<value>file:/${additional.configs}*</value>
</list>
</property>
</bean>
Obviously you need to set the additional.configs parameter to something sensible. You can't leave it blank on systems that don't have the file, since the wildcard will then match all files! Instead you can set it to a dummy value for a non-existent file. e.g.
additional.config=/non-existent-file.txt
Spring doesn't throw an error if a wildcard doesn't match anything, so this has the affect of making this property file optional, without having to resort to ignoreResourceNotFound=true.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/config/config.properties</value>
<value>file:/${additional.configs}</value>
</list>
</property>
</bean>
If it still does not work, you can try to specify properties location without slash:
<value>file:${additional.configs}</value>

Springframework beans

I have not until now had worked with spring framework. I tried reading and reading the spring documentation but could not locate the answer to the following simple question.
The application is ant built to a build directory. So far when I tried to start the JBOSS (or apache) server, the logs says it is unable to build the bean because it could not find
var/config/madagascar.prop.
.
WRT the following snippet in WEB-INF/applicationContext.xml
<bean id="application.home" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.lang.System"/>
<property name="targetMethod" value="getProperty"/>
<property name="arguments">
<list>
<value>application.home</value>
</list>
</property>
</bean>
<bean id="propertyFile" class="java.io.File">
<constructor-arg ref="application.home"/>
<constructor-arg value="var/config/madagascar.prop"/>
</bean>
Could you tell me where {application.home} and var/config are expected to be located?
Is it relative to the Eclipse project home or relative to WEB-INF?
What is the bean id="application.home" attempting to do - is it trying to read the value for "application.home" from the system env?
"application.home" is the runtime value of System.getProperty("application.home") and the "propertyFile" bean is a java.io.File object returned from calling new java.io.File(String, String) with the whatever application.home is set to and that second String.
If the bean you wanted was 'propertyFile', the runtime equivalent would be:
File file = new File(System.getProperty("application.home"),"var/config/madagascar.prop");
That config is a very Spring 1.x (read old) way of doing things. XML is a very kludgy way to perform many of these types of initialization, and that's one of the reasons that Spring's #JavaConfig approach became so popular.

spring ide and ref hyperlink click doesn't work if bean is defined in different file

when I have a bean
<bean name="myBean" class="mypackage.myBean">
<property name="otherBean" ref="otherBeanRef" />
</bean>
and I click on otherBeanRef I'm redirected to definition of otherBeanRef, however this only works if its in the same file.
how to configure spring ide to also support other spring files?
You need to add both files to the Spring Spring Config File Set.
Spring Explorer/Properties/Config Sets

Elegant ways to separate configuration from WAR in Tomcat

I am trying to find the best way to pass complex configurations in a Spring webapp running in Tomcat. Currently I use JNDI to pass data sources and strings from the Tomcat context into the webapp, and this works well.
But, lets say I need to select the implementation of a notification service. There is no way that Spring can conditionally select which bean to instantiate (although in the past I have used a JNDI string to import a predefined configuration of beans by setting contextConfigLocation).
I've also seen many webapps which supply a configuration tool which will create a custom WAR file. In my opinion this is bad form, if for no other reason than it prevents the redeployment of WARs from upstream without many checks to ensure all the configuration has been re-applied.
Ideally I would be able to supply a Spring XML file which existed on the filesystem, outside of the webapp. But, the spring import directive does not seem to resolve ${} variables, making it impossible to supply customisations.
Are there any techniques I can employ here to properly separate complex configuration from the webapp?
If I have a specific set of beans that I'd like to configure, and this configuration must be separated from the WAR file, I usually do the following:
In applicationContext.xml:
<!-- here you have a configurer based on a *.properties file -->
<bean id="configurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file://${configDir}/configuration.properties"/>
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="false" />
<property name="searchSystemEnvironment" value="false" />
</bean>
<!-- this is how you can use configuration properties -->
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${smtp.host}"/>
</bean>
In configuration.properties:
smtp.host=smtp.your-isp.com
You also need to start Tomcat with -DconfigDir=/path/to/configuration/directory
If you are using Spring 3, you can take advantage of the Spring Expression Language. Let's say you have two applications app1.war and app2.war and they require a properties file named config.properties. The applications will be deployed with context paths /app1 and /app2.
Create two directories app1 and app2 in a common directory, eg. C:\myConfig\app1 and C:\myConfig\app2.
Put config.properties inside app1 and another config.properties inside app2.
Then create a file ${CATALINA_HOME}/conf/[enginename]/[hostname]/context.xml.default with the contents:
context.xml.default:
<Context>
<Parameter name="myConfigDirectory" value="C:/myConfig" override="false"/>
</Context>
The parameter myConfigDirectory will be available to all the applications on the host. It is better to create this parameter in context.xml.default rather than in server.xml, because the file can be changed later without restarting tomcat.
In the applicationContext.xml inside war you can access config.properties using the SpEL expression: "#{contextParameters.myConfigDirectory + servletContext.contextPath}/config.properties", so for example you can write:
applicationContext.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file:#{contextParameters.myConfigDirectory + servletContext.contextPath}/config.properties" />
</bean>
The expression will get expanded to C:/myConfig/app1 for application with contextPath /app1, and C:/myConfig/app2 for application with contextPath /app2. This will make the applications access the config.properties file based on their contextPath.
If you want to be fully portable between web containers you cannot rely on anything outside your WAR-file. In Tomcat the SecurityManager allows you to discover the physical location on disk where your code is deployed, and you can then use that knowledge to navigate the disk to a location where your configuration file is placed.
See e.g. Determine location of a java class loaded by Matlab

Categories