Skip to main content

Posts

Showing posts from April, 2020

Spring Boot MVC Form Validation Example

In this Spring Boot MVC tutorial we’ll see how to validate form fields using standard validation annotations. Form validation is important part of web applications where user input is required so that users can’t enter invalid values. You will also see how to display error messages in front of the field with invalid value so that the user can re-enter input to make it valid. Starter dependencies Starter dependencies that you will be selecting for this Spring Boot form validation example are- spring-boot-starter-web spring-boot-starter-thymeleaf spring-boot-devtools Example uses Thymeleaf templates for view thus the thymeleaf starter. Applications that use spring-boot-devtools will automatically restart whenever files on the classpath change, so you don't have to rebuild and restart the server yourself every time. Maven – pom.xml pom.xml with the above mentioned starter dependencies. <dependency> <groupId>org.springframework.boot</groupId> <artif

Spring Boot With JSP Example

In this Spring Boot with JSP as view example we’ll see how to use JSP as a view with Spring Boot and what extra configuration is needed to do that. First thing to do is to select packaging as “war” while creating Maven project. If you are using STS then while creating new Spring starter project, choose packaging as war in “New Spring Starter Project”. If you are using eclipse then choose a webapp project in the archetype selection while creating Maven project. See example of creating Spring Boot application using STS here- Spring Boot Example Using Spring Tool Suite (STS) Starter dependencies that are needed- spring-boot-starter-web spring-boot-starter-tomcat Project structure Project structure for this Spring Boot JSP example should be as given below. Maven – pom.xml pom.xml should have the given dependencies. Following dependency is needed for compiling the JSP files. <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifact

Spring Boot + Spring Data JPA + MySQL + Spring RESTful

In this tutorial we’ll see how to create Spring Boot + Spring Data JPA + MySQL + Spring RESTful Webservice. There is already a similar example in the post Spring Data JPA Example . By using Spring Boot you will see a significant reduction in required configuration. If you want to see how to create Maven project, please check this post- Create Java Project Using Maven in Eclipse Table of contents Maven dependencies DB table Query JPA Entity class Spring Data JPA Repository Service class Controller class DB Configuration Create Spring Boot Application Class Running the application Maven Dependencies <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.knpcode</groupId> <art

Spring @Scope Annotation

Spring container not only instantiates a bean and wire its dependencies it also provides the scope of an instance. There is inbuilt support for six bean scopes in Spring Framework . For example, bean scope can be singleton, prototype and so forth. This post shows how to set a bean scope using Spring @Scope annotation. Table of contents Supported Spring bean scopes Where to use @Scope Annotation @Scope annotation with different scopes Spring @Scope annotation Singleton example Spring @Scope annotation Prototype example Spring @Scope annotation with @Configuration Supported Spring bean scopes Singleton scope – Single object instance corresponding to a bean definition is created for each Spring IoC container . Singleton scope is a default bean scope. Prototype scope – New object instance is created every time bean is requested from the Spring container. Request scope – For each HTTP request new bean instance is created. This Spring bean scope

Spring @ComponentScan Annotation

Spring framework can automatically detect the classes annotated with @Component and other stereotype annotations like @Repository, @Service, @Controller. You need some way to tell Spring where to look for classes annotated with one of these annotations. For that you can use Spring @ComponentScan annotation, you can also specify the base package to scan. Spring will scan the base package as well as all the sub-packages for the components and register them as beans. @ComponentScan is used along with @Configuration annotation to specify the packages to scan. Since bean are automatically registered so there is no need for having methods marked with @Bean annotation with in the configuration class. With @ComponentScan annotation for automatically registering the bean and @Autowired annotation for automatically injecting the dependencies there is no need for explicit configuration. @ComponentScan annotation is counterpart of <context:component-scan> element used in Spring X

Spring @DependsOn Annotation

Sometimes dependencies between beans may not be direct but you may still want to ensure that a specific bean is initialized before another bean. For example in one bean there is a static block which has to be executed before another bean is initialized. You can use Spring @DependsOn annotation to explicitly force one or more beans to be initialized. You will annotate one of the bean using @DependsOn annotation and the beans that are specified with @DependsOn are guaranteed to be created by the container before this bean. For example in the following definition it is guaranteed that the beanA and beanB are created by the Spring IOC container before creating beanC. @Bean @DependsOn({"beanA","beanB"}) public ClassC beanC() { .. .. } Spring @DependsOn annotation example Here is the complete example where three classes ClassA, ClassB and ClassC are defined. You want to ensure than ClassA and ClassB are initialized before ClassC is initialized. public cl

Spring @Autowired Annotation

Spring @Autowired annotation is used to automatically inject dependencies. Where to apply @Autowired annotation You can apply the @Autowired annotation to setter methods. See example . You can apply the @Autowired annotation to constructors. See example . You can apply @Autowired to fields. See example . You can also apply the annotation to methods with arbitrary names and multiple arguments. See example . Enabling @Autowired annotation To enable autowiring using @Autowired annotation ‘AutowiredAnnotationBeanPostProcessor’ class has to be registered. You can provide bean definition for it directly, though that is seldom needed. <bean class = "org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /> By using <context:annotation-config> element or using <context:component-scan> element (which implicitly enables the functionality of <context:annotation-config> element) AutowiredAnnotationBeanPostProces

Spring @Resource Annotation

For autowiring in Spring apart from using @Autowired annotation there is also support for JSR 330’s @Inject annotation and JSR-250 @Resource annotation. In this post we'll see how to use Spring @Resource annotation for autowiring. @Resource annotation in Spring @Resource annotation can be used on fields or bean property setter methods. @Resource takes a name attribute. By default Spring interprets that value as the bean name to be injected. In other words, this annotation follows autowire=by-name semantics. For example- @Service public class OrderService { private IStore store; // Autowired on Setter @Resource(name="retailStoreBean") public void setStore(IStore store) { this.store = store; } public void buyItems() { store.doPurchase(); } } Spring will look for a bean named “retailStoreBean” to inject here in the store property. The name specified with @Resource annotation is optional. If no name is explicitly specified, the default name