• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Autowiring does not work inside custom constraint validator using Spring-Test-MVC

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Eveyone,

I have posted this in the Spring forums but have not had a reply. Can anyone help with this please..?

I have an issue with autowiring inside a custom constraint validator when running an integration test using spring-test-mvc. The service I require is null.

However when I run the web app, its is auotwired correctly.

Custom Constraint Validator (Booking Service is null!)



Annotation


Service


Unit Test



Config



Controller


 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,

With Thanks to Rossen Stoyanchev I have this working.

In short the problem is to do with standloneSetup with does not load spring configuration.

One option is to create a LocalValidatorFactoryBean configured with a SpringConstraintValidatorFactory and plug it into the standaloneSetup builder.

However I decided to migrate from spring-test-mvc to spring-test in 3.2.

All that was required was to annotate my test class with @WebAppConfiguration, load in my spring configuration files with @ContextConfiguration and then @Autowired WebApplicationContext

Example




I can now @Autowire services in my custom jsr annotation validator.

Hope this helps others

Regards
Darren C.


 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We also faced the similar problem where @Autowiring was failing (not initialised) in ConstrainValidator Class. Our ConstraintValidator Implemented class was using a value which supposed to be read from the `application.yml` file.  Below solution helped us as this is using a pure spring scope. Hope this helps, with proper SpringJunit4ClassRunner.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it. There should have been no issue with using @Autowired inside a Constraint validator class. This means that something is wrong.

This issue has been reported on various platform and I have seen some workaround. But I wanted to know how to solve it directly.

Here is what I found.
The problem was the incorrect use of the of class being validated. You may notice that the validation is happening twice. the first time it should work but the second time you got @Autowired object is null.

The problem was that the entity or the class being validated was being used twice in the controller. For example, you may want validate the entity class and try to save it at the same time in the same method in the controller method. When you try to save the entity, it will try to validate the object again and this time the @Autowired object will be null.

Here is what you can do for this scenario

You can use create a dto class of the entity class(meaning that the entity and the dto class will have the same field). Relocate the annotation validation from the entity class to the dto class.Then copy the properties of the entity class unto your your entity class and save your entity. This way the validation will happen once and the null error message should go away. Your scenario may be different but the approach should be the same.


Below is an example of code that works
 
Georgy Louis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about it[/b]. There should have been no issue with using @Autowired inside a Constraint validator class. This means that something is wrong.

This issue has been reported on various platform and I have seen some workaround. But I wanted to know how to solve it directly.

Here is what I found.
The problem was the incorrect use of the of class being validated. You may notice that the validation is happening twice. the first time it should work but the second time you got @Autowired object is null.

The problem was that the entity or the class being validated was being used twice in the controller. For example, you may want validate the entity class and try to save it at the same time in the same method in the controller method. When you try to save the entity, it will try to validate the object again and this time the @Autowired object will be null.

Here is what you can do for this scenario

You can use create a dto class of the entity class(meaning that the entity and the dto class will have the same field). Relocate the annotation validation from the entity class to the dto class.Then copy the properties of the entity class unto your your entity class and save your entity. This way the validation will happen once and the null error message should go away. Your scenario may be different but the approach should be the same.


Below is an example of code that works

 
reply
    Bookmark Topic Watch Topic
  • New Topic