Spring - merge lists - spring

I have a factory bean that produces List. But I call this factory multiple times meaning I end up with a lot of beans of type List.
I have another bean that has a property of type List. Somehow I need to put all the List beans in to one single list and provide it as a property using Spring XML.
How do I do this?
<bean id="usefulBean" class="...">
<property name="listProperty" ref="listX1AndX2AndX3AndSoOn">
</bean>
<bean id="listX1" factory-bean="anInstanceFactory" factory-method="create">
...
</bean>
<bean id="listX2" factory-bean="anInstanceFactory" factory-method="create">
...
</bean>
<bean id="listX3" factory-bean="anInstanceFactory" factory-method="create">
...
</bean>

Related

Dependency Injection returned results of bean init-method

I have two simple beans. In the first bean it calls a init-method and return string value.
Now I want to this returned string from first bean init-method , inject to my second bean
helloWorldBean3 property newKey. Please advise me on how to implement this requirement.
<bean id="helloWorldBean2" init-method="loadKey"
class="com.java.snippets.enterprise.services.HelloWorld2">
<property name="key" value="${key.supportiveFile}" />
<bean id="helloWorldBean3"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="newKey" ref="???" />
</bean>
Try using Spring EL like so:
<bean id="helloWorldBean3"
class="com.java.snippets.enterprise.services.HelloWorld">
<property name="newKey" value=""#{helloWorldBean2.loadKey()}"" />
</bean>

How would I specify a bean reference as an argument-resolver, in Spring 3-1?

This is a syntax question about a Spring configuration (refer to spring-beans-3.1.xsd and spring-mvc-3.1.xsd).
I have a specific bean definition (id="SecurityRequestParametersArgumentResolver") that I want to register as a custom argument resolver. The xml fragment is:
<bean id="SecurityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<property name="credentialsManager" ref="CredentialsManager" />
<property name="tokenService" ref="TokenService" />
</bean>
... AND I would like to use a bean reference. The following three lines don't obey the xsd-grammar ( what should be the correct tag declaration here? )
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean ref="SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
... All of the examples I have seen look like THIS, and thus are going after the default no-argument constructor
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver"/>
</mvc:argument-resolvers>
</mvc:annotation-driven>
Repeating the question, what should the Spring syntax be in order to use a bean reference as a custom argument resolver?
Thanks!
At least the constructor argument problem can be solved easily
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class="class="xxx.security.web.SecurityRequestParametersArgumentResolver">
<constructor-arg value="123"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>
But I do not know any way to use a reference instead of a bean within <mvc:argument-resolvers>. But depending on what you want to do, there are several workarrounds.
Use a Bean Factory
Create a proxy class that implements the HandlerMethodArgumentResolver and forward every call to an other instance, then you can have your own SecurityRequestParametersArgumentResolver like a normal bean and register the Proxy in the <mvc:argument-resolvers> section.
Proxy:
class HandlerMethodArgumentResolverProxy
implements HandlerMethodArgumentResolver{
HandlerMethodArgumentResolver delegate;
publicHandlerMethodArgumentResolverProxy
(HandlerMethodArgumentResolver delegate){
this.delegate=delegate;
}
public boolean supportsParameter(MethodParameter parameter) {
this.delegate.supportsParameter(parameter);
}
//delegate for resolveArgument
}
config:
<bean id="securityRequestParametersArgumentResolver"
class="xxx.security.web.SecurityRequestParametersArgumentResolver">
...
</bean>
<mvc:annotation-driven>
<mvc:argument-resolvers>
<bean class "HandlerMethodArgumentResolverProxy">
<constructor-arg
ref="securityRequestParametersArgumentResolver"/>
</bean>
</mvc:argument-resolvers>
</mvc:annotation-driven>

spring: how to omit bean-name in a list?

Do I have to give ids to the elements of a list of Bars ?
<list value-type="foo.Bar">
<bean p:p1="someP1Value" p:p2="aP2Value" />
</list>
I get
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unnamed bean definition specifies neither 'class' nor 'parent'
nor 'factory-bean' - can't generate bean name
for this.
How can I omit the bean ids?
Well, the error message is pretty clear. When defining a <bean/> you must either specify class or parent attribute:
<list>
<bean class="foo.Bar" p:p1="someP1ValueA" p:p2="aP2ValueA"/>
<bean class="foo.Bar" p:p1="someP1ValueB" p:p2="aP2ValueB"/>
<bean class="foo.BarSubclass" p:p1="someP1ValueC" p:p2="aP2ValueC"/>
</list>
If you want to avoid exceesive use of class attribute, you can take advantage of parent feature:
<bean id="bar" abstract="true" class="foo.Bar"/>
<list value-type="foo.Bar">
<bean parent="bar" p:p1="someP1ValueA" p:p2="aP2ValueA"/>
<bean parent="bar" p:p1="someP1ValueB" p:p2="aP2ValueB"/>
<bean parent="bar" p:p1="someP1ValueC" p:p2="aP2ValueC" class="foo.BarSubclass"/>
</list>
What are the p:p1 and p:p2 namespaces?

Spring - Retrieve value from properties file

I have the following configuration in my applicationContext.xml:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:app.properties</value>
</list>
</property>
</bean>
Now, in my java class, how can I read the values from the file app.properties?
With Spring 3.0 you can use the #Value annotation.
#Component
class MyComponent {
#Value("${valueKey}")
private String valueFromPropertyFile;
}
Actually PropertyPlaceholderConfigurer is useful to inject values to spring context using properties.
Example XML context definition:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>${driver}</value></property>
<property name="url"><value>jdbc:${dbname}</value></property>
</bean>`
Example properties file:
driver=com.mysql.jdbc.Driver
dbname=mysql:mydb
Or you can create bean like
<bean name="myBean" value="${some.property.key}" />
and then inject this bean into your class

How do you acess a property of a bean for reading in a spring xml config file?

I want to do something like the following in spring:
<beans>
...
<bean id="bean1" ... />
<bean id="bean2">
<property name="propName" value="bean1.foo" />
...
I would think that this would access the getFoo() method of bean1 and call the setPropName() method of bean2, but this doesn't seem to work.
What I understood:
You have a bean (bean1) with a
property called "foo"
You have another bean (bean2) with a
property named "propName", wich also
has to have the same "foo" that in
bean1.
why not doing this:
<beans>
...
<bean id="foo" class="foopackage.foo"/>
<bean id="bean1" class="foopackage.bean1">
<property name="foo" ref="foo"/>
</bean>
<bean id="bean2" class="foopackage.bean2">
<property name="propName" ref="foo"/>
</bean>
....
</beans>
Doing this, your bean2 is not coupled to bean1 like in your example. You can change bean1 and bean2 without affecting each other.
If you REALLY need to do the injection you proposed, you can use:
<util:property-path id="propName" path="bean1.foo"/>
You need to use PropertyPathFactoryBean:
<bean id="bean2" depends-on="bean1">
<property name="propName">
<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
<property name="targetBeanName" value="bean1"/>
<property name="propertyPath" value="foo"/>
</bean>
</property>
</bean>
I think you have to inject bean1, then get foo manually because of a timing issue. When does the framework resolve the value of the target bean?
You could create a pointer bean and configure that.
class SpringRef {
private String targetProperty;
private Object targetBean;
//getters/setters
public Object getValue() {
//resolve the value of the targetProperty on targetBean.
}
}
Common-BeanUtils should be helpful.

Resources