groovy + Spring add list in different order will throw NotWritablePropertyException - spring

I am trying to integrate Groovy with Spring.
In one bean I would need to put in a list as property
However that only work if I define the item in list before the bean definition.
To avoid future errors...I would like to ask what is the mechanism behind compiling the context and causing the error?
The Item.groovy is a simple bean with field String itemName
My config is like
Not-working
<lang:groovy id="handler" script-source="ItemHandlerImpl.groovy">
<lang:property name="itemList">
<list>
<lang:groovy id="item1" script-source="Item.groovy">
<lang:property name="itemName" value="name1" />
</lang:groovy>
</list>
</lang:property>
</lang:groovy>
This throws
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'itemName' of bean class [org.springframework.scripting.groovy.GroovyScriptFactory]: Bean property 'itemName' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 70 more
After many attempts, this is my working config
<lang:groovy id="item1" script-source="Item.groovy">
<lang:property name="itemName" value="name1" />
</lang:groovy>
<lang:groovy id="handler" script-source="ItemHandlerImpl.groovy">
<lang:property name="itemList">
<list>
<ref bean ="item1">
</list>
</lang:property>
</lang:groovy>

Related

create a bean from jobparameter in spring batch

I want to a create bean with some of job parameters. I found one thread here
but that seems to unsolved and was an old post.
<bean id="context" class="com.test.MyConfig" scope="step" >
<property name="toDate" value="#{jobParameters['toDate']}" />
<property name="fromDate" value="#{jobParameters['fromDate']}" />
</bean>
<bean id="testId" class="com.test.SomeClass" scope="step" >
<property name="context" ref="context" />
</bean>
And setter attribute is like below And MyConfig does not implement any interface.
MyConfig config;
public void setConfig(MyConfig config)
{
this.config = config
}
I get an exception
Error creating bean with name 'lazybidingProxy.anotherBean' defined in
class path [resource/config/spring-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportException: Failed to convert value of type 'com.sun.proxy.$Proxy17 implementing org.springframework.beans.factory.Initializing,java.io.Serializable,org.springframework.aop.scope.ScopedObject.....
to required type[package.someClass]: no matching editors or conversion strategy found]

How do I inject a Class[] array in spring?

I've got a class which has a static method newInstance(Class[] classes, List properties), and I need Spring to be able to instantiate instances using this class.
What i've got so far in my XML configuration is :
<bean id="jaxbContext" scope="prototype" class="javax.xml.bind.JAXBContext" factory-method="newInstance">
<constructor-arg>
<list>
<value type=">com.company.pas.entity.Partner.class</value>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>com/company/pas/entity/mapping/partner-pojo2xml.xml</value>
</list>
</constructor-arg>
</bean>
This throws the below exception. Note that it complains that it can't find the newInstance method, but i assume that is because it can't find a newInstance method with a signature which matches the configuration. I suspect that Spring interprets the list of classes as java.lang.String, and not Class.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jaxbContext' defined in class path resource [com/company/pas/context/mappingContext.xml]: No matching factory method found: factory method 'newInstance'. Check that a method of the specified name exists and that it is static.
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:500)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:964)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:870)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
... 46 more
How do I get spring to inject an array of Class[] based on a configuration file?
JAXBContext.newInstance() has this overload:
newInstance(String contextPath)
newInstance(String contextPath,ClassLoader classLoader)
newInstance(Class... classesToBeBound)
newInstance(Class[] classesToBeBound,Map<String,?> properties)
newInstance(String contextPath,ClassLoader classLoader,Map<String,?> properties)
To be honest I can't find a newInstance(Class[] classes, List properties) overload so spring is right when tells you it can't find the newInstance() method

custom thymeleaf dialect registration causing exception

in applicationContext.xml
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
<property name="additionalDialects">
<set>
<bean class="org.test.custom.CustomDialact" />
</set>
</property>
</bean>
Stacktrace:
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'additionalDialects' of bean class [org.thymeleaf.spring3.SpringTemplateEngine]: Bean property 'additionalDialects' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1042)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:902)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:57)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1424)
... 50 more
i don't know but it was fixed after restarting machine. but mean while i find another solution
by extend a custom class with HashSet and in constructor add custom dialect in it and declare it in xml. provide it reference to additionalDialects . thats it .it will start running . it think it is problem related to generics as spring sometimes supply empty type
like HashSet<V> .

spring bean initializing instances differently via property wiring

I have the following properties in a property file:
context1.property1=value1
context1.property2=value2
context1.property3=value3
context2.property1=value4
context2.property2=value5
context2.property3=value6
I have a bean with the following structure:
class Bean {
private property1;
private property2;
private property3;
}
Is there any way better to initialize 2 instances of Bean without writing something like:
<bean id="bean1" class="com.test.Bean">
<property name="property1" value="${context1.value1}" />
<property name="property2" value="${context1.value2}" />
<property name="property3" value="${context1.value3}" />
</bean>
<bean id="bean2" class="com.test.Bean">
<property name="property1" value="${context2.value1}" />
<property name="property2" value="${context2.value2}" />
<property name="property3" value="${context2.value3}" />
</bean>
Thanks!
Have a look at PropertyOverrideConfigurer:
Property resource configurer that overrides bean property values in an application context definition. It pushes values from a properties file into bean definitions.
Configuration lines are expected to be of the following form:
beanName.property=value
Example properties file:
dataSource.driverClassName=com.mysql.jdbc.Driver
dataSource.url=jdbc:mysql:mydb
See also
Using PropertyOverrideConfigurer with Annotated Classes in Spring 3

BeanInstantiationException:Cannot convert type from java.lang.String to required type --- idref tag

I am new to spring and trying to understand the functionality of the idref tag
My config file is pasted here:
<bean id="AudioSystembean" class="com.springexample.AudioSystem">
<property name="price" value="200"/>
</bean>
<bean id="Vehicle1" class="com.SpringExample.Vehicle1">
<property name="vehicleType" value="CAR" />
<property name="audioSystem" >
<idref bean = "AudioSystembean" />
</property>
</bean>
When I am executing the code I am getting the error as : "BeanInstantiationException:Cannot convert type from java.lang.String to required type :com.SpringExampl.AudioSystem" [I dont remember the exact exception - I have the code at home]
If I use ref instead of idref it is working fine.
I tried google to understand about idref but could not get much information..
What am I doing wrong here?
In a Spring context, <idref> is used to pass the name of the referenced bean, rather than the bean itself (which is what <ref> does). So in your example:
<property name="audioSystem" >
<idref bean = "AudioSystembean" />
</property>
The audioSystem system property will be injected with the name of the AudioSystembean, i.e. the String "AudioSystembean".
This is not what you need here, though, you need the <ref> element, which passes a reference to the bean itself.

Resources