I am using Spring and Mybatis in my project . Project can run in any platform like SQL Server Oracle etc.
I am facing 1 Problem i want to access variable value From properties file,application Context file to Mybatis Mapper file.
For.eg :
ApplicationContext.xml - Spring file
config.properties file
in above file want to decalare variable lets say pName = XYZ
i want to access this pName in Mybatis Mapper XML file.
<select id="getValue" parameterType="java.lang.String" >
${pName}
</select>
How is it possible ?if have any other solution most welcome.
To access spring way :
Use
<util:properties id="myPropertyConfigurer" location="classpath:yourpropertiesfile.properties"/>
<context:property-placeholder properties-ref="myPropertyConfigurer" order="1" ignore-unresolvable="true" />
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="applicationDataSource" />
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
<property name="configurationProperties" ref="myPropertyConfigurer"></property>
</bean>
In your mapper xml file :
<select id="searchSomeOne" parameterType="map" .....>
SELECT
${pName} AS module
FROM MY_TABLE
WHERE
COL_ONE = #{moduleName} and
COL_TWO like #{username}
</select>
and define pName=MODULE in the yourpropertiesfile.properties
You can do in this way -
1. Add the below line of code in your mybatis-config.xml file
<properties resource="path/to/config.properties" />
Then you should be able access all the keys as ${keyName} in the mapper file .
Reference from the mybatis docs - http://mybatis.org/mybatis-3/configuration.html#properties
Related
Currently in my data context XML file, it's looking up values to substitute from a application.properties file:
<bean id="applicationProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" lazy-init="default">
<property name="location" value="classpath:application.properties" />
</bean>
<bean id="appleDataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="url" value="${apple.url}" />
</bean>
I'd like to change this from being looked up from the application.properties file, to being read out of a Properties/Map object.
This is because the configuration could come from more than one place (i.e. not just the classpath), and this logic is already implemented in my application.
I've seen MapPropertySource but I think while that can be done when the bean is configured in Java, I'm not sure this logic can be implemented when working with the XML file alone?
I am training to integrate the Struts2 and Spring and Hibernate.I using a properties file to set the dataSource:
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
And this is the db.properties following:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/sp3
jdbc.username=root
jdbc.password=123456
I find the example about this in spring-reference,but I just don't know why I must use ${jdbc.XXXXX} but not ${XXXXX}.I try to write "username=root","password=123456",and then it cause "Access denied for user 'Administrator'#'localhost' (using password: YES)"
If using the Expression Language principle:${jdbc.username} meanings "getJdbc().getUsername();"because in the struts-tag,${model} means getModel(),is it right?
I find the source about PropertyPlaceholderConfigurer and ComboPooledDataSource ,but I can not find any code about getJdbc();
Thank you for your help.
I don't understand what you're asking. jdbc.username is just a text, or a key if you want. You could have used "BLABLABLA=root" in db.properties and in your xml <property name="user" value="${BLABLABLA}" />.
Probably you are confusing PropertyPlaceholderConfigurer with PropertyOverrideConfigurer.
If you had the problems with your properties, you would have got an exception something like->
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'XYZ' in string value "${XYZ}".
What do you mean by 'everything is ok'? Were you able to access DB with previous setup?
The exception you are getting seems related to username and password combination OR you don;t have proper rights/GRANTS.
jdbc.XXXXX is just a key in the property file. You could have used anything in place of it.When you perform ${something} it would just pick the value for key 'something' from the property file and use it for populating the properties of bean.
For example :
<property name="jdbcUrl" value="${jdbc.url}" /> is just setting the value for a field named 'jdbcUrl' in class ComboPooledDataSource.
I've a properties file like this :
firstproperty=1,2,3,4
secondproperty=47,998,120
thirdproperty=54
My properties file is well defined in my Spring configuration as property for my PropertyPlaceHolderConfigurer bean.
I want to load values in a
HashMap<String, ArrayList<String>>
like this :
<util:map id="properties" map-class="java.util.HashMap">
<entry key="first" value="${firstproperty}" />
<entry key="second" value="${secondproperty}" />
<entry key="three" value="${thirdproperty}" />
</util:map>
The problem is that for each entry, multiple values separated by commas count as one value. I tried to configure value-type of my util-map to an ArrayList but it was unsuccessful. Any idea ?
P.S : I use Spring 3.2.
I searched Spring EL in config file, maybe this is what you want:
<bean id="taxCalculator" class="org.spring.samples.TaxCalculator">
<property name="defaultLocale" value="#{ systemProperties['user.region'] }.split(',')"/>
<!-- other properties -->
I am not sure about the location of split method, you can try yourself to find the correct way. For more details, please refer:http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html
I have a standalone jar that uses spring. The config in my spring xml uses placeholders of which I've been replacing when compiling with maven. Example spring config:
<bean id="foo" class="package.Foo">
<property name="host" value="${db.host}" />
</bean>
Instead of replacing ${db.host} using maven I'd like to pass in a properties file at runtime, e.g.
java -jar Application.jar productionDB.properties
This would allow me to switch the db host at runtime by passing in the production db properties file or the testing db properties file.
Is it possible to do this or are there any better ways of achieving the same goal?
You could specify your property file as a System Property, e.g.:
java -jar Application.jar -DappConfig=/path/to/productionDB.properties
Then you should be able to reference that in your application context:
<context:property-placeholder location="file:${appConfig}"/>
<bean id="foo" class="package.Foo">
<property name="host" value="${db.host}" />
</bean>
You could use a PropertyPlaceholderConfigurer to use a .properties file to pass in the required variables.
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:productionDB.properties</value>
</list>
</property>
</bean>
You can leave your bean declaration as is. The properties will be automatically taken from the productionDB.properties file.
There are a few options:
Set your resources via your container's JNDI and use Spring's <jee:jndi-lookup/>.
Use Spring's <context:property-placeholder location="classpath:/myProps.properties" />. I prefer this short-hand over the "full" bean definition because Spring will automatically use the correct implementation (PropertyPlaceholderConfigurer for Spring < 3.1, or PropertySourcesPlaceholderConfigurer for Spring 3.1+). Using this configuration, you would just drop the myProps.properties at the root of your classpath (${TOMCAT_HOME}/lib for example).
You can pass the values using the context:property-placeholder. So your setup would be something like:
<context:property-placeholder location="file://opt/db.properties"/>
Then when you are wiring up your Foo service, you can use the property names in your config, such as
<bean id="foo" class="package.Foo">
<property name="host" value="${db.host}" />
</bean>
Then just use the different set of files for each environmnet
See the spring docs for more details.
There is this tag <terracottaConfig url="host1:9510,host2:9510,host3:9510"/> in ehcache.xml file inside spring web application. I want to externalize url attribute of this tag. Value of URL should be replaced by a property from external file. It will be very helpful if you suggest any solution to this problem.
You can put something like this -
<terracottaConfig url="${terracotta.config.location}" /> , however the big catch is that this will be loaded only from the system properties. It is not resolved from PropertyPlaceHolder as it is not a Spring configuration file.
So if you want to use an external config file, you will basically have to programatically set this system property before the Spring Application starts loading up the ehcache.xml file - one way to do that will be write your custom ServletContextListener to load up your properties file and set the system property based on that, this way when the ehcache.xml is loaded up it would be able to resolve the place holder properly.
Your answer helped me to solve my problem. I just want to add that instead of setting system property through program, I am using util:properties as follows
<bean id="sysProperties" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" value="#{#systemProperties}"/>
<property name="targetMethod" value="putAll"/>
<property name="arguments">
<util:properties>
<prop key="propertyname_used_in_ecache_xml">#{proerties_defined_using_property_factory['propertyname_defined_in_external_properties_file']}</prop>
</util:properties>
</property>
</bean>
<bean id="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" depends-on="sysProperties">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>