java spring session how to custom cookie key - spring

I using spring session HttpSession, how can I custom cookie key, I tried this solution: Custom cookie name when using Spring Session. but it does not work, the name is SESSION still.
my config like below:
<context:annotation-config/>
<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>
<context:property-placeholder location="classpath:/env/env_test.properties"/>
<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
p:port="${spring.redis.port}" p:hostName="${spring.redis.host}"/>
<bean id="mapSessionRepository" class="org.springframework.session.MapSessionRepository" />
<bean id="sessionRepositoryFilter"
class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg ref="sessionRepository"/>
<property name="httpSessionStrategy">
<bean class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieName" value="_session_id" />
</bean>
</property>
</bean>

You just need to add below bean to create custom cookie.
<bean class ="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="JSESIONID"></property>
</bean>
JESSIONID - Custom Cookie Name
Please remove below configuration fr`enter code here`om xml file.
<bean id="sessionRepositoryFilter"
class="org.springframework.session.web.http.SessionRepositoryFilter">
<constructor-arg ref="sessionRepository"/>
<property name="httpSessionStrategy">
<bean class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieName" value="_session_id" />
</bean>
</property>
</bean>

You have to create a bean with class
org.springframework.session.web.http.DefaultCookieSerializer
So inside of that bean, you define your custom properties, after you declare that bean as a property of the following:
org.springframework.session.web.http.CookieHttpSessionStrategy
By example:
<bean id="yourCookieSerializer" class="org.springframework.session.web.http.DefaultCookieSerializer">
<property name="cookieName" value="yourCustomName"/>
<property name="cookiePath" value="yourCustomPath"/>
...
</bean>
<bean id="cookieHttpSessionStrategy" class="org.springframework.session.web.http.CookieHttpSessionStrategy">
<property name="cookieSerializer" ref="yourCookieSerializer"></property>
</bean>

Related

ignoreUnresolvablePlaceholders not working

I am using ignoreUnresolvablePlaceholders in my spring context.
as below.
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messaging.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
event setting ignoreUnresolvablePlaceholders to true its not ignoring my spring bean being injecting.
i am passing amq.topic= in property file for
<bean id="messageTopic1" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="${amq.topic}" />
</bean>
But its not ignoring this bean for being injecting.
below is my Spring-context.xml amd property file.
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:messaging.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="false" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<!-- Active MQ Broker Configuration Details -->
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${amq.url}" />
</bean>
<bean id="messageQueue1" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="${amq.queue}" />
</bean>
<bean id="messageTopic1" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="${amq.topic}" />
</bean>
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>
<bean id="activeMQ" class="com.isc.common.messaging.AmqUtilityHelper">
<property name="destination" ref="messageQueue1" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- <bean id="activeMQ1" class="com.isc.common.messaging.AmqUtilityHelper">
<property name="destination" ref="messageTopic1" />
<property name="jmsTemplate" ref="jmsTemplate" />
</bean> -->
<bean id="messageBroker" class="com.isc.common.messaging.AmqUtilityHelper">
<property name="activeBroker" value="${active.broker}" />
</bean>
</beans>
And Below is the property file that i am loading.
#AMQ/Solace properties production:
# uncomment the borker to use
active.broker=activeMQ
#active.broker=solace
#active.broker=activeMQ
#AMQ Broker Properties
amq.url=failover:(tcp://localhost:61616)??initialReconnectDelay=2000&maxReconnectAttempts=5
amq.queue=messageQueue1
amq.topic=
#Solace Broker Properties
solace.url=smf://192.168.56.101:55555
solace.userName=spring_user#Solace_Spring_VPN
solace.passWord=spring_password
solace.jndiName=JNDI/CF/spring1
solace.queue=JNDI/Q/requests
#solace.topic=JNDI/topic1
By default, ApplicationContext implementations eagerly create and
configure all singleton beans as part of the initialization process.
Generally, this pre-instantiation is desirable, because errors in the
configuration or surrounding environment are discovered immediately,
as opposed to hours or even days later. When this behavior is not
desirable, you can prevent pre-instantiation of a singleton bean by
marking the bean definition as lazy-initialized. A lazy-initialized
bean tells the IoC container to create a bean instance when it is
first requested, rather than at startup.
Can you please try to lazily initialize your bean using below construct.
lazy-init="true"
<bean id="messageTopic1" class="org.apache.activemq.command.ActiveMQTopic"
lazy-init="true">
<constructor-arg value="${amq.topic}" />
</bean>

Spring: RestTemplate messageConverters are not registered

I have an issue with the registration of the messageConverters on the org.springframework.web.client.RestTemplate.
I have tried (with no effect) two solutions:
1) Defining a bean of restTemplate in the Spring configuration, with all the converters that I need:
<bean id="restTemplate" name="restTemplate" class="org.springframework.web.client.RestTemplate" autowire-candidate="true">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="test.myApp.MyHttpMessageConverter" />
</list>
</property>
</bean>
2) Declaring the messageConverters in the <mvc:annotation-driven> tag:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.FormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="test.myApp.MyHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
I also used the attribute register-defaults="true"
With both of these two solutions, the restTemplate instance contains only the same 6 default converters:
ByteArrayHttpMessageConverter
StringHttpMessageConverter
ResourceHttpMessageConverter
SourceHttpMessageConverter
AllEncompassingFormHttpMessageConverter
Jaxb2RootElementHttpMessageConverter
There is no trace of the test.myApp.MyHttpMessageConverter and of the org.springframework.http.converter.FormHttpMessageConverter.
I'm using the 4.3.1.RELEASE version of Spring.
How to make possible the registration of the messageConverters?
Thanks in advance.
I have found the solution.
Since I'm using the Spring Integration <int-http:outbound-gateway> component, I need to add the attribute "rest-template" in the outbound-gateway.
So, the working solution is the (1) that I posted above, together with this configuration:
<int-http:outbound-gateway rest-template="restTemplate" ... />
Otherwise, it will be used a default rest-template (without custom messageConverters).

