Passing a parameter to a Spring factory bean factory method

I have a Spring bean that is declared as follows:

 <osgi:reference id="basicAuthSecurityHandler" interface="com.groupgti.handler.authentication.basic.Handler"/> <bean id="securityHandler" factory-bean="basicAuthSecurityHandler" factory-method="getSecurityHandler"/> 

My getSecurityHandler method is as follows:

 public ConstraintSecurityHandler getSecurityHandler(String realm) { ConstraintSecurityHandler handler =(ConstraintSecurityHandler) factory.getBean("securityHandler"); handler.setRealmName(realm); return handler; } 

This securityHandler bean is located in the prototype . I need to pass a parameter to the getSecurityHandler method when it was created using spring. Is it possible? I can not find documentation about this.

+4
source share
2 answers

The only way I worked was:

 <osgi:reference id="basicAuthSecurityHandler" interface="com.groupgti.handler.authentication.basic.Handler"/> <bean id="securityHandler" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetObject" ref="basicAuthSecurityHandler"/> <property name="targetMethod" value="getSecurityHandler"/> <property name="arguments"> <list> <value type="java.lang.String">${com.groupgti.esb.targetjobs.indeed.userRealm}</value> </list> </property> </bean> 

I had to use MethodInvokingFactoryBean . I tried using constructor-arg , but then I got an exception that there is no such constructor. Using MethodInvokingFactoryBean everything works fine.

+5
source

In older versions of Spring, this could be done using constructor-arg>. See Docs here . Perhaps you can still do this. I have not tried it!

+1
source

Source: https://habr.com/ru/post/1432242/


All Articles