您的位置:首页 > 编程语言 > Java开发

SpringMVC+Spring+MyBatis 的综合练习 5 (配置 Spring)

2018-01-12 17:20 411 查看
Spring 框架是时下越来越流行的框架了,为啥好我是说不明白的,反正就是流行。它已经基本上干掉了 Struts,不仅提出了 Spring Data 对数据的一系列框架,居然现在还要再颠覆一下大家的观念,给你个 Spring Boot,让你彻底抛弃xml……真的行吗?没用过,看网上说的是褒贬不一。这说明还需要进一步成熟,所以我还是先老老实实地把这个练习做完。没准儿我做完了,Boot 就成熟了……怎么觉得自己像葡萄架上的蜗牛呢?蜗牛就蜗牛吧,做个努力奔跑的蜗牛,总会到终点的。加油!

btw,Spring 官网上的动画挺好玩,那个 logo 在鼠标 over 的时候会变绿,下面一个说明功能的图也是拖动过去会变绿。但愿不是我码了下面的代码之后,看着一大堆的问题脸变绿……

Spring的配置集中在两个核心文件,当然随后的servlet也需要随着创建而不断加入或者创建配置。今天就先看着两个。这两个文件都出现在《 SpringMVC+Spring+MyBatis的综合练习 4 (配置 web.xml )》中的 web.xml 文件中。

applicationContext.xml: 出现在 web.xml 文件的第12行。

dispatcherServlet.xml: 出现在 web.xml 文件的第27行。

让我们逐个走起。

5.1 配置 applicationContext.xml

为了今后能方便使用,我们把数据库连接属性配置在一个properties文件中。这种方式是引入参数的常见做法。

5.1.1 配置数据库连接属性文件

dbconfig.properties 代码:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm?useSSL=false
jdbc.user=root
jdbc.password=root


说明:

(1) 这是标准的 MySQL 数据连接的做法。

(2) localhost可以改成IP地址(没试过域名,我猜应该也行)。

(3) useSSL=false 是为了避免在 MySQL 5 之后,系统默认为 SSL 登录而产生的警告(warning)。

(4) 注意:所有行的结尾不要有空格,避免出错。

(5) 保存在类路径 classpath 下,具体到本项目是:/src/main/resources

5.1.2 配置 applicationContext.xml

applicationContext.xml 保存在类路径 classpath 下,具体到本项目是:/src/main/resources

按照上面已经写好的 properties 文件开始编写。

<?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: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-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd"> 
<context:component-scan base-package="com.hh">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- Spring的配置文件,主要配置和业务逻辑有关的 -->
<!-- ============数据源,事务控制 等等等等============== -->
<context:property-placeholder location="classpath:dbconfig.properties" />
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>

<!-- =============配置和MyBatis整合 ================-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 指定MyBatis全局配置文件的位置 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 指定mybatis的mapper文件的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>

<!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描所有dao接口的实现,加入到ioc容器中 -->
<property name="basePackage" value="com.hh.ssm.dao"></property>
</bean>
<!-- ===========================================-->

<!-- ==============事务控制的配置 ===================-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 控制数据源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>

<!-- 开启基于注解的事务,使用xml配置形式的事务(比较重要的都是使用配置式) -->
<aop:config>
<!-- 切入点表达式 -->
<aop:pointcut expression="execution(* com.hh.ssm.service..*(..))" id="txPoint" />
<!-- 配置事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>

<!-- 配置事务增强 , 事务如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有方法都是事务方法 -->
<tx:method name="*" />
<!-- 以get开始的所有方法 -->
<tx:method name="get*" read-only="true" />
</tx:attributes>
</tx:advice>

<!-- Spring配置文件的核心点(数据源、与MyBatis的整合、事务控制) -->
</beans>


说明:

applicationContext.xml 是 Spring 框架中的必备配置文件,可以说是主文件。数据源、事务控制、整合MyBatis等几乎所有管理工作都在这个文件中配置。主要完成了以下几项工作:

(1) 定义要扫描的包,但是只扫描注解为 @Controller 以外的类和方法(context:exclude-filter),Controller 则交给下面的dispatcherServlet.xml 去处理。下面会看到。(context 标签)

(2) 定义数据源 pooledDataSource,使用 c3p0 中的 ComboPooledDataSource 类,从 dbconfig.properties 文件中获取参数。这地方要小心,因为 { } 是在引号里面,所以IDE不提示,也不会自动配对。我吃过亏。(bean 标签)

(3) 整合 MyBatis,定义 Session 工厂为 sqlSessionFactory。指定了 Mybatis 全局配置文件 mybatis-config.xml 的位置和 mapper 文件的位置。指明所用数据源为 pooledDataSource。(bean 标签)

(4) 配置扫描器, 扫描所有dao接口的实现,将 Mybatis 接口的实现加入到 IOC 容器中。(bean 标签)

(5) 定义事务管理器 transactionManager,配置数据源使用 pooledDataSource。(bean 标签)

(6) 配置 AOP,开启基于注解的事务,使用 XML 配置形式(我也见过全注解的,但是还是这样看着明白些,也踏实些。老师说,相对重要的事务管理最好还是用 XML 配置)。定义切入点表达式 txPoint,指明使用事务增强 txAdvice。表达式中“ com.hh.ssm.service..*(..) ”表示Service包中的所有内容,包括子包。同样,因为也是在引号里面写的,需要注意不要少写了半个括号什么的。(aop:config 标签)

(7) 配置事务增强 txAdvice,将所有方法都看成事务方法,其中以 get 开头的方法(都是查询用的)都设为只读。(tx:advice 标签)

5.2 配置 dispatcherServlet.xml

这是从 web.xml 文件中定义的 JavaBean 提出的要求。它保存的位置是:WEB-INF 文件夹下,和 web.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd 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-4.3.xsd"> <!-- SpringMVC的配置文件,主要是和网站跳转逻辑有关 -->
<context:component-scan base-package="com.hh"
use-default-filters="false">
<!-- 只扫描控制器 Controller -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- 配置视图解析器,方便页面返回 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 两个标准配置 -->
<!-- 将springmvc不能处理的请求交给tomcat -->
<mvc:default-servlet-handler />
<!-- 能支持更高级的一些功能,JSR303校验,快捷的ajax...映射动态请求 -->
<mvc:annotation-driven />
</beans>


说明:

这是 SpringMVC 的配置文件,主要是和网站跳转逻辑有关。完成了以下几项工作:

(1) 配置要扫描的包(context:component-scan 标签),同时禁用默认过滤规则。只扫描 注解为 @Controller 的方法(context:include-filter)。

注意:使用 context:include-filter 的时候,一定要禁用默认过滤规则;而在applicationContext.xml 中使用的 context:exclude-filter 就不用设置禁用。

(2) 配置视图解析器(bean 标签),指定前缀(/WEB-INF/views)和后缀(.jsp)。前缀要求我们建立对应的文件夹。

(3) (mvc:default-servlet-handler 标签)将 SpringMVC 不能处理的请求交给 Tomcat 处理。

(4) 配置注解驱动(mvc:annotation-driven 标签),能支持更高级的一些功能,JSR303 校验,快捷的 ajax……映射动态请求等。

org.springframework.stereotype.Controller

org.springframework.web.servlet.mvc.Controller

注意:这两个家伙长的太像了,在上面的 XML 配置中,包含和排除的包都应该是:

- - org.springframework.stereotype.Controller

千万别搞错了,我因为这个,找了好久。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: