Programmatically change property value - spring

<beans:bean id="loginUrlAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
<beans:property name="loginFormUrl" value="/session-timeout-1.do" />
</beans:bean/>
I have 2 different session-timeout pages for different types of users. After a user logs in, the value of the property may have to change from "/session-timeout-1.do" to "/session-timeout-2.do" after checking the type of the user.
I am wondering that is there an API that can change the property value at runtime?
Or is it possible to have a variable in the config file, e.g.
<beans:property name="loginFormUrl" value="${time-out-url}">
where variable "time-out-url" can be set programmatically?

You could subclass the LoginUrlAuthenticationEntryPoint class and provide your own logic.
See the class source here:
http://git.springsource.org/spring-security/rwinchs-spring-security/blobs/2d271666a406a4409def9afcd73ea340c40a7a88/web/src/main/java/org/springframework/security/web/authentication/LoginUrlAuthenticationEntryPoint.java
Specifically the method:
determineUrlToUseForThisRequest
which "Allows subclasses to modify the login form URL that should be applicable for a given request".

Related

How to get set elements in the same sequence as in a Spring bean definition

I have a lot of bean definitions that look similar to this
<bean id="TransformationMapOrganization " class="com.artifact_software.adt.plugin.transformation.RemoveColumnsTransformationImpl">
<property name="pluginId" value="Remove Division, Department, and Cost Code" />
<property name="dataStoreName" value="person_data"/>
<property name="columnNames">
<set>
<value>Division</value>
<value>Dept Code</value>
<value>Cost Code</value>
</set>
</property>
</bean>
In the code, the columnNames are defined as:
protected List<String> columnNames;
It appears that erroneous duplicate values are ignored rather causing an error which is good. I hope that I can count on that since it does make life easier!
What set implementation will Spring use?
What is the correct way to iterate through columnNames to get the columnNames in the same sequence as they are specified in the bean?
You can set the implementation class via <set set-class="com.my.SetImpl" />, (see current doc). (com.my.SetImpl must implement java.util.Set)
alternatively: define targetClass on your SetFactoryBean...
If omit, current spring, will use java.util.LinkedHashSet.
More correct, reliable & future-safe it would be to map columnNames as java.util.Set not as a List (+ to use set-class).
If no set-class attribute is supplied, the container chooses a Set implementation.
(in your case,) Obviously spring manages to convert from set to list "smoothly" ("by hand" it's also easy done thx to api design). Spring (seems to) also preserves you distinct entries, LinkedHashSet implementation additionally guarantees/should "preserve order"...
Your Bean definition and field names are different. Change
<property name="ColumnNames">
to
<property name="columnNames">

How to serialize JodaTime using UNIX timestamp format with Jackson

I am using
jackson-annotations-2.4.3.jar
jackson-core-2.4.3.jar
jackson-databind-2.4.3.jar
jackson-datatype-joda-2.4.3.jar
and Spring 3.2.11. I am using the joda time's DateTime format, and i want to serialize beans that have some date-times as properties. What i would like is to serialize only date-time's timestamp. Instead, jackson serializes the whole object, which leads to problems in js afterwards.
What i am trying to achieved worked when using jackson 1.8.3.
I have tried to register a JodaModule to the object mapper for MappingJackson2HttpMessageConverter, by defining this in applicationContext.xml. Even though the joda module is loaded, it doesnt seem to work.
I tried the following config:
<beans:beans>
<beans:bean id="objectMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean"
p:simpleDateFormat="yyyy-MM-dd'T'HH:mm:ss.SSSZ">
</beans:bean>
<beans:bean
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"
p:targetObject-ref="objectMapper" p:targetMethod="registerModule">
<beans:property name="arguments">
<beans:list>
<beans:bean class="com.fasterxml.jackson.datatype.joda.JodaModule" />
</beans:list>
</beans:property>
</beans:bean>
</beans:beans>
After that i tried:
What else should i try?
Nobody seems to care about this question. However, i will close it, since i have found the answer: i had to save the configuration in a xml file and import it in multiple xml's so that the config would be visible to all the involved contexts.

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>

When to use <ref bean> and when to use <ref local> in Spring?

When to use <ref bean="service" /> and when to use <ref local="service" /> in Spring?
Specifying the target bean by using the bean attribute of the ref tag is the most general form, and will allow creating a reference to any bean in the same BeanFactory/ApplicationContext (whether or not in the same XML file), or parent BeanFactory/ApplicationContext. The value of the bean attribute may be the same as either the id attribute of the target bean, or one of the values in the name attribute of the target bean.
<ref bean="someBean"/>
Specifying the target bean by using the local attribute leverages the ability of the XML parser to validate XML id references within the same file. The value of the local attribute must be the same as the id attribute of the target bean. The XML parser will issue an error if no matching element is found in the same file. As such, using the local variant is the best choice (in order to know about errors are early as possible) if the target bean is in the same XML file.
<ref local="someBean"/>
This is from the Spring source reference here
The local attribute on the ref element is no longer supported in the 4.0 beans xsd since it does not provide value over a regular bean reference anymore. Simply change your existing ref local references to ref bean when upgrading to the 4.0 schema.
<ref local="someBeanId"> should be used when you have a duplicate id in your parent-child config files and you want to distinguish between the two in either config file.
<ref parent="someBeanId"> should be used in the child config file to reference the parent id.
<ref bean="someBeanId"> should be used when you do not have a duplicate id in your parent-child config files.
<ref local=".."> requires that the bean being referenced is in the same config file.
<ref bean="..."> requires only it to be in the same context, or in a parent context.
The difference is primarily one of documentation. If you see <ref local="...">, then you know you need only look in the same file to find it. Other than that, there's not much difference. I would generally use <ref bean="..."> in most cases.
In spring 4.1
the local attribute is not valid.
i used in the parent xml a name attribute for the
and in the child file a referenced the bean by the alias i gave already.

c3p0 useScatteredAquireTask Spring Bean

Hello I wan't to know how can I set up a bean so that it sets the ScatteredAquireTask to "True".
I've been trying:
<bean id="c3p0Props" class="com.mchange.v2.resourcepool.BasicResourcePool.ScatteredAcquireTask" >
<property name="USE_SCATTTERED_ACQUIRE_TASK" value="true" />
</bean>
I also tried ...resourcepool.experimental.useScatteredAcquireTask... didn't worked. I'm not sure how can I set this on spring. I'm using 0.9.1.2, can't go to 0.9.2.prep1 at the moment. Thanks.
That's because USE_SCATTTERED_ACQUIRE_TASK isn't a property of the ScatteredAcquireTask class (i.e. there's no method called setUSE_SCATTTERED_ACQUIRE_TASK), it's an internal static field of the class that's not accessible to Spring.
You're not going to be able to set that values in a Spring bean defintion, you need to find out how to influence that value by some other means.

Resources