top of page
Search
Writer's pictureNguezang Arsene

Spring and the SpringMVC Framework: Lesson 1

Spring Framework

A framework that supports the development of java applications. We configure different components to work together efficiently. With this, we are able to build applications from POJOs(Plain Old Java Objects). There is Little or no interference of the business logic into the framework.

Spring has a layered (N-tier) architecture made up of 3 main layers (tiers)

  1. Presentation :Objects like the SpringMVC controllers are configured in the presentation context(tier)

  2. Service Objects: Business specific objects exists in the business context (tier)

  3. Data access objects exist in the persistence context(tier)

Spring MVC main features

  1. Single Central Servlet: The FrontController or DispatcherServlet manages HTTP level request/response and delegate these to the appropriate interfaces

  2. Separation of concerns

  3. Views are plug and play : Views work perfectly when first used or connected without reconfiguration or adjustments by the user

  4. Controllers are HTTP agnostic: No need to use the Servlet API, Spring's API is wrapped on top of this.

Spring MVC Major Interfaces



SpringMVC flow based on the above diagram

  1. The DispatcherServlet first receives a request from the client

  2. The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request

  3. The HandlerMapping returns the Controller that should handle this request

  4. The Controller processes the request by calling the appropriate service methods

  5. The Controller returns the ModelAndView object to the DispatcherServlet (The ModelAndView object contains the model data and the view name).

  6. The DispatcherServlet sends the view name to the ViewResolver to find the actual View to invoke.

  7. The view Resolver returns the view

  8. The DispatcherServlet passes the model to the view to render the result

  9. The model data is bound to the view, rendered and sent back to the DispatcherServlet

  10. The DispatcherServlet returns this response back to the client that builds and displays this to the user.

Spring MVC Front Controller Configuration

Create a new Maven Project and in the web.xml file, add the following configurations in order to use the DispatcherServlet


In the pom.xml file, add the following dependencies


pom.xml configurations
pom.xml configurations

Then create a folder called config (directory) in WEB-INF, inside this folder, create a new xml file called springmvc-config.xml and add the following codes to it.

Configuring the DispatcherServlet in web.xml
Configuring the DispatcherServlet in web.xml

line 7 to 15

Spring’s dispatcher servlet is implemented by the class org.springframework.web.servlet.DispatcherServlet. The initialization parameter contextConfigLocation tells Spring where to load configuration files. The <load-on-startup> tag tells the servlet container to load this servlet upon start up with highest priority.

Line 17 to 20

Tells the container to route all requests to Spring’s dispatcher servlet. For example, the following URL will be processed by the dispatcher servlet:

http://localhost:8088/SpringLesson1/


JavaConfig Version


springmvc-config.xml


configurations of the springmvc.xml

<context:component-scan base-package="pkg,pkg1.."/>

Scans defined packages to find and register @Component-annotated classes and activate basic annotations[e.g. @Autowired] - within the current application context.


<mvc:annotation-driven/>

Enables support for specific annotations [e.g. @RequestMapping, etc.] that are required for Spring MVC to dispatch requests to @Controllers. It is based on MVC XML namespace.


<tx:annotation-driven/>

Enables support for specific annotations that are required for Spring Transactions @Transactional. It is based on transaction XML namespace.


JavaConfig Version


spring config with java

@Configuration This indicates that a class declares one or more @Bean methods. @EnableWebMvc imports some special Spring MVC configuration @ComponentScan("blog.nguezang") This specifies the base packages to scan for annotated components(beans)


Spring MVC Annotations


simple hierarchy of annotations

Here are some of the common annotations used in SpringMVC

@Component: It means that the Spring framework will autodetect these classes for dependency injection when annotation-based configuration and class path scanning is used.

@Repository - automatic translation of exceptions

@Controller – rich set of framework functionality

@Service – “home” of @Transactional

@RequestMapping

@ModelAttribute

@RequestParam

@SessionAttributes

More about the use of these annotations in the coming lessons


Data Binding

  1. Automatically maps request parameters to domain objects

  2. Simplifies code by removing repetitive tasks

  3. Built-in Data Binding handles simple String to data type conversions

  4. HTTP request parameters [String types] are converted to model object properties of varying data types.

  5. Does NOT handle COMPLEX data types; that requires custom formatters

  6. Does handle complex nested relationships

Inversion of Control (IOC)

  1. Promotes loose coupling between classes and subsystems

  2. Adds flexibility to the code for future changes

  3. Classes are easier to unit test in isolation

IOC (Inversion of Control) is implemented using Dependency Injection (DI)


Dependency Injection (DI)

The Container injects dependencies when it creates the bean Dependency Injection Types Examples

1. Property based(byType)

@Autowired

ProductService productService;

2. Setter based(byName)

ProductService productService;

@Autowired

public void setProductService(ProductService productService){ this.productService = productService;

}

3. Constructor based:

ProductService productService;

@Autowired

public ProductController(ProductService productService) { this.productService = productService;

}

Note: Best practice is to use Constructor based dependency Injection because it automatically enforces the order and completeness of the instantiated objects. Furthermore, when the last object is instantiated the wiring phase of your application is completed.


Notes and References

167 views0 comments

Recent Posts

See All

Komentáře


Post: Blog2_Post
bottom of page