custom thymeleaf dialect registration causing exception - spring

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> .

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>

Spring DI : session vise object creation using aop proxy for inherited class

In my project i have used struts2 and spring. Spring is used just for DI. I am having all my actions created by sessions and so as model beans via spring DI. Now I want to use inheritance for my class which will be generated through aop based proxy and it will be per session. Coding goes as below.
<bean id="common.EshopDefaultAction" class="com.common.actions.EshopDefaultAction" scope="session" >
<property name="site" ref="master.site" />
<property name="menu" ref="master.menu" />
<property name="indexDAO" ref="common.indexDAO" />
<property name="categoryDAO" ref="master.categoryDAO" />
<property name="productDAO" ref="master.productDAO" />
</bean>
<bean id="common.IndexAction" parent="common.EshopDefaultAction" class="com.common.actions.IndexAction" scope="session">
<property name="indexInfo" ref="common.indexInfo" />
<aop:scoped-proxy />
</bean>
both actions are having pairs of setters and getters.
I want to have site,menu, indexDAO, categoryDAO, productDAO objects created by session for all of its child like IndexAction just shown above. Right now it creates different objects for EshopDefaultAction and IndexAction.
Adding <aop:scoped-proxy /> to EshopDefaultAction bean definition gives error like
Invalid property 'targetBeanName' of bean class [com.common.actions.IndexAction]:
Bean property 'targetBeanName' is not writable or has an invalid setter method.
Does the parameter type of the setter match the return type of the getter?

groovy + Spring add list in different order will throw NotWritablePropertyException

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>

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?

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