Spring MVC: Difference between revisions
From My Limbic Wiki
Line 29: | Line 29: | ||
=Configuration= | =Configuration= | ||
===View Resolver - Bean=== | ===View Resolver - Bean=== | ||
<bean | <source lang="Xml"> | ||
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> | |||
<property name="prefix"> | <property name="prefix"> | ||
<value>/WEB-INF/views/</value> | <value>/WEB-INF/views/</value> | ||
Line 40: | Line 40: | ||
<mvc:resources mapping="/webjars/**" location="/webjars/"/> | <mvc:resources mapping="/webjars/**" location="/webjars/"/> | ||
</source> | |||
===Resources=== | ===Resources=== | ||
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" | <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" |
Revision as of 19:57, 23 September 2019
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*
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
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
<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"/>
Servlet
<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>
Dependecied
<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>