How to access db2 sequence in Spring - spring

How to access db2 sequence in Spring?
Need to access a sequence to generate an id and pass it to an insert statement using spring using jdbcTemplate.

First, you have to define a bean that handles the sequence. Something like this:
<bean id="incrementer" class="org.springframework.jdbc.support.incrementer.DB2SequenceMaxValueIncrementer">
<property name="dataSource" ref="dataSource" />
<property name="incrementerName" value="YOUR_SEQUENCE_NAME" />
</bean>
Then, you have to wire this bean to the bean that's going to invoke it. e.g.:
#Autowired
private DataFieldMaxValueIncrementer incrementer;
Finally, you use the sequence bean to get a value:
Long identifier = incrementer.nextLongValue();

Have you tried queryForInt() with the SQL select sequence.nextval from dual?
http://static.springsource.org/spring/docs/2.5.6/api/org/springframework/jdbc/core/simple/SimpleJdbcTemplate.html#queryForInt(java.lang.String, java.lang.Object...)

Related

MethodInvokingFactoryBean returns itself instead of desired object

I am trying to use a MethodInvokingFactoryBean to get an instance of a com.amazonaws.regions.Region for use in configuring a com.amazonaws.services.kinesis.AmazonKinesisClient. I am doing this in Blueprint, Camel, Karaf.
<bean id="awsRegion" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="com.amazonaws.regions.RegionUtils"/>
<property name="targetMethod" value="getRegion"/>
<property name="arguments">
<list>
<value>EU-WEST-1</value>
</list>
</property>
</bean>
<bean id="kinesisClient" class="com.amazonaws.services.kinesis.AmazonKinesisClient">
<property name="region" ref="awsRegion"/>
</bean>
This seems like it should work, with the first bean creating a Region, and the second bean using it.
However, I get an error that makes it seem like the MethodInvokingFactoryBean is just returning an instance of itself instead of Region.
org.osgi.service.blueprint.container.ComponentDefinitionException: Error setting property: PropertyDescriptor <name: region, getter: null, setter: [class com.amazonaws.AmazonWebServiceClient.setRegion(class com.amazonaws.regions.Region)]
...
Caused by: java.lang.Exception: Unable to convert value org.springframework.beans.factory.config.MethodInvokingFactoryBean#2289c050 to type com.amazonaws.regions.Region
The method I'm invoking in RegionUtils should return a Region
https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/regions/RegionUtils.html#getRegion-java.lang.String-
I came upon this way of getting the Region in the Client in this question, where the solution seemed to work for the asker.
Bind aws sqs region camel
At first , apache-camel tag is excess here. As second, you missed that asker is using spring context and you are using blueprint context. Try something like this:
<bean id="awsRegion" class="com.amazonaws.regions.RegionUtils" factory-method="getRegion">
<argument value="EU-WEST-1"/>
</bean>
<bean id="kinesisClient" class="com.amazonaws.services.kinesis.AmazonKinesisClient">
<property name="region" ref="awsRegion"/>
</bean>
EDIT: just tested this example with latest aws-java-sdk and it is worked

Difference b/w primary and autowire-candidate attribute of bean tag in spring

I am new to spring. When i am going through auto wiring byType i came to know about these attributes primary and autowire-candidate.
I didn't get the exact difference b/w these two as setting these parameter to false will make the other bean a candidate for autowiring.
Can anybody help me in understanding these two.
Thanks
Let say there is interface
interface Translator { String translate(String word);}
Your application use the translator widely to translate from English to Polish. However, is some specific case you want to use dedicated translator, because vocabulary is specific. For example, string always means "sequence of characters" but never "underwear".
Sample configuration:
<bean class="EnglishToPolishTranslator" />
<bean class="ComputerScienceEnglishToPolishTranslator" autowire-candidate="false"/>
Everywhere EnglishToPolishTranslator will be autowired except some concrete place where ComputerScienceEnglishToPolishTranslator will be injected by reference.
Some day next customer arrive with requirement: use simpler words. The requirement is achieved by class SimpleEnglishToPolishTranslator. But computer science translator should remain unchanged: it is too costly to modify it.
Your company want keep product easy to maintain. Base application will not be modified, but for the customer the product will extended with extra library configured:
<bean class="SimpleEnglishToPolishTranslator" primary="true"/>
In result, everywhere SimpleEnglishToPolishTranslator will be used except computer science area.
Maybe it is overcomplicated, but shows a difference I found between autowire-candidate and primary
BTW, "autowire-candidate" doesn't have corresponding annotation. It looks to me that "autowire-candidate" is dead end in Spring evolution
if we configure bean for more than one time with different ids then IOC will throw an Exception. To overcome this duplicate beans problem, we can use autowire-candidate=”false” or primanry="true"
Example: i have two classes Mobile and Processor
Case -1: autowire-candidate=”false”
<bean id="mobile" class="com.Mobile" autowire="byType">
<property name="mobileName" value="Redmi"></property>
<property name="mobileModel" value="Note 5"></property>
</bean>
<bean id="process1" class="com.Processor"
autowire-candidate="false">
<property name="process" value="2GHz"></property>
<property name="ram" value="4GB"></property>
</bean>
<bean id="process2" class="com.Processor">
<property name="process" value="1GHz"></property>
<property name="ram" value="3GB"></property>
</bean>
As per above configuration, process1 bean will be ignored and process2 bean will be injected.
Case -2: primanry="true"
<bean id="mobile" class="com.Mobile" autowire="byType">
<property name="mobileName" value="Redmi"></property>
<property name="mobileModel" value="Note 5"></property>
</bean>
<bean id="process1" class="com.Processor" primary="true">
<property name="process" value="2GHz"></property>
<property name="ram" value="4GB"></property>
</bean>
<bean id="process2" class="com.Processor">
<property name="process" value="1GHz"></property>
<property name="ram" value="3GB"></property>
</bean>
As per above configuration, process2 bean will be ignored and process1 bean will be injected.

