Building the Spring MVC framework

If you create a Spring project

The Spring MVC framework should be well known in Java web projects. Wouldn't you build a Spring framework? As someone who's just learning Java, I can, and if you can't, that's really sad.

1. After creating a project in MyEclipse, you can choose 配置a Spring project, which is not discussed here. Because I only use Eclipse.

2. Manual build. Just hands-on.

Create a new Java Web project

1. Open Eclipse, Project Explorerright-click under the tab, and select Web - Dynamic Web Prodect(you should know this step!!!).

newProject.png

2. Click Next. Choose a project name that you think is not bad. Note: naming is very important. Treat every naming as solemn and sacred as naming your own child.

SpringDemo.png

3. No more, done.

demoMenu.png

Get the jar package of the Spring framework

No matter if you cheat or cheat, as long as you get the jar package of the Spring framework. I will give you an address here, you can download it decently. Address: http://projects.spring.io/spring-framework/
Find the version that suits you, download it and save it to a suitable location, it's that simple. After decompression, it should look like this:

spring4.2.6.png

If you look at the name of the package, you may have a general understanding of what this jar package is for, and the next step is to introduce what you need.
Then, you need the jar package you need, and copy it to the project /WebContent/WEB-INF/lib. Why do you want to do this, we will talk about it below.

import jar package

I remember a friend who learned Java complained that: Java is guiding packages every day, which is not as cool as .Net. I don't think so now.
Right-click on the project name, and Build Path - Configure Bulid Path... - Libraries - Add JARs...find the project in the pop-up box /WebContent/WEB-INF/lib, so you can see the jar package you just copied.

add-jars.png

configure configure configure

The most important step in building a Spring framework should be configuration. The official website explains the framework as follows:

The Spring MVC framework is designed around a DispatcherServlet, which distributes requests to various processors and supports configurable processor mapping, view rendering, localization, time zone and theme rendering, and even file uploads. Handlers are classes and methods annotated with @Controller and @RequestMapping in your application, and Spring provides extremely versatile and flexible configurations for handler methods.

So, first we /WebContent/WEB-INF/should create a new web.xmlfile under, and then configure DispatcherServlet in this file.

<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup></servlet><servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern></servlet-mapping><context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value></context-param>

You can also configure character encoding. The default startup page is not configured here. For details, see the example project: https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/web.xml , because here is Name it DispatcherServlet springMVCand have it load as soon as the web project starts. Next we need /WebContent/WEB-INF/to create a springMVC-servlet.xmlSpring configuration file in the directory. The default file name recommended in Spring's official documentation is [servlet-name]-servlet.xmlfile, where the servlet-name is called springMVC, so I created a new one springMVC-servlet.xml.

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"               xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-4.2.xsd
  http://www.springframework.org/schema/mvc
  http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"><!-- use default annotation mapping--><mvc:annotation-driven /><mvc:resources location=" /" mapping="/index.html" /><!-- Automatically scan controllers in the controller package--><context:component-scan base-package="cn.mayongfa.api.controller" /><context :component-scan base-package="cn.mayongfa.controller" /><!-- upload file interception, set the maximum upload file size 30M=30*1024*1024(B)=31457280 bytes --><bean id= "multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="31457280" /></bean>


For details, see: https://github.com/mafly/SpringDemo/blob/master/WebContent/WEB-INF/springMVC-servlet.xml
We web.xmldefine in the file contextConfigLocation, specify the Spring configuration file to be loaded, the general files are Named applicationContext.xml, in this file, we can scan class packages, read configuration files, data source management, AOP configuration, cache and message queue configuration, so next, create a new applicationContext.xmlfile.

<?xml version="1.0" encoding="UTF-8" ?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- Read multiple configuration files into the container and hand it over to Spring management --><bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:global.properties</value>
            <value>classpath:jdbc.properties</value>
        </list>
    </property></bean><!-- Scan class packages, convert Spring-annotated classes into beans automatically, and complete Bean injection--><context:component-scan base-package="cn.mayongfa.common" " /><context:component-scan base-package="cn.mayongfa.service" /><context:component-scan base-package="cn.mayongfa.dao" /><!--master configure datasource- -><bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    <property name="driverClassName">
        <value>${master.jdbc.driverClassName}</value>
    </property>
    <property name="url">
        <value>${master.jdbc.url}</value>
    </property>
    <property name="username">
        <value>${master.jdbc.username}</value>
    </property>
    <property name="password">
        <value>${master.jdbc.password}</value>
    </property>

    ...</bean><!--slave 配置数据源 --><bean id="slaveDataSource" class="com.alibaba.druid.pool.DruidDataSource"
    init-method="init" destroy-method="close">
    
            ...</bean><bean id="dataSource" class="cn.mayongfa.service.imp.DynamicDataSource">
    <property name="targetDataSources">
        <map>
            <entry key="slave" value-ref="slaveDataSource" />
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="masterDataSource" /></bean><!-- 配置Jdbc模板 --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property></bean><!-- Configure transaction manager-->...<!-- Provide transaction enhancement through AOP configuration, let service package under All methods of all beans own transactions -->...


The above is just a simple configuration, and the file is not complete. For a specific complete project example, see GitHub: https://github.com/mafly/SpringDemo

At this point, in fact, we have completed the configuration, and the next step is to create the Packagepackages we need to implement different packages to accomplish different things.

Add Package

The meaning and advantages and disadvantages of layering are not discussed here. According to the normal layered architecture, they are generally divided into View layer, Action layer, Service layer, and Dao layer. We do the same here. The following is called the class library.

package.png

You can create a new one in this way. The specific structure is as follows:
demoLastMenu.png

At this point, the work of building the Spring MVC framework is complete. The next step is to configure specific data sources, caches, AOP, and JMS. good luck!


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324615474&siteId=291194637