I’m a new Spring user, and probably doing something wrong (due to a misunderstanding of the concepts/internals of Spring IOC), so hopefully this question will result in a simple answer.
Here’s what I’m trying to do: I’m trying to use two beans from two different 3rd party libraries:
<bean id="validator" class="org.owasp.esapi.ESAPI" factory-method="validator"/>
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>
The problem is that they both have the same Id, and that is not allowed. I’m trying to auto-wire the ESAPI validator, and the LocalValidatorFactoryBean is used by spring if I followed correctly.
So my simplified class for using the ESAPI validator would be:
public class ValidatedString {
@Autowired(required=true)
Validator stringValidator;
public void doSomethingWithTheValidator() {
// do something
}
}
But what I can’t understand is how I can change the Id. Both return a Validator class, from a different package, and I was under the impression that the id field needs to be the same as the class name.
Help??
Have you tried changing the
id‘s? Both theidandnameattributes are up to yourself, there is no requirement that they be the same as the class name, it’s just a bit easier to figure out what the beans “are” that way.When beans are
@Autowired, Spring will use the best match based on either name, type (default as I recall) or constructor and so if yourValidatorin theValidatedStringclass is anorg.owasp.esapi.ESAPIthen that’s what will be injected.Cheers,