How can I define a Spring bean in an xml config file matching this attribute ?
private List<String[]> durations;
I've different things, but none of them seems to work - I'm getting exception from the parser.
Thank you !
You could try a <list> in <list> in the spring application context. A <list> is converted by spring to either a real java.util.List or an array.
<property name="durations">
<list>
<list>
<value>abc</value>
<value>def</value>
</list>
<list>
<value>abc2</value>
<value>def2</value>
</list>
</list>
</property>
Try this:
<bean name="test" class="Test">
<property name="values" value="hugo,emil"/>
</bean>
Spring should carryout the conversion for you if you seperate the values with comma's.
values is a String[] array
Try without generic type : private List durations;
Related
I am new to java AOP. I am supposed to convert the following xml config to java annotation config in my spring boot application. May I know how exactly to convert this xml config to java annotation config:
I think none of the examples that I saw in stackoverflow match the pattern I am trying to convert.
<bean id="xyzRestTemplate"
class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="xyzClientHttpRequestFactory" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbDataMarshaller" />
<property name="unmarshaller" ref="jaxbDataMarshaller" />
</bean>
</list>
</property>
<property name="interceptors">
<list>
<bean class="com.example.XYZHeaderRequestInterceptor" />
</list>
</property>
</bean>
<bean id="jaxbDataMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>com.a.b.c.d.v2</value>
</list>
</property> </bean>
When people talk about converting from XML they don't mean necessarily doing the same thing exactly. What makes Spring Boot attractive isn't just that a configuration is a java class.
You should convert this to use RestTemplate https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-resttemplate.html
Then you just build the RestTemplate using the builder to have the JaxB marshaller and the interceptor you want.
Rest Template - XML Indentation
A nice testcase that passes with XML and passes with #Configuration classes will prove you got it right.
How do I use PropertyPlaceHolderConfigurer in the below when the property to be injected is a list. I know how it works with annotations. Need to understand its working with xml.
<bean id="attributes" class="com.test.SamlAttributes">
<property name="groups">
<list>
<value>group1</value>
</list>
</property>
<property name="attribute">
<list>
<value>value3</value>
</list>
</property>
</bean>
I have the following beans in my Spring config file:
<bean id="myList" class="java.util.ArrayList">
<constructor-arg>
<list>
<ref bean="elem1"/>
<ref bean="elem2"/>
<ref bean="elem3"/>
<ref bean="elem4"/>
</list>
</constructor-arg>
</bean>
<bean id="elem4" class="myClass">
<property name="title" value="random4"/>
</bean>
<bean id="elem1" class="myClass">
<property name="title" value="random1"/>
</bean>
<bean id="elem3" class="myClass">
<property name="title" value="random3"/>
</bean>
<bean id="elem2" class="myClass">
<property name="title" value="random2"/>
</bean>
I have noticed that in my application the elements in myList are in the following order: elem4, elem1, elem3, elem2. I was expecting that the elements in my list will be in the order I set when I declared the ref beans ( elem1, elem2, elem3, elem4).
Is there an order in which Spring initializes beans?
Is there a way I can specify an order for the elements in my list?
The spring does respect the order you gave in the list. The elements in the list would exactly be the [elem1,elem2,elem3,elem4] as you specified. Otherwise, you are doing something wrong, can you show the code which prints you the different order?
The order of bean initialization however may be different and depends on bean dependencies, so, for example if you have two beans
<bean id="holder" class="my.HolderBean" lazy-init="false">
<property name="inner" ref="inner"/>
</bean>
<bean id="inner" class="my.InnerBean" lazy-init="false"/>
Then regardless of the xml definition order, the InnerBean will be initialized first, then during HolderBean initialization it will be injected into HolderBean.
I want to define in my Spring XML context a bean that has a property of the type List of classes: i.e. List<Class<?>> classes
How do I send that bean a number of classes, say java.lang.String and java.lang.Integer?
The list needs not be reusable, i.e. I will not refer to it in another bean.
With Spring, the simplest possibility usually works.....
<property name="classes">
<list>
<value>java.lang.String</value>
<value>java.lang.Integer</value>
</list>
</property>
<property name="classes">
<list>
<bean class="java.lang.Class" factory-method="forName">
<constructor-arg value="java.lang.String"/>
</bean>
</list>
</property>
Something like that...
I need to load some properties into a Spring context from a location that I don't know until the program runs.
So I thought that if I had a PropertyPlaceholderConfigurer with no locations it would read in my.location from the system properties and then I could use that location in a context:property-placeholder
Like this
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
<context:property-placeholder location="${my.location}"/>
but this doesn't work and nor does location="classpath:${my.location}"
Paul
You can do this with a slightly different approach. Here is how we configure it. I load default properties and then overrided them with properties from a configurable location. This works very well for me.
<bean id="propertyPlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="locations">
<list>
<value>classpath:site/properties/default/placeholder.properties
</value>
<value>classpath:site/properties/${env.name}/placeholder.properties
</value>
</list>
</property>
</bean>
The problem here is that you're trying to configure a property place holder using property placeholder syntax :) It's a bit of a chicken-and-egg situation - spring can't resolve your ${my.location} placeholder until it's configured the property-placeholder.
This isn't satisfactory, but you could bodge it by using more explicit syntax:
<bean class="org.springframework.beans.factory.config.PropertyPlaceHolderConfigurer">
<property name="location">
<bean class="java.lang.System" factory-method="getenv">
<constructor-arg value="my.location"/>
</bean>
</property>
</bean>