Spring Configuration Query

I have one spring configuration file with entry like below...
<bean id="beanId" class="a.b.c.d.MyBean">
<property name="firstProperty" value="report_{date}.xls"/>
</bean>
Somewhere in my java code, I am fetching this bean and then its property "firstProperty" later.
I am little curious, when I get the value of property "firstProperty" I get report_.xls i.e report_20130307.xls
I have searched all my code including bundles, xmls but not clear that where we are setting {date} with todays timestamp.
Do you have any clue where we can do this?
Thanks
Jai
It is the property-placeholder mechanism.
Read more on http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/xsd-config.html#xsd-config-body-schemas-context-pphc.
In most of the cases, the values to property are set from properties file using expression language. Like
<bean id="dataSource" class="a.b.c.d.DataSource">
<property name="databaseUrl" value="{db.url}"/>
</bean>
Or if the property is a ref to another bean, e.g. Object B is member variable of Object A.
<bean id="refA" class="a.b.c.d.A">
<property name="b" ref="refB"/>
</bean>
<bean id="refB" class="a.b.c.d.B">
</bean>
Its quite simple guys...as we know setter are called for each property. So same in my case,
In bean we are setting variable "firstProperty" + today timestamp like below.
public void setfirstProperty(String firstProperty) {
this.firstProperty = firstProperty + <methodToReplaceDateStringWithTimeStamp>;
}
Thanks
Jai

Is it possible to set a resource property value inside applicationcontext?

In an applicationcontext.xml, is it possible to set a value which can be used later in SPEL expressions?
For example is there a way to do this?:
<setProperty name="foo" value="someval" />
<bean id="beanId" name="beanName" class="SomeClass">
<property name="someVal" value="blah_${foo}"/>
</bean>
The actual reason I want to do this is that I use statements to create entity managers which are used in many different application contexts. The problem is that the entity managers require a unique name which is used by Bitronix to create a local file which breaks if multiple unit tests run at the same time using the same name for that field. To set that unique name I currently have a separate properties file for each application context and import it to get a unique name from it.
Rather than doing that nonsense I'd rather just do this:
<setProperty name="uniqueName" value="someUniqueName" />
<import resource="classpath*:shared/db/fooDb.xml" />
You can do this using Spring-el and util namespace:
<util:properties id="myprops">
<prop key="foo">someval</prop>
</util:properties>
<bean id="beanId" name="beanName" class="SomeClass">
<property name="someVal" value="blah_#{myprops.foo}"/>
</bean>

How to refer to the returned type

I want to create a spring bean as below.
<bean id="qNameString" class="javax.xml.xpath.XPathConstants.STRING"/>
Here I want the reference to return type which is a QName but I understand the way I referred is wrong. Can someone please help on this.
That won't work, because class="javax.xml.xpath.XPathConstants.STRING" makes no sense, since what you're referring to isn't a class.
You can refer to static fields using <util:constant>, as documented here:
<property name="...">
<util:constant static-field="javax.xml.xpath.XPathConstants.STRING"/>
</property>
Spring can create a QName for you like this:
<bean id="qName" class="java.xml.namespace.QName">
<constructor index="0" value="localpart"/>
<constructor index="1" value="namespaceURI"/>
</bean>
Replace localpart and namespaceURI with the local name and namespace.
To reference a constant in a class, like javax.xml.xpath.XPathConstants.STRING
<bean id="qNameString" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="targetField" value="javax.xml.xpath.XPathConstants.STRING"/>
</bean>
A shorter version is available with the util schema:
<util:constant static-field="java.xml.xpath.XPathConstants.STRING"/>
Apart from being shorter, the id of the bean will be java.xml.xpath.XPathConstants.STRING rather than qNameString.
See FieldRetrievingFactoryBean and The util schema

Resources