Spring MVC

From My Limbic Wiki

Scope

  • The goal, is to provide a good MVC framework
  • for : web application or REST services
  • it's the core framework
  • Simple Framework to develop web applications


Comparaison with Spring Boot and Spring https://www.springboottutorial.com/spring-boot-vs-spring-mvc-vs-spring*

800px

Nice tutorial: https://www.youtube.com/watch?v=g2b-NbR48Jo

Mock - Dependency Injection

Problem, not easy to mock If i want to use different instances of the interface: no chances <source lang="Java">

@RestController
public class WelcomeController {
  private WelcomeService service = new WelcomeService();  	
  
  @RequestMapping("/welcome")
  public String welcome() {
    return service.retrieveWelcomeMessage();
  }
}

</source>

Solution

  • Kind of do Reflection: takes a lot of time in the Unit test

Spring Boot Solution

Spring Boot

Configuration

View Resolver - Bean

<source lang="Xml">

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix">
           <value>/WEB-INF/views/</value>
       </property>
       <property name="suffix">
           <value>.jsp</value>
       </property>
 </bean>
  
 <mvc:resources mapping="/webjars/**" location="/webjars/"/>

</source>

Resources

<source lang="Xml">

   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
       destroy-method="close">
       <property name="driverClass" value="${db.driver}" />
       <property name="jdbcUrl" value="${db.url}" />
       <property name="user" value="${db.username}" />
       <property name="password" value="${db.password}" />
   </bean>

   <jdbc:initialize-database data-source="dataSource">
       <jdbc:script location="classpath:config/schema.sql" />
       <jdbc:script location="classpath:config/data.sql" />
   </jdbc:initialize-database>

   <bean
       class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
       id="entityManagerFactory">
       <property name="persistenceUnitName" value="hsql_pu" />
       <property name="dataSource" ref="dataSource" />
   </bean>

   <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
       <property name="entityManagerFactory" ref="entityManagerFactory" />
       <property name="dataSource" ref="dataSource" />
   </bean>

   <tx:annotation-driven transaction-manager="transactionManager"/>

</source>

Servlet

<source lang="Xml">

   <servlet>
       <servlet-name>dispatcher</servlet-name>
       <servlet-class>
           org.springframework.web.servlet.DispatcherServlet
       </servlet-class>
       <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>/WEB-INF/todo-servlet.xml</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>

   <servlet-mapping>
       <servlet-name>dispatcher</servlet-name>
       <url-pattern>/</url-pattern>
   </servlet-mapping>

</source>

Dependecied

<source lang="Xml">

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>4.2.2.RELEASE</version>
</dependency>

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.5.3</version>
</dependency>

<dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-validator</artifactId>
   <version>5.0.2.Final</version>
</dependency>

<dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.17</version>
</dependency>

</source>