I've created a few beans for validation of an object:
<!-- RES rules engine -->
<bean id="rules-execution-server-engine"
class="util.res.RuleEngineRESJSE">
<constructor-arg index="0" value="util.res.rulesengine.log" />
</bean>
<bean id="rio-object" class="UROImpl">
</bean>
<bean id="trade-validator-context"
class="rule.trade.TradeValidationContext">
<constructor-arg index="0" ref="rio-object" />
</bean>
<bean id="trade-validator"
class="validator.RESTradeValidator">
<constructor-arg index="0" ref="trade-validator-context" />
<constructor-arg index="1" ref="validation-rules-helper" />
</bean>
<bean id="validation-rules-helper"
class="util.res.RESRulesHelperImpl">
<constructor-arg index="0" value="rule.traderules.loc" />
<constructor-arg index="1" ref="rules-execution-server-engine" />
</bean>
I can call and create them through a main method sucessfully.
However I am now trying to integrate this code within my other modules.
The trade-validator bean implements a interface which looks like:
public interface IValidator {
public Object validate(Object obj) throws ValidationException;
}
I am hoping to call this via the interface:
validator.validate(rioObject);
However this is causing issues as I cannot instanate the interface and from the XML you can see the actual requires two other beans for it's constructor. I've been looking at the autowired approach. But am still getting to grips with it.
Is there a way to point it towards the bean as being the validator to use? As atm the validator is null.
My recommendation is to avoid using autowire for bigger deployments and wire the beans yourselves.
BTW, why are you not able to instantiate the trade validator ? What is the error that you are getting?
However this is causing issues as I cannot instanate the interface and
from the XML
Related
Hi All in my Spring application i have used AutoWired NamedParameterJdbcTemplate.
#Autowired
NamedParameterJdbcTemplate namedParametersJdbcTemplate;
in my rest-servlet.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/mylfrdb"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" id="namedParameterJdbcTemplate">
<constructor-arg ref="jdbcTemplate"></constructor-arg>
</bean>
<bean class="org.springframework.jdbc.core.simple.SimpleJdbcCall" id="simpleJdbcCall">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
It working fine.
No i have to use performance intercepter with Spring AOP.
So i added following thing in my rest-servlet.xml
<aop:config >
<aop:pointcut expression="#target(org.springframework.stereotype.Service)" id="allServices"/>
<aop:advisor pointcut-ref="allServices" advice-ref="perfMonitor"/>
</aop:config>
So i got error like this.
Can not set org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate field com.lfr.dao.impl.FlatAdvertiseDaoImpl.namedParametersJdbcTemplate to com.sun.proxy.$Proxy15
So i refered this question and tried to implement 2nd solution give is by using CGLIB and
<aop:config proxy-target-class="true" >
No i am getting this error
Could not generate CGLIB subclass of class [class org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Superclass has no null constructors but no arguments were given
I had the exact same error message. I was using Spring 3.2.5.RELEASE version. After debugging and trying to repeat the problem with PetClinic example came out it was matter of Spring version. This problem didn't occur in Spring 4.1.1. Try to upgrade, maybe it works.
As part of learning spring, I'm trying to declare a priority queue bean of MyMessageObject objects, but keep getting the following BeanCreationException: "Error creating bean with name 'messagesHeap' defined in URL [file:/my.app.spring.xml]: Could not resolve matching constructor"
Here's my spring definitions:
<bean id="messagesHeap" class="java.util.PriorityQueue">
<constructor-arg type="int" name="initialCapacity" value="100" index="0"/>
<constructor-arg name="comparator" type="java.util.Comparator" index="1" ref="orderComparator"/>
</bean>
<bean id="orderComparator" class="com.my.myComparator"/>
The class myComparator implements java.util.Comparator as required.
What am I doing wrong here?
try this
<bean id="messagesHeap" class="java.util.PriorityQueue">
<constructor-arg value="100" />
<constructor-arg ref="orderComparator" />
</bean>
I'm migrating my project's spring XML configuration to #Configuration class and I'm trying to figure out how should I handle bean tags with abstract="true" property. Something like:
<bean id="factoryTemplate" class="com.template.FactoryTemplate" abstract="true">
<constructor-arg index="1" ref="resizerFactory"/>
<constructor-arg index="2" ref="metricFactory"/>
<constructor-arg index="3" ref="settingsDao"/>
</bean>
Clarification: I can't change the XML where the abstract bean is defined because I'm part of a larger project and I'm changing only my modules.
Is there a methodology on how should I do such migration?
XML:
<bean id="myBean" class="com.google.common.collect.HashMultimap">
<constructor-arg index="0" value="200"/>
<constructor-arg index="1" value="2"/>
</bean>
Is it correct? When I try to inject this bean in another like:
<property name="myBean" ref="myBean"/>
i received the error: Cannot apply property values to null instance
try this
<bean id="myBean" factory-method="create" class="com.google.common.collect.HashMultimap">
<constructor-arg index="0" value="200" />
<constructor-arg index="1" value="2" />
</bean>
HashMultimap does not have a public constructor, therefore it's neither a Bean nor a POJO.
Source: google collections - HashMultimap
Instances are created using the static factory methods, among them one with no arguments (create()), maybe you can use this?
Suppose I have my own bean which contains another beans hardcoded.
How to join this with Spring configuration?
First way is to use factory-method:
<bean id="bean1" class="myClass1"/>
<bean id="bean1.member" factory-bean="bean1" factory-method="getMember"/>
<bean id="bean2" class="myClass2">
<property name="collaborator" ref="bean1.member"/>
</bean>
Another way is to use EL:
<bean id="bean1" class="myClass1"/>
<bean id="bean2" class="myClass2">
<property name="collaborator" value="#{bean1.member}"/>
</bean>
In latter case Spring does not realize the dependency. Anyway, Bean Graph in Eclipse displays beans unrelated.
Are there better ways? May be I may implement some interface with MyClass1 so that it will treated as container or context?
You should create a separate bean for bean1.member and inject it into bean1
<bean id="bean3previouslyMember" class="myCompoundBean"/>
<bean id="bean1" class="myClass1">
<property name="member" ref="bean3previouslyMember"/>
</bean>
<bean id="bean2" class="myClass2">
<property name="collaborator" ref="bean3previouslyMember"/>
</bean>
Unless myClass1 is not modifiable and has no setter, this is generally what we do.