What does the @Valid annotation indicate in Spring?

It’s for validation purposes. Validation It is common to validate a model after binding user input to it. Spring 3 provides support for declarative validation with JSR-303. This support is enabled automatically if a JSR-303 provider, such as Hibernate Validator, is present on your classpath. When enabled, you can trigger validation simply by annotating a … Read more

Error creating bean with name ‘org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0’ defined in ServletContext resource

This is a limitation in Spring AOP. When you use AspectJ pointcuts to weave aspects into beans, Spring will use CGLIB to generate a subclass of the target, and invoke the aspects from that subclass. If the target class does not have a public default constructor, however, this will fail. CGLIB does have the ability … Read more

Spring Boot : Custom Validation in Request Params

Case 1: If the annotation ValuesAllowed is not triggered at all, it could be because of not annotating the controller with @Validated. @Validated @ValuesAllowed(propName = “orderBy”, values = { “OpportunityCount”, “OpportunityPublishedCount”, “ApplicationCount”, “ApplicationsApprovedCount” }) public class OpportunityController { @GetMapping(“/vendors/list”) public String getVendorpage(@RequestParam(required = false) String term,..{ } Case 2: If it is triggered and throwing … Read more

why do we have to use @Modifying annotation for queries in Data Jpa

CAUTION! Using @Modifying(clearAutomatically=true) will drop any pending updates on the managed entitites in the persistence context spring states the following : Doing so triggers the query annotated to the method as an updating query instead of a selecting one. As the EntityManager might contain outdated entities after the execution of the modifying query, we do … Read more

How to register Spring @Configuration annotated class instead of applicationContext.xml file in web.xml?

In web.xml you need to bootstrap the context with AnnotationConfigWebApplicationContext: <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.AnnotationConfigWebApplicationContext </param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value> org.package.YouConfigurationAnnotatedClass </param-value> </init-param> </servlet> And don’t forget to use @EnableWebMvc for your MVC annotations to kick in. further reading: Spring 3.1 MVC Enhancements Spring 3.1 MVC Namespace Enhancements And Configuration EDIT as a … Read more

How to Autowire Bean of generic type in Spring?

Simple solution is to upgrade to Spring 4.0 as it will automatically consider generics as a form of @Qualifier, as below: @Autowired private Item<String> strItem; // Injects the stringItem bean @Autowired private Item<Integer> intItem; // Injects the integerItem bean Infact, you can even autowire nested generics when injecting into a list, as below: // Inject … Read more

Is there a way to @Autowire a bean that requires constructor arguments?

You need the @Value annotation. A common use case is to assign default field values using “#{systemProperties.myProp}” style expressions. public class SimpleMovieLister { private MovieFinder movieFinder; private String defaultLocale; @Autowired public void configure(MovieFinder movieFinder, @Value(“#{ systemProperties[‘user.region’] }”) String defaultLocale) { this.movieFinder = movieFinder; this.defaultLocale = defaultLocale; } // … } See: Expression Language > Annotation … Read more

Populating Spring @Value during Unit Test

If possible I would try to write those test without Spring Context. If you create this class in your test without spring, then you have full control over its fields. To set the @value field you can use Springs ReflectionTestUtils – it has a method setField to set private fields. @see JavaDoc: ReflectionTestUtils.setField(java.lang.Object, java.lang.String, java.lang.Object)