Spring configuration method call to pass Calendar.YEAR - spring

I am trying to pass this arguments to the method call Calendar.add(Calendar.YEAR, -10) in the Spring config. How to pass Calendar.YEAR which is actually an int, but Spring config treats it as a String and throws error.
It works if I call without Spring like:
from = from.add(Calendar.YEAR, -10);
Config:
<bean id="currCalendar"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.util.Calendar"/>
<property name="staticMethod">
<value>java.util.Calendar.getInstance</value>
</property>
</bean>
<bean id="from1"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="currCalendar"/>
</property>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<value type="int">currCalendar.YEAR</value>
<value type="int">-50</value>
</list>
</property>
</bean>
Please suggest how to do in Spring

You almost had it, use the #{} placeholder:
<list>
<value type="int">#{currCalendar.YEAR}</value>
<value type="int">-50</value>
</list>

Related

How to override the bean definition in spring XML bean definition

I want to oveerride the bean definition of below one.
<bean id="productPrimaryImagePopulator" parent="defaultProductPrimaryImagePopulator">
<property name="imageFormatMapping" ref="imageFormatMapping"/>
<property name="imageFormats">
<list>
<value>zoom</value>
<value>product</value>
<value>thumbnail</value>
<value>cartIcon</value>
</list>
</property>
</bean>
To new one like below in new occ extension.
<bean id="productPrimaryImagePopulator" parent="defaultProductPrimaryImagePopulator">
<property name="imageFormatMapping" ref="imageFormatMapping"/>
<property name="imageFormats">
<list>
<value>sampleProduct</value>
</list>
</property>
</bean>
Can you please guide me how can we do this?
If you want to override the OOB populator, you can try spring-related bean declaration and changes.
<bean id="testImageFormatMapping" parent="defaultImageFormatMapping">
<property name="mapping">
<map>
<entry key="superZoom" value="1200Wx1200H"/>
<entry key="zoom" value="515Wx515H"/>
<entry key="store" value="365Wx246H"/>
<entry key="product" value="300Wx300H"/>
<entry key="thumbnail" value="96Wx96H"/>
<entry key="cartIcon" value="65Wx65H"/>
<entry key="styleSwatch" value="30Wx30H"/>
</map>
</property>
</bean>
<bean id="testProductPrimaryImagePopulator" parent="defaultProductPrimaryImagePopulator">
<property name="imageFormatMapping" ref="testImageFormatMapping"/>
<property name="imageFormats">
<list>
<value>zoom</value>
<value>product</value>
<value>thumbnail</value>
<value>cartIcon</value>
</list>
</property>
</bean>
<bean id="testProductGalleryImagesPopulator" parent="defaultProductGalleryImagesPopulator">
<property name="imageFormatMapping" ref="testImageFormatMapping"/>
<property name="imageFormats">
<list>
<value>zoom</value>
<value>product</value>
<value>thumbnail</value>
</list>
</property>
</bean>
<bean id="testProductPopulator"
parent="defaultProductPopulator">
<property name="productPrimaryImagePopulator" ref="testProductPrimaryImagePopulator"/>
<property name="productModelUrlResolver" ref="testcommercewebservicesProductModelUrlResolver"/>
</bean>
<bean id="testProductConverter" parent="defaultProductConverter">
<property name="populators">
<list>
<ref bean="testProductPopulator"/>
</list>
</property>
</bean>

How to register hibernate interceptor in Spring

I need to register class which is derived from EmptyInterceptor in Spring. Do someone know how to register this interceptor to Hibernate session? Definition of my SesionFactory is bellow.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.foo</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties"/>
</bean>
In order to register an interceptor you have to use entityInterceptor property. Please try :
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.foo</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties"/>
<property name="entityInterceptor">
<bean class="foo.bar.MyInterceptor"/>
</property>
</bean>

Call SOAP service asynchronously using Spring

I've to call a SOAP web service asynchronously. Currently, I'm calling it in a synchronous way using Spring webservicetemplate.
Current config is like:
<bean id="interceptedHttpClientBuilder" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="httpClientBuilder" />
<property name="targetMethod" value="addInterceptorFirst"> </property>
<property name="arguments">
<list>
<bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender.RemoveSoapHeadersInterceptor"/>
</list>
</property>
</bean>
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom">
<property name="socketTimeout" value="120000" />
</bean>
<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />
<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" />
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
<property name="defaultRequestConfig" ref="requestConfig" />
</bean>
<bean id="messageSender" class="org.springframework.ws.transport.http.HttpComponentsMessageSender">
<constructor-arg ref="httpClient"></constructor-arg>
</bean>
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="packagesToScan">
<list><value>...</value></list>
</property>
</bean>
<bean id="wsClientSecurityInterceptor"
class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="securementActions" value="UsernameToken" />
<property name="securementUsername"><value>${username}</value></property>
<property name="securementPassword"><value>${password}</value></property>
<property name="securementPasswordType" value="PasswordText" />
</bean>
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<property name="marshaller" ref="jaxb2Marshaller"></property>
<property name="unmarshaller" ref="jaxb2Marshaller"></property>
<property name="defaultUri"><value>${ws.url}</value></property>
<property name="interceptors">
<list>
<ref local="wsClientSecurityInterceptor"/>
</list>
</property>
<property name="messageSender" ref="messageSender"></property>
</bean>
Java call looks like:
MyResponse response = (MyResponse) webServiceTemplate.marshalSendAndReceive(req, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
((SoapMessage) message).setSoapAction("test");
}
});
May I know how can I change it to call the service asynchronously? Or Do I need to use something else in spring to achieve this?
Not sure why you use spring-integration tag in your question, but if we are here, please, definitely take a look into the #MessagingGateway with the Future<> as a return type: https://docs.spring.io/spring-integration/docs/5.0.1.RELEASE/reference/html/messaging-endpoints-chapter.html#async-gateway
The SOAP WebService can be called via Spring Integration <int-ws:outbound-gateway>: https://docs.spring.io/spring-integration/docs/5.0.1.RELEASE/reference/html/ws.html
The samples are here: https://github.com/spring-projects/spring-integration-samples
To be more clear the code might looks like this:
<int:gateway id="mathService"
service-interface="org.springframework.integration.samples.async.gateway.MathServiceGateway"
default-request-channel="requestChannel"
async-executor="executor"/>
Where that MathServiceGateway is like this:
public interface MathServiceGateway {
Future<Integer> multiplyByTwo(int i);
}
The WS call is simple as well:
<int-ws:outbound-gateway request-channel="requestChannel" uri="http://www.w3schools.com/xml/tempconvert.asmx"/>

