I have a simple bean tag in the bean xml file as shown below. This is just a dummy values
<bean id="myBeanId" class="myBeanClass">
<property name="myProperty" value=${myPassword} />
</bean>
<bean id ="myOtherBeanId" class="myOtherBeanClass">
<property name="myOtherProperty" ref="myBeanId">
</bean>
myPassword is a variable names stored in a separate properties file. Now, I instead of storing the direct value of myPassword from the properties file, I will have encrypted string in the property file and I want to call my custom written Decrypt method on myPassword property instead. something like this.
<bean id="myBeanId" class="myBeanClass">
<property name="myProperty" value=com.xxx.Security.Decrypt(${myPassword}) />
</bean>
How can I do this?
Use the MethodInvokingFactoryBean if you want to invoke another bean's method and use the returned object as a bean.
<bean id="securityBean" class="com.xxx.Security">
</bean>
<bean id="myBeanId" class="myBeanClass">
<property name="myProperty">
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="securityBean"/></property>
<property name="targetMethod"><value>Decrypt</value></property>
<property name="arguments">
<list>
<value>${myPassword}</value>
</list>
</property>
</bean>
</property>
</bean>
How about Using with jaspyt,
Properties file entry
password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
Bean Entry
<bean id="myBeanId" class="myBeanClass">
<property name="myProperty" value=${password} />
</bean>
Source : http://www.jasypt.org/spring31.html
Related
Consider I have something like this in beans.xml:
<bean id="emails" class="org.some.package.SomeClass">
<property name="emailList">
<list>
<value>pechorin#hero.org</value>
<value>raskolnikov#slums.org</value>
<value>stavrogin#gov.org</value>
<value>porfiry#gov.org</value>
</list>
</property>
</bean>
But I need to add emailList property into multiple beans. How can I do that without writing property to each bean? Can externalize property and inject it into each bean?
I expect something like:
<property name="commonProp">
<list>
<value>pechorin#hero.org</value>
<value>raskolnikov#slums.org</value>
<value>stavrogin#gov.org</value>
<value>porfiry#gov.org</value>
</list>
</property>
<bean id="emailsOne" class="org.some.package.ClassOne">
<property name="emailList" ref="commonProp" />
</bean>
<bean id="emailsTwo" class="org.some.package.ClassTwo">
<property name="emailList" ref="commonProp" />
</bean>
You can do it using: util:list
<util:list id="myList" value-type="java.lang.String">
<value>foo</value>
<value>bar</value>
</util:list>
Then use this myList reference in other beans.
I have a java.util.Properties object which has few key value pairs in it. I am trying to use this Property object in the spring configuration file i.e. define the key in the spring config and during runtime, it should get the value from the properties object.
For ex:
<bean id="test" class="com.sample.Test">
<constructor-arg value="${PROPERTY_KEY} />
</bean>
Now during the runtime, the constructor should get the value that is present in the Property object.
Is there a way to get this done ?
Note: I do not want to use config.properties here. Looking to use java.util.Properties
Thanks,
Rahul
<context:property-placeholder location="classpath:placeholder.properties"/>
or
<bean id="properties"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:<file-name>.properties" />
</bean>
First you have to create bean to access your property file, like below
<bean id="appProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:localhost-mysql.properties</value>
<value>classpath*:mail-server.properties</value>
</list>
</property>
</bean>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties" ref="appProperties" />
</bean>
Next you can access Key-Value pair from your property files, like below
<bean id="mailServerSettings" class="com.ratormonitor.app.context.EmailSettings">
<property name="host" value="${mail.server.host}" />
<property name="port" value="${mail.server.port}" />
<property name="username" value="${mail.server.username}" />
<property name="password" value="${mail.server.password}" />
<property name="requestContextPath" value="${request.context.path}" />
</bean>
Hope this code will solve your problem.
You can use Spring Expression Language (SpEL) to get a java object value in spring configuration xml file.
An example :
<property name="misfireInstruction"
value="#{T(org.quartz.CronTrigger).MISFIRE_INSTRUCTION_FIRE_ONCE_NOW}"/>
So this is how I did:
As I said I had a java.util.Properties object. I then created a CustomProperty class which extended PropertySource>
public class CustomPropertySource extends PropertySource<Map<String, Object>>
Then in my main class I did the following:
AbstractApplicationContext context = new ClassPathXmlApplicationContext(new String[] {springConfigLocation, false);
context.getEnvironment().getPropertySources.addlast(new CustomPropertySource("custom", propertiesObject));
conext.refresh();
And then in the spring config file, I had to add this:
<context: property-placeholder ignore-unresolvable="true"/>
So in this way, I could fetch values for the keys defined in the spring config file, just like how we get the values from property files.
Thanks,
Rahul
I have a prototype bean 'client' for which I want to call a method 'addHandler' every time it is instantiated. I am using 'MethodInvokingFactoryBean' for this. I the docs, I can see that by default MethodInvokingFactoryBean operates in a singleton fashion and caches the result of 'getObject' to return in subsequent calls.
To circumvent this, I want to call 'setSingleton' with 'false'. I am not sure about how to do this.
<bean id="provider" class="com.example.credProvider">
<constructor-arg value="key1"/>
</bean>
<bean id="handler" class="com.example.Handler"/>
<bean id="client" class="com.example.DBClient" scope="prototype">
<constructor-arg><ref local="provider"/></constructor-arg>
<property name="endpoint" value="http://xyz:8080/" />
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="client"/></property>
<property name="targetMethod"><value>addHandler</value></property>
<property name="arguments">
<ref local="handler" />
</property>
</bean>
Will adding a scope="prototype" like this help? -
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" scope="prototype">
...
</bean>
I have two properties file in my application-
app.properties
level.user=username
easyDeploy_general.properties
user.update=Update
I have defined them in spring-servlet.xml in below way
<bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>WEB-INF/resources/easyDeploy_general</value>
<value>WEB-INF/resources/app</value>
</list>
</property>
</bean>
Now,I want to access those property key value pair from my controller. How can I achieve these?
If your controller is annotated then you can use #Value
#Value("${level.user}")
private String levelUser;
#Value("${user.update}")
private String userUpdate;
If it is xml driven then
<bean id="" class="some.myController">
<property name="levelUser" value="${level.user}" />
<property name="userUpdate" value="${user.update}" />
</bean>
I need to concatenate the string value of a spring bean, to an existing string, and then set it as an attribute of another bean:
<bean id="inet" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass"><value>java.net.InetAddress</value></property>
<property name="targetMethod"><value>getLocalHost</value></property>
</bean>
<bean id="host" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref local="inet"/></property>
<property name="targetMethod"><value>getHostName</value></property>
</bean>
At this point, I have the hostname, in the 'host' bean. I now need to concatenate it and pass it to the publishedEndpointUrl attribute. Something like this:
<jaxws:endpoint
id="foo"
publishedEndpointUrl= "http://" + host + "/Foo"
implementor="com.example.v1.foo"
address="/v1/Foo"/>
How is this done using spring xml configuration?
You could use Spring-EL and factory-method:
<bean id="localhost" class="java.net.InetAddress" factory-method="getLocalHost" />
<bean id="publishedUrl" class="java.lang.String">
<constructor-arg value="#{'http://' + localhost.hostName + '/Foo'}" />
</bean>
<jaxws:endpoint
...
publishedEndpointUrl="#publishedUrl"
...
EDIT:
The jaxws:endpoint tag appears to be able to reference bean values by using the #beanId notation but does not like Spring-EL. So by constructing a String bean, we get around this and it still looks fairly neat.
You need to look at PropertyPlaceholderConfigurer. This allows you define global properties, which can either come from a properties file, or in your case, you can define a default value, in which case it's just a global property. The following will work:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName">
<value>SYSTEM_PROPERTIES_MODE_OVERRIDE</value>
</property>
<property name="properties">
<props>
<prop key="driver">jdbc.oracle.Driver</prop>
<prop key="dbname">fred</prop>
</props>
</property>
<property name="locations">
<list>
<value>file:properties/application.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>
This means that you have default values for ${driver} and ${dbname}, which are used to define the data source. These values can be overridden in the application.properties file, or even as a -D option on the command line.
As jaxws:* namespace does not like Spring EL, an alternative could be to declare an EndpointImpl bean, instead of the jaxws:endpoint object.
It is some more work, but as pointed out in http://cxf.apache.org/docs/jax-ws-configuration.html, it is the actual implementation used by the namespace declaration.
You can mix propertyplaceholder vars and Spring EL:
<bean id="dataSource" class="xx.xxx.xxxxx.datasource.DataSourceWrapper" destroy-method="close">
<property name="dataSourceClassName" value="${db.dataSourceClassName}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maximumPoolSize" value="${db.maxConnections}" />
<property name="connectionTimeout" value="${db.connectionTimeout}" />
<property name="dataSourceProperties">
<props>
<prop key="databaseName">${db.databaseName}</prop>
<prop key="serverName">${db.serverName}#{':'}${db.port}</prop>
</props>
</property>
Look at ${db.serverName}#{':'}${db.port} concat.