Programmatically set property of bean

I am trying to figure out how I can adjust the "period" property of the "destroyWorldTask" bean that is defined like this in my list of beans. Is this possible? What is the proper way to do this?
<bean id="mytimerfactory"
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="daemon" value="true"/>
<property name="myTimerTasks">
<list>
<bean class="org.springframework.scheduling.timer.ScheduledTimerTask" id="destroyWorldTask">
<property name="delay" value="100"/>
<property name="period" value="10000/>
<property name="runnable">
<bean class="com.scene7.is.util.SafeRunnable">
<constructor-arg ref="destroyWorld"/>
</bean>
</property>
</bean>
</list>
</property>
</bean>
There are two possible answers:
1. If you want the "period" property be set somewhere in the program, you don't need to set in the context configuration. (Which I think not suitable for you, as you are using a spring class, not yours).
2. Extend from org.springframework.scheduling.timer.ScheduledTimerTask and make your edition of the class, something like:
public MyTimeScheduledTimerTast extends ScheduledTimerTask{
//...
}
and set that property in your program. (Now it's in your hand)
Then update your context configuration like this:
<bean id="mytimerfactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="daemon" value="true"/>
<property name="myTimerTasks">
<list>
<bean class="myPackage.MyScheduledTimerTask" id="destroyWorldTask">
<!-- Set those properties that are not set in your program -->
</bean>
</list>
</property>
</bean>

Spring resource

Problem: I want to make the below bean definitions (specified in aaplicationContext.xml) optional for "org.springframework.web.context.ContextLoaderListener". If i am not providing the "emsPropLocation" context parameter correctly, tomcat web container is not able to initialized properly and it is obvious reason. Is there any way to make it optional?
appicationContext.xml:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="false"/>
<property name="location" value="file:/#{contextParameters.emsPropLocation}" />
</bean>
<!-- TIBCO Connection Factory Bean -->
<bean id="tibcoConnectionFactory" class="com.tibco.tibjms.TibjmsConnectionFactory">
<constructor-arg value="${emsServerURL}"/>
<property name="userName" value="${emsUserName}"/>
<property name="userPassword" value="${emsPassword}"/>
<property name="connAttemptCount" value="${connAttemptCount}"/>
<property name="connAttemptDelay" value="${connAttemptDelay}"/>
<property name="connAttemptTimeout" value="${connAttemptTimeout}"/>
<property name="reconnAttemptCount" value="${reconnAttemptCount}"/>
<property name="reconnAttemptDelay" value="${reconnAttemptDelay}"/>
<property name="reconnAttemptTimeout" value="${reconnAttemptTimeout}"/>
</bean>
<!-- bean id="tibcoUtil" class="com.nr.ns.upload.TibcoUtil" scope="singleton">
<constructor-arg value="true"/>
</bean-->
<bean id="jmsExceptionListener" class="com.nr.ns.upload.LogMsgExceptionListener"/>
<!-- Spring CachingConnectionFactory Bean -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="tibcoConnectionFactory"/>
<property name="reconnectOnException" value="${reconnectOnException}"/>
<property name="sessionCacheSize" value="${sessionCacheSize}"/>
<property name="exceptionListener" ref="jmsExceptionListener"/>
</bean>
<!-- JMSTemplate Bean -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory"/>
<property name="receiveTimeout" value="${receiveTimeout}"/>
<property name="deliveryMode" value="${deliveryMode}"/>
</bean>
We are keeping WAR file outside tomcat and to make it happen we have "app.xml" file inside TOMCAT_HOME/conf/Catalina/localhost.
app.xml:
<Context path="/app"
docBase="/abc/ccp/app.war"
reloadable="true"
unpackWAR="false">
<Parameter name="emsPropLocation"
value="/xyz/config/EMSServerConf.properties"
override="false"/>
</Context>
have a try to change the ignoreResourceNotFound property of your propertyConfigurer to true.
If contextParameters.emsPropLocation is not set, this will default to what is afer the colon.
<property name="location" value="file:/#{contextParameters.emsPropLocation:/xyz/config/EMSServerConf.properties}" />

Spring and Mybatis multiple data sources setup

My applications uses Spring3+MyBatis3. I'm trying to setup multiple data source for it. Setup looks like:
<!-- db1 setup-->
<bean id="db1SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:configLocation="WEB-INF/mybatis/sqlMapConfig.xml"
p:dataSource-ref="db1DataSource" />
<bean id="db1SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="db1SqlSessionFactory"/>
</bean>
<!-- db2 setup -->
<bean id="db2SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
p:configLocation="WEB-INF/mybatis/sqlMapConfig.xml"
p:dataSource-ref="db2DataSource" />
<bean id="db2SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg ref="db2SqlSessionFactory"/>
</bean>
In the logs, I've found this message:
No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: [db1SqlSessionFactory, db2SqlSessionFactory]
I googled and looked into mybatis manuals but couldn't find way how to setup multiple data sources with mybatis.
Any ideas?
also solved ! just reference your factory bean in MapperScannerConfigurer : sqlSessionFactoryBeanName
First data source >>>>>>>
<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource1"/>
</bean>
<bean id="MapperScannerConfigurer1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.package.p1"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
</bean>
Second data source >>>>>>
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
</bean>
<bean id="MapperScannerConfigurer1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.package.p2"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
</bean>
solved, the problem was that I must specify directly reference to sqlSessionFactory
<bean id="myDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
p:sqlSessionTemplate-ref="db1SqlSessionTemplate"
p:mapperInterface="my.project.domain.dao.MyDao"
p:sqlSessionFactory-ref="db1SqlSessionFactory"/>
In a DAO implementation use SqlSessionTemplate instead of SqlSessionDaoSupport. Inject bean db1SqlSessionTemplate or db2SqlSessionTemplate.
#Repository
public class TestDaoImpl implements TestDao{
#Autowired
private SqlSession db1SqlSessionTemplate;
...
db1SqlSessionTemplate.selectList("testSelect");
...
}
When extending SqlSessionDaoSupport the context Spring does not know that you use SqlSession.

Resources