本文源碼:GitHub·點(diǎn)這里 || GitEE·點(diǎn)這里
10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問(wèn)題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有滑縣免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
Spring是一個(gè)開(kāi)源框架,框架的主要優(yōu)勢(shì)之一就是其分層架構(gòu),分層架構(gòu)允許使用者選擇使用哪一個(gè)組件,同時(shí)為 J2EE 應(yīng)用程序開(kāi)發(fā)提供集成的框架。Spring使用基本的JavaBean來(lái)完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限于服務(wù)器端的開(kāi)發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合的角度而言,任何Java應(yīng)用都可以從Spring中受益。簡(jiǎn)單來(lái)說(shuō),Spring是一個(gè)分層的輕量級(jí)開(kāi)源框架。
1)、分層架構(gòu)
一站式,每一個(gè)層都提供的解決方案
web層:struts,spring-MVC
service層:spring
dao層:hibernate,mybatis,jdbcTemplate,JPA
2)、輕量級(jí)
依賴資源少,銷毀的資源少。
3)、高內(nèi)聚低耦合
Spring就是一個(gè)大容器,可以將所有對(duì)象創(chuàng)建和依賴關(guān)系統(tǒng)一維護(hù),交給Spring管理。
4)、AOP編程的支持
Spring提供面向切面編程,可以方便的實(shí)現(xiàn)對(duì)程序進(jìn)行權(quán)限攔截、運(yùn)行監(jiān)控等功能。
5)、事務(wù)的支持
只需要通過(guò)配置就可以完成對(duì)事務(wù)的管理,而無(wú)需手動(dòng)編程
6)、集成測(cè)試
Spring對(duì)Junit4支持,可以通過(guò)注解方便的測(cè)試Spring程序。
7)、降低API的使用難度
Spring 對(duì)JavaEE開(kāi)發(fā)中非常難用的一些API(JDBC、JavaMail、遠(yuǎn)程調(diào)用等),都提供了封裝,使這些API應(yīng)用難度大大降低
8)、集成各種框架
Spring不排斥各種優(yōu)秀的開(kāi)源框架,其內(nèi)部提供了對(duì)各種優(yōu)秀框架的集成,如:Struts、Hibernate、MyBatis等。

1、核心容器
容器是Spring框架的核心模式,該模塊包含Bean的創(chuàng)建、配置、管理等功能。
2、AOP編程
AOP 編程可以幫助應(yīng)用程序解耦,使用AOP編程模式,可以把系統(tǒng)中的核心點(diǎn)從對(duì)象方法中解耦,統(tǒng)一管理。
3、數(shù)據(jù)訪問(wèn)
該模塊集成了JDBC,解決JDBC開(kāi)發(fā)模式導(dǎo)致的大量代碼冗余,集成常用的Dao層框架,hibernate,mybatis等,使開(kāi)發(fā)環(huán)境的搭建更加便捷。
4、Web編程
Spring不僅集成各種流程的MVC框架,還自帶springmvc強(qiáng)大的框架,有助實(shí)現(xiàn)界面邏輯和應(yīng)用程序分離,在Web層面實(shí)現(xiàn)應(yīng)用的解耦。
項(xiàng)目結(jié)構(gòu)圖:

Spring框架上下文環(huán)境容器配置。
<!--讀取外部配置文件-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 允許JVM參數(shù)覆蓋 -->
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<!--忽略沒(méi)有找到的配置參數(shù)-->
<property name="ignoreResourceNotFound" value="true"/>
<!--資源文件位置-->
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 啟動(dòng)組件掃描,排除@Controller組件,該組件由SpringMVC配置文件掃描 -->
<context:component-scan base-package="com.spring.mvc">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 配置DRUID的連接池 -->
<bean id="druidDataSource" abstract="true">
<!-- 配置初始化,最小,最大 -->
<property name="initialSize" value="${jdbc.initialSize}"/>
<property name="minIdle" value="${jdbc.minIdle}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<!-- 配置連接等待超時(shí)時(shí)間 -->
<property name="maxWait" value="${jdbc.maxWait}" />
<!-- 配置間隔多久才進(jìn)行一次檢測(cè),檢測(cè)需要關(guān)閉的空閑連接,單位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
<!-- 配置一個(gè)連接在池中的最小生存時(shí)間,單位毫秒 -->
<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" />
<!-- 打開(kāi)PSCache,并且指定每個(gè)連接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters,去掉后監(jiān)控界面SQL無(wú)法統(tǒng)計(jì) -->
<property name="filters" value="stat" />
</bean>
<!-- 設(shè)置數(shù)據(jù)源信息 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="druidDataSource">
<!-- 配置連接的基本信息 -->
<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整合mybatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--讀取mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<!-- 自動(dòng)掃描mapper.xml映射文件 -->
<property name="mapperLocations" value="classpath:mybatis/mapper/**.xml"/>
<!--分頁(yè)助手插件-->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=MySQL
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- Mapper接口文件掃描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.spring.mvc.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--設(shè)置JDBC操作數(shù)據(jù)庫(kù)-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" lazy-init="false">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--設(shè)置mybatis操作數(shù)據(jù)庫(kù)-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--方式一:spring事物管理器-->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 關(guān)聯(lián)數(shù)據(jù)源 -->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--開(kāi)啟事務(wù)控制的注解支持-->
<tx:annotation-driven transaction-manager="dataSourceTransactionManager"/>
<!--配置手動(dòng)事物管理-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="dataSourceTransactionManager"/>
</bean>
Mvc開(kāi)發(fā)環(huán)境容器配置。
<!--掃描文件-->
<context:component-scan base-package="com.spring.mvc.controller" />
<!-- MVC默認(rèn)的注解映射的方式 -->
<mvc:annotation-driven />
<mvc:default-servlet-handler/>
<!-- 視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/page/" />
<property name="suffix" value=".jsp" />
</bean>
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
mybatis-config.xml文件
<configuration>
<settings>
<!--打印sql語(yǔ)句-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>
<display-name>frame_spring</display-name>
<!--Spring相關(guān)配置-->
<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>
<!-- 編碼過(guò)濾器,以UTF8編碼 -->
<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>
<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>
GitHub·地址
https://github.com/cicadasmile/spring-mvc-parent
GitEE·地址
https://gitee.com/cicadasmile/spring-mvc-parent
當(dāng)前標(biāo)題:Spring框架基礎(chǔ)(01):核心組件總結(jié),基礎(chǔ)環(huán)境搭建
URL分享:http://www.chinadenli.net/article32/isjdsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、定制網(wǎng)站、全網(wǎng)營(yíng)銷推廣、App設(shè)計(jì)、虛擬主機(jī)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
營(yíng)銷型網(wǎng)站建設(shè)知識(shí)