I am trying to find the way to pass objects to Spring MethodInvokingFactoryBean arguments list. Here is my Spring configuration:
<bean id="qName" class="javax.xml.namespace.QName">
<constructor-arg index="0" value="${com.groupgti.esb.online.tests.talentq.tns}"/>
<constructor-arg index="1" value="${com.groupgti.esb.online.tests.talentq.serviceName}"/>
</bean>
<bean id="wsdlUrl" class="java.net.URL">
<constructor-arg index="0" value="${com.groupgti.esb.online.tests.talentq.url}"/>
</bean>
<bean id="service" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<bean id="serviceObject" class="com.groupgti.onlinetest.talentq.jaxb.TQIntegrationV2"/>
</property>
<property name="targetMethod">
<value>create</value>
</property>
<property name="arguments">
<list>
<value type="java.net.URL">wsdlUrl</value>
<value type="javax.xml.namespace.QName">qName</value>
</list>
</property>
</bean>
This is not working:
<value type="java.net.URL">wsdlUrl</value>
<value type="javax.xml.namespace.QName">qName</value>
I am getting the exception:
Caused by: org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.net.URL'; nested exception is java.lang.IllegalArgumentException: Could not retrieve URL for OSGi resource[wsdlUrl|bnd.id=573|bnd.sym=com.groupgti.esb.online.tests.talentq]: OSGi resource[wsdlUrl|bnd.id=573|bnd.sym=com.groupgti.esb.online.tests.talentq] cannot be resolved to URL because it does not exist
This is because parameter is passed as String, just wsdlUrl and not as an java.net.URL object.
I have also tried this:
<property name="arguments">
<ref bean="wsdlUrl"/>
<ref bean="qName"/>
</property>
This gives me an exception that ref attribute does not belong here. So how then should I pass an object to arguments list?
Found a solution. I had to add <list> and then declare <ref>:
<property name="arguments">
<list>
<ref bean="wsdlUrl"/>
<ref bean="qName"/>
</list>
</property>
Like this, everything is working.
Related
I have aspring batch job that use an StaxeventItemWriter , it's works fine . I want the result Xml file to be formatted , how can I do that . is there any property like ?
this is my ItemWriter , I put only the relevant part of my code to simplify , if needed I can add the hole job.thanks
<bean id="xmlWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
<property name="resource" value="file:src/main/resources/myFile.xml"></property>
<property name="rootTagName" value="Users"></property><bean id="MyMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>model.myModel</value>
</list>
</property>
</bean>
<property name="marshaller" ref="MyMarshaller"></property>
</bean>
<bean id="MyMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>model.myModel</value>
</list>
</property>
</bean>
You can activate the JAXB_FORMATTED_OUTPUT option on the marshaller with:
<bean id="MyMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>model.myModel</value>
</list>
</property>
<property name="marshallerProperties">
<map>
<entry>
<key>
<util:constant static-field="javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT" />
</key>
<value type="java.lang.Boolean">true</value>
</entry>
</map>
</property>
</bean>
I have the problem with resolve spring's "classpath:" feature. I a have my file in the next path:
src/main/java/resources/FederationMetadata.xml
Also, I have a bean:
<bean id="metadata" class="org.springframework.security.saml.metadata.CachingMetadataManager">
<constructor-arg>
<list>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadataDelegate">
<constructor-arg>
<bean class="org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider">
<constructor-arg>
<value type="java.io.File">classpath:FederationMetadata.xml</value>
</constructor-arg>
<property name="parserPool" ref="parserPool"/>
</bean>
</constructor-arg>
<constructor-arg>
<bean class="org.springframework.security.saml.metadata.ExtendedMetadata">
</bean>
</constructor-arg>
</bean>
</list>
</constructor-arg>
</bean>
So my problem is that application fails with the next exception:
D:\myFolder\myProject\classpath:FederationMetadata.xml does not exist
As I understand Spring doesn't resolve file location. I tried with classpath*:, and it didn't help. In another project I have the same settings (with "classpath:") and it works fine. What it can be?
This issue was driving me crazy as well.
At some point I realized that FilesystemMetadataProvider was replaced with ResourceBackedMetadataProvider.
So now if you have your Metadata XML inside of the classpath, do something like this:
<bean id="metadata" class="org.springframework.security.saml.metadata.CachingMetadataManager">
<constructor-arg>
<list>
<bean class="org.opensaml.saml2.metadata.provider.ResourceBackedMetadataProvider">
<constructor-arg>
<bean class="java.util.Timer"/>
</constructor-arg>
<constructor-arg>
<bean class="org.opensaml.util.resource.ClasspathResource">
<constructor-arg value="/FederationMetadata.xml"/>
</bean>
</constructor-arg>
<property name="parserPool" ref="parserPool"/>
</bean>
</list>
</constructor-arg>
</bean>
Try
<value type="java.io.File">file:///FederationMetadata.xml</value>
instead of
<value type="java.io.File">classpath:FederationMetadata.xml</value>
If you want it accessed from class path you need to put it inside
WEB-INF/classes folder.
the path is relative to classes folder ie if the file is inside
WEB-INF/classes/configs/somefile.xml
then
classpath:configs/somefile.xml
I stuck with this problem.
Used components: Spring 1.2.8, Hibernate 3.2.0 cr1, tomcat, struts, java 6
I am trying to get bean from ProxyFactoryBean with scope = prototype. I am not successful. I have no clue what is wrong.
Here is context:
<beans>
<bean id="ruleCheckTask" class="rulechecker.RuleCheckTask" singleton="false">
<bean id="ruleCheckTaskPrototype" class="org.springframework.aop.target.PrototypeTargetSource">
<property name="targetBeanName" value="ruleCheckTask" />
</bean>
<bean id="transactionInterceptorRuleCheckTask" class="org.springframework.transaction.interceptor .TransactionInterceptor">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributeSource">
<value>
rulechecker.IRuleCheckTask.run=PROPAGATION_REQUIRE S_NEW
</value>
</property>
</bean>
<bean id="ruleCheckTaskService" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="target" ref="ruleCheckTaskPrototype" />
<property name="proxyInterfaces">
<value>
rulechecker.IRuleCheckTask
</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptorRuleCheckTask</value>
</list>
</property>
</bean>
</beans>
In code when I do following:
...................
...................
IRuleCheckTask checkTask = (IRuleCheckTask) applicationContext.getBean("ruleCheckTaskService") ;
checkTask.setTestCase(oneTestCase);
I got following exception when trying call setTestCase on checkTask bean:
java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoi npointUsingReflection(AopUtils.java:287)
at org.springframework.aop.framework.ReflectiveMethod Invocation.invokeJoinpoint(ReflectiveMethodInvocat ion.java:181)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :148)
at org.springframework.transaction.interceptor.Transa ctionInterceptor.invoke(TransactionInterceptor.jav a:96)
at org.springframework.aop.framework.ReflectiveMethod Invocation.proceed(ReflectiveMethodInvocation.java :170)
at org.springframework.aop.framework.JdkDynamicAopPro xy.invoke(JdkDynamicAopProxy.java:176)
at $Proxy21.setTestCase(Unknown Source)
It was working if in ProxyFactoryBean I used ruleCheckTask instead ruleCheckTaskPrototype. Problem is that in that case I always obtain singleton of ruleCheckTask. And I need always new instance.
One small thing RuleCheckTask implements Runnable interface.
Could anybody give me a hint?
Thank you
try with:
<bean id="ruleCheckTaskService" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="targetName" value="ruleCheckTask" />
<property name="singleton" value="false" /> <!-- this do the trick -->
<property name="proxyInterfaces">
<value>
rulechecker.IRuleCheckTask
</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptorRuleCheckTask</value>
</list>
</property>
</bean>
You could also set targetSource (no target) to ruleCheckTaskPrototype instead. The diference is that on the first one, you have an independient instance of the proxy configuration, on the second one, the PrototypeTargetSource gets a new instance on each request.
I have a list as follows:
ArrayList<DiameterMessageHandler> handlers = new ArrayList<>();
handlers.add(new AARHandler());
handlers.add(new CERHandler());
handlers.add(new PPAHandler());
handlers.add(new STRHandler());
handlers.add(new DWRHandler());
I am wondering how to create a spring bean that takes handlers as one of its arguments, i.e. is it possible to do this in the applicationContext.xml - Do I have to create separate beans for the list and each of the handlers(AARHandler etc) first? Here is my applicationContext.xml
<bean id="DiameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start">
<constructor-arg type="java.lang.String" index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1">WHAT GOES HERE?</constructor-arg>
</bean>
Probably you want all these handlers be Spring beans too. This is the configuration:
<bean id="DiameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" init-method="start">
<constructor-arg value="${pcca.host}" />
<constructor-arg>
<list>
<ref bean="aarHandler" />
...
</list>
</constructor-arg>
</bean>
<bean id="aarHandler" class="com.rory.ptspsim.diameterclient.AARHandler" />
I think the most appropriate way to do that is:
<bean id="DiameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start">
<constructor-arg type="java.lang.String" index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1">
<list>
<ref bean="aarHandler" />
<ref bean="cerHandler" />
<ref bean="ppaHandler" />
<ref bean="strHandler" />
<ref bean="dwrHandler" />
</list>
</constructor>
</bean>
If you want all available Handlers, Spring will also collect them for you via Autowiring:
public DiameterClient(#Autowired List<DiameterMessageHandler> handlers){
this.handlers = handlers;
}
Now Spring will inject a List of all available Handlers.
See Spring Reference 4.9.2: #Autowired
<bean id="DiameterClient" class="com.rory.ptspsim.diameterclient.DiameterClient" scope="singleton" init-method="start">
<constructor-arg type="java.lang.String" index="0"><value>${pcca.host}</value></constructor-arg>
<constructor-arg index="1">
<list>
<bean class="AARHandler"/>
<bean class="CERHandler"/>
</list>
</constructor-arg>
</bean>
<list>
<ref bean="handler1" />
<ref bean="handler2" />
<ref bean="handler3" />
<ref bean="handler4" />
<ref bean="handler5" />
</list>
<bean id="handler1" class="AARHandler"/>
<bean id="handler2" class="CERHandler"/>
<bean id="handler3" class="PPAHandler"/>
<bean id="handler4" class="STRHandler"/>
<bean id="handler5" class="DWRHandler"/>
I'm trying to create an array of objects in a Spring context file so I can inject it to a constructor that's declared like this:
public RandomGeocodingService(GeocodingService... services) { }
I'm trying to use the <array> tag:
<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
<constructor-arg ref="proxy" />
<constructor-arg value="" />
</bean>
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<array value-type="geocoding.GeocodingService">
<!-- How do I reference the google geocoding service here? -->
</array>
</constructor-arg>
</bean>
I haven't been able to find an example or something in the in the documentation on how to do this. Also, you have any suggestions for a better way of acheiving what I'm trying to do, please let me know :).
That's because there's no such thing as <array>, there's only <list>.
The good news is that Spring will auto-convert between lists and arrays as required, so defined your array as a <list>, and Spring will be coerce it into an array for you.
This should work:
<bean id="googleGeocodingService" class="geocoding.GoogleGeocodingService">
<constructor-arg ref="proxy" />
<constructor-arg value="" />
</bean>
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<list>
<ref bean="googleGeocodingService"/>
</list>
</constructor-arg>
</bean>
Spring will also coerce a single bean into a list, if required:
<bean id="geocodingService" class="geocoding.RandomGeocodingService">
<constructor-arg>
<ref bean="googleGeocodingService"/>
</constructor-arg>
</bean>
Spring can automatically convert a list into an array[]
check it out http://forum.springsource.org/showthread.php?37767-Injecting-String-Array
<bean name="test" class="Test">
<property name="values" value="hugo,emil"></property>
</bean>
Check out the util schema.
I'm actually using <array> tag to inject an array of objects into a bean and it works.
Take a look at the following code...
<bean id="song1" class="mx.com.company.songs.Song">
<property name="name" value="Have you ever seen the rain?"/>
</bean>
<bean id="song2" class="mx.com.company.songs.Song">
<property name="name" value="La bamba"/>
</bean>
<bean id="guitarPlayer" class="mx.com.company.musician.GuitarPlayer">
<property name="songs">
<array>
<ref bean="song1"/>
<ref bean="song2"/>
</array>
</property>
</bean>