How do I add a jackson objectmapper dateformat config into spring mvc config?

I have a problem about jackson 2.1.
My pojo have some date properties, I want turn it to string, I setted it in spring-servlet.xml but it's not usefull.
I don't like use #JsonSerialize(using = JsonDateSerializer.class) on the setter.
this is my configuration:
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
Assuming you are using Spring 3.1, you should customize your mvc-annotation driven tag properties,
as is shown in
Configuring ObjectMapper in Spring
Assuming that your bean declaration is correct I think it should be something like
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="com.fasterxml.jackson.databind.ObjectMapper">
<property name="dateFormat">
<bean class="java.text.SimpleDateFormat">
<constructor-arg type="java.lang.String" value="yyyy-MM-dd"></constructor-arg>
</bean>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
My setup is:
<!-- Date Format -->
<bean id="dateFormatter" class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-MM-dd"/>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="dateFormatter" />
<property name="targetMethod" value="setTimeZone" />
<property name="arguments">
<list>
<ref bean="timeZone"/>
</list>
</property>
</bean>
<!-- End Date Format -->
<!-- Jackson Object Mapper -->
<bean id="jacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="configure" />
<property name="arguments">
<list>
<value type="org.codehaus.jackson.map.DeserializationConfig.Feature">FAIL_ON_UNKNOWN_PROPERTIES</value>
<value>false</value>
</list>
</property>
</bean>
<bean id="jacksonDeserializationConfig" class="org.codehaus.jackson.map.DeserializationConfig" factory-bean="jacksonObjectMapper" factory-method="getDeserializationConfig" />
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonDeserializationConfig" />
<property name="targetMethod" value="setDateFormat" />
<property name="arguments">
<list>
<ref bean="dateFormatter"/>
</list>
</property>
</bean>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="jacksonObjectMapper" />
<property name="targetMethod" value="setDeserializationConfig" />
<property name="arguments">
<list>
<ref bean="jacksonDeserializationConfig"/>
</list>
</property>
</bean>
<!-- End Jackson Object Mapper -->
<!-- JSON provider -->
<bean id="jsonRestProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider">
<property name="mapper" ref="jacksonObjectMapper"/>
</bean>

How to add more hbm to existing mappingResources or existing hbm list in Spring

I have a parent-app, which includes sub-apps.
My Parent app has its own included list of hbms
<bean name="mappingResources"
class="my.xxx.MyListFactoryBean">
<property name="sourceList">
<list>
<value>aaa/bbb/aa.hbm.xml</value>
<value>aaa/bbb/bb.hbm.xml</value>
<value>aaa/bbb/cc.hbm.xml</value>
</list>
</property>
</bean>
My sub-apps want to add its own list of dependent hbms to the parent-app's.
The way it should work is, if it includes this sub-app then it would include the new hbms as well and the child-app would initiate the include.
new hbms to be included could look like
xx/dd.hbm.xml
xx/ee.hbm.xml
How can we do it?
Your Solution could be:
Split up the 'mappingResources' to
<bean name="mappingResources" class="my.xxx.MyListFactoryBean">
<property name="sourceList" ref="hbmSourceList" />
</bean>
<bean id="hbmSourceList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>aaa/bbb/aa.hbm.xml</value>
<value>aaa/bbb/bb.hbm.xml</value>
<value>aaa/bbb/cc.hbm.xml</value>
</list>
</constructor-arg>
</bean>
In the child-app
refer to the bean "hbmSourceList" and invoke an "addAll" on it with an another list via the "MethodInvokingFactoryBean"
<bean id="hbmSourceListExtender" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref bean="hbmSourceList"/></property>
<property name="targetMethod"><value>addAll</value></property>
<property name="arguments">
<ref local="childAppHbmSourceList"/>
</property>
</bean>
<bean id="childAppHbmSourceList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>xx/dd.hbm.xml</value>
<value>xx/ee.hbm.xml</value>
</list>
</constructor-arg>
</bean>

Resources