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
Related
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.
I have this structure in my Jboss 6.4, and I need to access outside of the war to another folder to take the configuration for Hibernate:
/usr/webapps/MYAPP/deployments/MYAPP.war
/usr/webapps/MYAPP/appconfig/hibernate.properties
I have to use this pattern, and I'm using Hibernate 4.2, in my case I need to take the content of the properties file to use it in my hibernate.cfg.xml like here:
<property name="connection.driver_class">${db.driver}</property>
<property name="hibernate.dialect">${db.dialect}</property>
<property name="hibernate.connection.url">${db.url}</property>
<property name="hibernate.connection.username">${db.username}</property>
<property name="hibernate.connection.password">${db.password}</property>
hibernate.properties:
db.username=usernameTest
db.password=passwordTest
db.driver=driverTest
db.url=urlTest
db.dialect=dialectTest
What should be the best practice to do this, I was watching some cases with Spring here, but in my case it's only with JEE
Before post this Question, I google to get Properties from Spring project(Its NOT web-based project). I am confused as every one are talking about application-context.xml and have configuration like
However, I am working on normal Java Project with Spring(NO Web-app and stuff like that). But I would like to get some common properties from properties file and that needs to be used in JAVA file. How can achieve this by using Spring/Spring Annotations?
Where I should configure myprops.properties file under my project and how to invoke through spring?
My understanding is application-context.xml is used ONLY for web based projects. If not, how should I configure this application-context.xml as I do NOT have web.xml to define the application-context.xml
You can create an XML based application context like:
ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/appContext.xml");
if the xml file is located on your class path. Alternatively, you can use a file on the file system:
ApplicationContext ctx = new FileSystemXmlApplicationContext("conf/appContext.xml");
More information is available in the Spring reference docs. You should also register a shutdown hook to ensure graceful shutdown:
ctx.registerShutdownHook();
Next, you can use the PropertyPlaceHolderConfigurer to extract the properties from a '.properties' file and inject them into your beans:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
Lastly, if you prefer annotation based config, you can use the #Value annotation to inject properties into you beans:
#Component
public class SomeBean {
#Value("${jdbc.url}")
private String jdbcUrl;
}
As of Spring 4, you can use the #PropertySource annotation in a Spring #Configuration class:
#Configuration
#PropertySource("application.properties")
public class ApplicationConfig {
// more config ...
}
If you would like to have your config outside of your classpath, you can use the file: prefix:
#PropertySource("file:/path/to/application.properties")
Alternatively, you can use an environmental variable to define the file
#PropertySource("file:${APP_PROPERTIES}")
Where APP_PROPERTIES is an environmental variable that has the value of the location of the property file, e.g. /path/to/application.properties.
Please read my blog post Spring #PropertySource for more information about #PropertySource, its usage, how property values can be overridden and how optional property sources can be specified.
You don't have to use Spring.
You can read with plain java like this:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
Can you figure out how your project will be used in the whole app? If your project is used as a build path for a web app and the configuration in your project is achieved through spring annotations, so no doubt you are puzzled about how to add an application.xml file. My suggest is you have to announce the guys who will use your project, tell them what you need and you just need to add #Value("${valuename}") in your code.
Create new property file inside your src/main/resources/ directory and file extension must be .properties e.g. db.properties
Write following context properties in your spring xml configuration file:
<context:property-placeholder location="db.properties"/>
Usage: ${property-key}
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
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