<security:custom-authentication-provider /> means? - spring

i have a bean in xml like below
<bean id="theCustomAuthenticationProvider" class="test.custom.CustomAuthenticationProvider">
<security:custom-authentication-provider />
a.may i know what does security:custom-authentication-provider means when i put it in my bean like above?
b. do i need to create
<bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager"> ref to theCustomAuthenticationProvider
in the xml ?
c. if b. answer is yes, alternatively, can i use ref of theCustomAuthenticationProviderinside tag?

The idea is that marking a bean:
<security:custom-authentication-provider />
Will registor the bean as a authentication provider with the AuthenicationManager provided by spring security. You don't need b.

Related

Spring injecting one reference bean into another reference bean

In the below spring configuration snippet, injecting reference of "SomeManager" into bean "SomeWorker" and "SomeLocal". The reference of "SomeLocal" is also injected into "SomeWorker".
My doubt is is it possible to inject the same reference of "SomeManager" injected into "SomeWorker" into another bean, like here in "SomeLocal".
Problem is if i inject it separately into "SomeWorker" and "SomeLocal" than unnecessarily there will be two instances of "SomeManager" which is basically not required in this scenario as "SomeLocal" is referred only within the "SomeWorker"
<bean id="SomeWorker" class="com.test.worker.SomeWorker">
<property name="someManager" ref="SomeManager" />
<property name="someLocal" ref="SomeLocal"/>
<bean id="SomeLocal" class="com.test.local.SomeLocal">
<property name="someManager" ref="SomeManager" />
</bean>
<bean id="SomeManager" class="com.test.manager.SomeManager"/>

jee:remote-slsb use custom factory

i have this definition for calling an EJB
<util:properties id="ejbJndiConfig" location="file:/path/to/ejb-jndi-config.properties" />
<jee:remote-slsb id="myEjbService"
jndi-name="myEjbName"
business-interface="foo.bar.MyBusinessInterface"
cache-home="false"
lookup-home-on-startup="false"
refresh-home-on-connect-failure="true"
environment-ref="ejbJndiConfig"
expose-access-context="true">
</jee:remote-slsb>
All is working great and an instance of:
org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean is created to call the EJB method. But what about if i want to change this default behaviour and change the used class?
With old style spring we can do something like this:
<bean id="service" class="org.springframework.remoting.rmi.RmiProxyFactoryBean">
<property name="remoteInvocationFactory" ref="invocationFactory"/>
....
</bean>
<bean id="invocationFactory" class="src.rmi.CustomRemoteInvocationFactory"/>
is possible with new definition style?

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.

Create a Guava TypeToken in Spring xml config?

I'd like to be able to inject Guava TypeToken objects by specifying them as a bean in a Spring xml configuration. Is there a good way to do this? Has anyone written any cade/library to make this easier?
TypeToken seems to work by using reflection to introspect its generic types and is thus constructed using an anonymous class like:
new TypeToken<List<String>>() {}
Spring's xml config syntax doesn't seem to accept generics at all, presumably because it's built at runtime and doesn't "need" them (since generics are compile time checks and technically erased at runtime).
So the only way I know to instantiate a TypeToken bean is to do it in java:
TokenConfig.java:
#Configuration
public class TokenConfig {
#Bean
public TypeToken<List<String>> listOfStringsToken() {
return new TypeToken<List<String>>() {};
}
}
system-test-config.xml:
<beans>
<context:annotation-config/>
<bean class="com.acme.TokenConfig"/>
<bean class="com.acme.Consumer">
<property name="typeToken" ref="listOfStringsToken"/>
</bean>
</beans>
Is there a way to do this with just an xml config?
Maybe you can use spring FactoryBeans: look for factory methods at http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html
To answer my own question:
It IS possible to create a non-generic TypeToken using the static constructor TypeToken.of(Class), but this wont work for deeper generic types.
Here's the Spring xml config:
<bean class="com.google.common.reflect.TypeToken" factory-method="of">
<constructor-arg type="java.lang.Class" value="java.lang.Integer" />
</bean>
Which is equivelent to:
TypeToken.of(Integer.class)
and
new TypeToken<Integer>() {}
I also found a way to use the TypeToken.of(Type) constructor with a ParameterizedType constructed using Google Guice's Types utility. Guava has one too, but it's not public. :'(
I'm not sure if this is quite as robust as using TypeToken/TypeCapture, but it seems to work. Unfortunately it's pretty ugly and long... (maybe someone can simplify it?)
<bean class="com.google.common.reflect.TypeToken" factory-method="of">
<constructor-arg index="0">
<bean class="com.google.inject.util.Types" factory-method="newParameterizedType">
<constructor-arg index="0">
<value type="java.lang.Class">java.util.List</value>
</constructor-arg>
<constructor-arg index="1">
<array><value type="java.lang.Class">java.lang.String</value></array>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
Which is equivelent to:
new TypeToken<List<String>() {}

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