Spring Framework Foundation (01): Core Components Summary, Foundation Environment Setup

Posted by Rebelrebellious on Tue, 10 Sep 2019 03:03:18 +0200

Source code for this article: GitHub. Click here || GitEE. Click here

1. Spring Framework

1. Introduction to Framework

Spring is an open source framework. One of the main advantages of this framework is its hierarchical architecture, which allows users to choose which component to use and provides an integrated framework for J2EE application development.Spring uses basic JavaBean s to accomplish things that were previously only possible with EJB s.However, Spring is not limited to server-side development.Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling.Simply put, Spring is a layered, lightweight, open source framework.

2. Advantage Analysis

1) Hierarchical Architecture

One-stop solution at each level
web tier: struts, spring-MVC
service layer:spring
dao layer: hibernate, mybatis, jdbcTemplate, JPA

2), Lightweight

Less dependent resources, less destroyed resources.

3) High cohesion and low coupling

Spring is a large container that unifies the creation and maintenance of all objects and dependencies for Spring administration.

4) Support for AOP programming

Spring provides facet-oriented programming, which facilitates the interception of privileges and the monitoring of programs.

5) Transaction support

Transaction management can be accomplished through configuration without manual programming

6) Integration testing

Spring supports Junit4 and can easily test Spring programs with annotations.

7) Reduce the difficulty of using API

Spring provides encapsulation of some API s (JDBC, JavaMail, remote calls, etc.) that are very difficult to use in JavaEE development, making their application much less difficult.

8) Integrating various frameworks

Spring does not exclude excellent open source frameworks, but integrates them internally, such as Struts, Hibernate, MyBatis, and so on.

2. Core Component Analysis

1. Core Containers

Containers are the core pattern of the Spring framework, which includes Bean creation, configuration, management, and so on.

2. AOP programming

AOP programming can help decouple applications. Using AOP programming mode, the core points in the system can be decoupled from the object method and managed uniformly.

3. Data Access

This module integrates JDBC, resolves the large amount of code redundancy caused by JDBC development mode, integrates common Dao layer framework, hibernate, mybatis, and so on, making the development environment more convenient to set up.

4. Web Programming

Spring not only integrates the MVC framework of various processes, but also has a powerful framework of springmvc, which helps to separate interface logic from application and decouple application at the Web level.

3. Environmental Construction

Project Structure Diagram:

1. Spring Environment Configuration

  • spring-context

Spring Framework Context Container Configuration.

<!--Read external profile-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <!-- allow JVM Parameter Coverage -->
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
    <!--Ignore configuration parameters not found-->
    <property name="ignoreResourceNotFound" value="true"/>
    <!--Resource File Location-->
    <property name="locations">
        <list>
            <value>classpath:jdbc.properties</value>
        </list>
    </property>
</bean>
<!-- Start component scan, exclude@Controller Component, which is composed of SpringMVC Profile Scan -->
<context:component-scan base-package="com.spring.mvc">
    <context:exclude-filter type="annotation"
                            expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- To configure DRUID Connection Pool -->
<bean id="druidDataSource" abstract="true">
    <!-- Configuration Initialization,Minimum,Maximum -->
    <property name="initialSize" value="${jdbc.initialSize}"/>
    <property name="minIdle" value="${jdbc.minIdle}" />
    <property name="maxActive" value="${jdbc.maxActive}" />
    <!-- Configure Connection Wait Timeout -->
    <property name="maxWait" value="${jdbc.maxWait}" />
    <!-- Configure how often to do a check to detect idle connections that need to be closed in milliseconds -->
    <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
    <!-- Configure a connection's minimum lifetime in the pool,Unit milliseconds -->
    <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />
    <property name="validationQuery" value="SELECT 'x'" />
    <property name="testWhileIdle" value="true" />
    <property name="testOnBorrow" value="false" />
    <property name="testOnReturn" value="false" />
    <!-- open PSCache,And specify on each connection PSCache Size -->
    <property name="poolPreparedStatements" value="true" />
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
    <!-- Configuring monitoring statistics intercept filters,Monitor interface after removal SQL Unable to count -->
    <property name="filters" value="stat" />
</bean>
<!-- Set Data Source Information -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="druidDataSource">
    <!-- Basic information for configuring connections -->
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>
<!--Spring integration mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <!--read mybatis configuration file-->
    <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
    <!-- Automatic Scan mapper.xml Mapping File -->
    <property name="mapperLocations" value="classpath:mybatis/mapper/**.xml"/>
    <!--Paging Assistant Plugin-->
    <property name="plugins">
        <array>
            <bean class="com.github.pagehelper.PageHelper">
                <property name="properties">
                    <value>
                        dialect=mysql
                    </value>
                </property>
            </bean>
        </array>
    </property>
</bean>
<!-- Mapper Interface File Scan -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.spring.mvc.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--Set up JDBC Operational Database-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" lazy-init="false">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!--Set up mybatis Operational Database-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--Mode 1: spring Things Manager-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <!-- Associated Data Sources -->
    <property name="dataSource" ref="dataSource"/>
</bean>
<!--Turn on annotation support for transaction control-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!--Configure Manual Things Management-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="dataSourceTransactionManager"/>
</bean>
  • spring-mvc

Mvc Development Environment Container Configuration.

<!--Scan Files-->
<context:component-scan base-package="com.spring.mvc.controller" />
<!-- MVC Default way of annotation mapping -->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<!-- view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/page/" />
    <property name="suffix" value=".jsp" />
</bean>

2. jdbc parameter configuration

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring-mvc
jdbc.username=root
jdbc.password=123
jdbc.initialSize=10
jdbc.minIdle=10
jdbc.maxActive=120
jdbc.maxWait=60000
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=300000

3. mybatis configuration

mybatis-config.xml file

<configuration>
    <settings>
        <!--Print sql Sentence-->
        <setting name="logImpl" value="STDOUT_LOGGING"/>
    </settings>
</configuration>

4. web.xml Configuration

<display-name>frame_spring</display-name>
<!--Spring Related Configuration-->
<context-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</context-param>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring*.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Encoding filters to UTF8 Code -->
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

5. Configuration of pom.xml

<build>
    <finalName>${pom.artifactId}</finalName>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
    </plugins>
</build>

4. Source code address

GitHub·address
https://github.com/cicadasmile/spring-mvc-parent
GitEE·address
https://gitee.com/cicadasmile/spring-mvc-parent

Topics: Java Spring JDBC Mybatis xml