Spring placeholder format - spring

This is syntax which is working
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:application.properties"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
This is alternative format which is NOT working
<context:property-placeholder location="classpath:application.properties" system-properties-mode="OVERRIDE"/>
Any ideas why? I will always crach on tests while building.
Application properties file contains just this
hibernate.show.sql = false
hibernate.format.sql = true

Your syntax looks correct. Do you have the context namespace registered in your context file?
For example:
<beans ...
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="...
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

Related

Read spring yml properties from xml configuration

I am trying to read properties from application.yml in spring-bean.xml like this:
<bean name="#{bean.name}" />
Is it possible ? or I should specify location of my application.yml file?
Yes It's Possible
For YAML Properties
You have to use YamlPropertiesFactoryBean
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
Then define your property in src/main/resource/application.yaml
bean:
name: foo
Now use can use the property in xml to create a bean
<bean name="${bean.name}"
class="net.asifhossain.springmvcxml.web.FooBar"/>
Here's my complete XML config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
<property name="resources" value="classpath:application.yaml"/>
</bean>
<context:property-placeholder properties-ref="yamlProperties"/>
<bean name="${bean.name}" class="net.asifhossain.springmvcxml.web.FooBar"/>
</beans>

Access a property file outside war- Spring 2.5.1 version

I need to access a property file outside war. I am keeping path to the outside property
file in a class path property file. The Project version is Spring 2.5.1 and
#Configuration and Environemnt classes are not available unfortunately.
System.properties is in the class path it has key entry to the outside property.
key.externalFile=C:\temp\external.properties
I tried with below approach
<context:property-placeholder location="classpath:System.properties"/>
<context:property-placeholder location="file:${key.externalFile}" />
In the external.properties file it has id
keyValue= 444;
I need to inject this value to a bean as below.
<bean id="helloWorldBean" class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${keyValue} />
I am getting the error Could not resolve placeholder 'keyValue' in string value "${keyValue}"
I also tried out with
<context:property-placeholder location="file:${key.supportiveFile}" />
<util:properties id="props" location="file:${key.supportiveFile}"/>
but it ended with the same results.
<bean id="helloWorldBean"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${props.keypath}" />
</bean>
Please help me and looking forward.
Complete Code
1. Context file
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:property-placeholder location="classpath:constants.properties"/>
<context:property-placeholder location="file:${key.supportiveFile}"/>
<bean id="helloWorldBean"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="key" value="${keypath}" />
</bean>
</beans>
2. System property file - constants.properties
key.supportiveFile=C\:\\Users\\Kasun\\AppData\\Local\\Temp\\key.properties
3. Test Class
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext("applicationContext.xml");
HelloWorld hello = (HelloWorld) context.getBean("helloWorldBean");
hello.sayHello();
}
Thanks. Unfortunately it still cannot resolve. Exception in thread "main" org.springframework.beans.factory.BeanInitializationException: Could not load properties; nested exception is java.io.FileNotFoundException: ${key.externalFile} (The system cannot find the file specified)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:78)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:542)
at org.springframework.context.support.AbstractApplicationContexeBeanFactoryPostProcessors(AbstractApplicationContext.java:516)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:348)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:123)
at org.springframework.context.support.ClassPathXmlApplicationContext.t.invok
Remove the context attribute definitions of propertyPlaceholderConfigurer and replace them with:
<bean id="placeholderConfig1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:System.properties</value>
</property>
<property name="ignoreUnresolvablePlaceholders">
<value>true</value>
</property>
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
</bean>
<bean id="placeholderConfig2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:${key.externalFile}</value>
</property>
</bean>

how to make spring-mvc application multi-language?

I am working on web service with spring-mvc 4, I have to make it multilanguage. Like that: if coming url request contains '..?lang=tr' for turkish and '..?lang=en' for english. I read on stackoverflow that <mvc:annotation-driven /> override LocaleChangeInterceptor. But when I remove that, app is not working.And I was using this tutorial. I didnt find a solution for that yet. For configuration below, messages are always in english even if I switch language. Furthermore I need to get lang messages in java instead of jsp also. But it always return default message there in java class when I syso. I messed up here. and asking for your help.
#Autowired
private MessageSource messageSource;
Locale locale = LocaleContextHolder.getLocale();
String msg = messageSource.getMessage("deneme", null, "Deault Message!", locale);
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd">
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
</mvc:interceptors>
<bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">
<property name="basename" value="classpath:/messages"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" id="localeChangeInterceptor">
<property name="paramName" value="lang"></property>
</bean>
<bean class="org.springframework.web.servlet.i18n.CookieLocaleResolver" id="localeResolver">
<property name="defaultLocale" value="en"></property>
</bean>
<!-- Defining which view resolver to use -->
<bean class= "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
add this to your xml to register your localChangeInterceptor and it will work
<mvc:annotation-driven/>
<context:component-scan
base-package="controller">
</context:component-scan>
<bean id="handlerMapping"
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
</bean>

Spring applicationContext configuration for scheduled methods

I'm working on a Spring Roo web app, and need to use a Scheduled method that runs in background every certain time, then I added some
lines to my applicationContext.xml, and that method ran ok, but then
occured that the app wasn't able to set up/read the
transactionManager, the console says The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven'.
I already searched, and found in this site, 2 or 3 answers to this error message, such aS:
add xmlns:tx="http://www.springframework.org/schema/tx to the
header [already done]
add component:scan before tx:annotation [already done] but when I try to run my app on the server, it throws
a HTTP Status 404 The Requested Resource is not available, could you
help me to fix the xml file?
this is my applicationContext.xml file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx">
<context:component-scan base-package="org.webapp.services.tasks">
</context:component-scan>
<task:annotation-driven />
<bean id="scheduledMethod" class="com.myhome.myproject.web.ScheduledJobsController">
</bean>
<context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>
<context:spring-configured/>
<context:component-scan base-package="com.myhome.myproject">
<context:exclude-filter expression=".*_Roo_.*" type="regex"/>
<context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
</context:component-scan>
<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
...
</bean>
<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<context:component-scan base-package="com.myhome.myproject"></context:component-scan>
<tx:annotation-driven mode="aspectj" transaction-manager="transactionManager"/>
<bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory">
<property name="persistenceUnitName" value="persistenceUnit"/>
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
You declared the usage of the tx namespace by doing
xmlns:tx="http://www.springframework.org/schema/tx"
But you forgot to declare the location of the xsd for this namespace in the xsi:schemaLocation attribute:
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd

spring multiple property file

I have a spring project. There are two property files. one property file is config in dbConfig.xml and I cannot change it. I have my own appConfig.xml
I have the following in it
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="${configProps.srcDir}"/>
</bean>
using either srcDir or configProps.srcDir does not seem to work.
${} works only for property files loaded by the context:propertyplaceholder. If you are using spring 3.0 you can use #{} which is processed as a Spel (spring expression language). The following should work.
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="#{configProps.srcDir}"/>
</bean>
Here how we resolved it
Context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="Settings.xml"/>
<import resource="Database.xml"/>
</beans>
Settings.xml
In the list many property file could be added
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
</beans>
Sample bean for processing db.properties file
Database.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- the transaction manager -->
<bean id="dstxnManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="appDataSource"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="appDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- DB connection factory -->
<bean id="storageDaoFactory" class="com.dao.StorageDAOFactory">
<constructor-arg><ref bean="dstxnManager"/></constructor-arg>
</bean>
</beans>

Resources