Spring MVC: please explain difference between @RequestParam and @ModelAttribute

@RequestParam just populates stand-alone variables (which may of course be fields in a @ModelAttribute class). These variables will be thrown away when the Controller is done, unless they have been fed into the model. Don’t confuse the word “model” with session. The http conversation is generally: HTTP.GET, server response, then HTTP.POST. When you have the … Read more

Parameter 0 of constructor in required a bean of type ‘java.lang.String’ that could not be found

Since you do not provide the public default constructor and you added your own non-default constructor the instantiation will fail. I would suggest you to define the input file path as property like @Value(“${inputFilePath}”). If you need further initialization in your bean define a void method and annotate it with @PostConstruct and do the initialization … 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

What is the proper annotation since @SpringApplicationConfiguration, @WebIntegration, is deprecated in Spring Boot Framework?

Take a look into JavaDocs of deprecated classes: * @deprecated as of 1.4 in favor of * {@link org.springframework.boot.test.context.SpringBootTest} with * {@code webEnvironment=RANDOM_PORT} or {@code webEnvironment=DEFINED_PORT}. */ … @Deprecated public @interface WebIntegrationTest { * @deprecated as of 1.4 in favor of {@link SpringBootTest} or direct use of * {@link SpringBootContextLoader}. */ … @Deprecated public @interface … 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 entities in the persistence context spring states the following : Doing so triggers the query annotated to the method as an updating query instead of selecting one. As the EntityManager might contain outdated entities after the execution of the modifying query, we do not … Read more

Overriding an Autowired Bean in Unit Tests

If you just simply want to provide a different bean in your tests, i think you don’t need to use spring profiles or mockito. Just do the following: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = { TestConfig.class }) public class MyTest { @Configuration @Import(Application.class) // the actual configuration public static class TestConfig { @Bean public IMyService myService() { return … Read more

Scheduling a job with Spring programmatically (with fixedRate set dynamically)

Using a Trigger you can calculate the next execution time on the fly. Something like this should do the trick (adapted from the Javadoc for @EnableScheduling): @Configuration @EnableScheduling public class MyAppConfig implements SchedulingConfigurer { @Autowired Environment env; @Bean public MyBean myBean() { return new MyBean(); } @Bean(destroyMethod = “shutdown”) public Executor taskExecutor() { return Executors.newScheduledThreadPool(100); … Read more

How to inject a Map using the @Value Spring Annotation?

You can inject values into a Map from the properties file using the @Value annotation like this. The property in the properties file. propertyname={key1:’value1′,key2:’value2′,….} In your code. @Value(“#{${propertyname}}”) private Map<String,String> propertyname; Note the hashtag as part of the annotation.

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

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