Skip to content

@Value annotation in Validator triggers Exception when resolving. [SPR-7196] #11855

@spring-projects-issues

Description

@spring-projects-issues

Derek Clarkson opened SPR-7196 and commented

We have written a custom validator and annotation. When we attempt to inject a value provided by a PropertyPlaceholderConfigurer, an exception is triggered. We can successfully inject bean references, which leads us to believe that there is an issue around injecting values. The following are the distilled segments of the code to show the setup and exception.

	...
	<context:annotation-config />
	<mvc:annotation-driven />
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

	...
@Target( { TYPE, ANNOTATION_TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PasswordMatchValidator.class)
@Documented
public @interface PasswordMatch {
	String message() default "{au.edu.open.website.PasswordMatchValidation}";
	Class<?>[] groups() default {};
	Class<? extends Payload>[] payload() default {};
	String field();
}
public class PasswordMatchValidator implements ConstraintValidator<PasswordMatch, Object> {

	// This works and the properties bean is inject on each validation execution.
	@Autowired
	private SystemProperties props;
	
	// This does not work and triggers an exception.
	@Value("$host{file.storage.directory}")
	private String dir;

	public void initialize(final PasswordMatch constraintAnnotation) {
		fieldName = constraintAnnotation.field();
	}
	
	// ... Rest of validator code goes here.

}

Here is the exception that is thrown when validating:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name
		'au.edu.open.sde.validator.PasswordMatchValidator': Injection of autowired dependencies failed; 
		nested exception is org.springframework.beans.factory.BeanCreationException: 
			Could not autowire field: private java.lang.String au.edu.open.sde.validator.PasswordMatchValidator.dir; 
		nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
			No bean named 'au.edu.open.sde.validator.PasswordMatchValidator' is defined
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:286)
	at org.springframework.validation.beanvalidation.SpringConstraintValidatorFactory.getInstance(SpringConstraintValidatorFactory.java:50)
	at org.hibernate.validator.engine.ConstraintTree.createAndInitializeValidator(ConstraintTree.java:192)
	at org.hibernate.validator.engine.ConstraintTree.getInitializedValidator(ConstraintTree.java:171)
	at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:113)
	at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
	at org.hibernate.validator.engine.ValidatorImpl.validateConstraint(ValidatorImpl.java:334)
	at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForRedefinedDefaultGroup(ValidatorImpl.java:278)
	at org.hibernate.validator.engine.ValidatorImpl.validateConstraintsForCurrentGroup(ValidatorImpl.java:260)
	at org.hibernate.validator.engine.ValidatorImpl.validateInContext(ValidatorImpl.java:213)
	at org.hibernate.validator.engine.ValidatorImpl.validate(ValidatorImpl.java:119)
	at org.springframework.validation.beanvalidation.SpringValidatorAdapter.validate(SpringValidatorAdapter.java:74)
	at org.springframework.validation.DataBinder.validate(DataBinder.java:688)
	...

Affects: 3.0.1

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions