ssm整合中的几个配置文件详解。

4 篇文章 0 订阅
4 篇文章 0 订阅

1.web.xml web工程的配置文件。

web.xml的命名空间

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="MyWebApp" version="2.5">

首先配置监听器以装载applicationContext.xml配置文件。

例:

<!--Spring的ApplicationContext 载入 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
	<!-- web容器启动时,按照如下的路径载入applicationContext.xml配置文件 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>

然后可以配置编码过滤器:

<!-- 编码过滤器,以UTF8编码 
		很简单很实用的一个过滤器,当前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码
		的问题,那这个类就可以出场了。
		CharacterEncodingFilter类具有encoding和forceEncoding两个属性,其中encoding是表示设置request的编码,
		forceEncoding表示是否同时设置response的编码。
	-->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

然后时配置SpringMVC的DispatcherServlet:

<!-- 配置DispatcherServlet -->
	<servlet>
		<servlet-name>manage</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 自定义SpringMVC配置文件路径 -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springMVCConfig.xml</param-value>
		</init-param>
		<!-- 随容器自动启动完成初始化 -->
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>manage</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

web.xml完成。

2.diapatcher-servlet.xml SpringMVC配置文件

dispatcher-servlet.xml的命名空间是:

<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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context.xsd">

首先,我们使用<context:component-scan>查找使用构造型(stereotype)注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)。告诉dispatcherServlet,controller类在哪个包下。

<!-- 注解驱动 -->
<mvc:annotation-driven />
		
<!-- 定义Controller的扫描包 -->
<context:component-scan base-package="mms.controller" />
	
<context:annotation-config>

我们一般在含有Spring的项目中,可能会看到配置项中包含这个配置节点<context:annotation-config>,这是一条向Spring容器中注册

AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor

RequiredAnnotationBeanPostProcessor

这4个BeanPostProcessor.注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

那么那些注释依赖这些Bean呢。

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

这里来说说配置开启的两个常用的组件,RequestMappingHandlerMapping和RequestMappingHandlerAdapter。


RequestMappingHandlerMapping是HandlerMapping的实现类,它会在容器启动的时候,扫描容器内的bean,解析带有@RequestMapping 
注解的方法,并将其解析为url和handlerMethod键值对方式注册到请求映射表中。
RequestMappingHandlerAdapter是HandlerAdapter的实现类,它是处理请求的适配器,说白了,就是确定调用哪个类的哪个方法,并且构造方法参数,返回值。


像其实<context:component-scan/>标签是告诉Spring容器来扫描指定包下的类,并注册被@Component,@Controller,@Service,@Repository等注解标记的组件相似。 
<mvc:annotation-driven/>是告知Spring容器,我们启用注解驱动,支持@RequestMapping注解,这样我们就可以使用@RequestMapping来配置处理器

接着可以配置视图解析器:

<!-- 定义视图解析器 -->
<bean
	class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="prefix" value="" />
	<property name="suffix" value=".html" />
</bean>

如果设置了拦截器则需要在这里配置拦截器:避免一些静态资源被拦截的问题

<!-- 处理静态资源被“/”所拦截的问题 -->
<mvc:default-servlet-handler />
<!-- 定义拦截器 -->
<mvc:interceptors>
		
	<mvc:interceptor>
		<!-- 所有的请求都进入 -->
		
		<mvc:mapping path="/**"/>
		<!-- 不拦截登陆页面 -->
		<mvc:exclude-mapping path="/login.html" />
		<mvc:exclude-mapping path="/mms/css/*" />
		<mvc:exclude-mapping path="/mms/js/**" />
		<mvc:exclude-mapping path="/mms/images/*" />
		<!-- 不拦截处理登陆的业务 -->
		<mvc:exclude-mapping path="/Login/loginUser" />
			
		<bean class="mms.interceptors.MyHandlerInterceptor"/>
    </mvc:interceptor>
		
</mvc:interceptors>

3.applicationContext.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:mvc="http://www.springframework.org/schema/mvc"
	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/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-4.0.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">

首先可以配置propertiesConfigurer,使用PropertyPlaceholderConfigurer可以在XML配置文件中加入外部属性文件,当然也可以指定外部文件的编码,这样以后,在配置文件的上下文中就可以以${xxx}的形式进行引用如:

	<bean
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<!-- 允许JVM参数覆盖 -->
		<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
		<!-- 忽略没有找到的资源文件 -->
		<property name="ignoreResourceNotFound" value="true" />
		<!-- 配置资源文件 -->
		<property name="locations">
			<!-- 配置jdbc配置文件 -->
			<list>
				<value>classpath:jdbc.properties</value>
			</list>
		</property>
	</bean>

在<list>中也可以同时引入多个属性文件。

接着配置的是<context:component-scan basepackage="xxx">这个元素的意思是,扫描这个包,把这个包里和Spring相关的注解的类,注册为Spring的Bean。

<!-- 自动注入mms中的bean -->
<context:component-scan base-package="mms" />

接下来可以配置DataSource连接池。连接池这边则使用了开始引入的属性文件jdbc.properties。

<!-- 注入连接池 -->
	<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
		destroy-method="close">
		<!-- 数据库驱动 -->
		<property name="driverClass" value="${jdbc.driver}" />
		<!-- 相应驱动的jdbcUrl -->
		<property name="jdbcUrl" value="${jdbc.url}" />
		<!-- 数据库的用户名 -->
		<property name="username" value="${jdbc.username}" />
		<!-- 数据库的密码 -->
		<property name="password" value="${jdbc.password}" />
		<!-- 检查数据库连接池中空闲连接的间隔时间,单位是分,默认值:240,如果要取消则设置为0 -->
		<property name="idleConnectionTestPeriod" value="60" />
		<!-- 连接池中未使用的链接最大存活时间,单位是分,默认值:60,如果要永远存活设置为0 -->
		<property name="idleMaxAge" value="30" />
		<!-- 每个分区最大的连接数 -->
		<property name="maxConnectionsPerPartition" value="150" />
		<!-- 每个分区最小的连接数 -->
		<property name="minConnectionsPerPartition" value="5" />
	</bean>

DataSource连接池是为产生一个SqlSeessionFactory Bean。因此接着配置SqlSessionFactory,把这个DataSource注入到里面去。

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 定义数据源 -->
		<property name="dataSource" ref="dataSource" />
		<!-- 指定mybatis全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
		<!-- 扫描mappers目录以及子目录下的所有xml文件 -->
		<property name="mapperLocations" value="classpath:mybatis/mappers/**/*.xml" />
		<!-- 别名扫描包 -->
		<property name="typeAliasesPackage" value="mms.pojo"/>
	</bean>

然后可以配置事务

	<!-- 配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

最后配置最重要的mapper,用自动扫描方式

<!-- 定义Mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="mms.mapper" />
</bean>

4.mybatis-config.xml

在这个配置文件中,由于在applicationContext.xml文件中已经配置完,而且mapper的映射路径也指定完毕,故在此文件中不再需要配置这两样。

在此配置文件中,可以配置<settings/>和<typeAliases/>元素。

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM整合是指将Spring、SpringMvc和Mybatis这三个框架进行整合,以提高项目的开发效率和代码的复用性。下面是SSM整合的思路与配置详解: 1. 配置准备: 在开始整合之前,需要确保你已经配置好了Java开发环境和相应的IDE工具。此外,还要保证你已经下载安装了Spring、SpringMvc和Mybatis的相关依赖库。 2. 配置过程: 2.1 引入依赖: 首先,在你的项目引入Spring、SpringMvc和Mybatis的相关依赖库。你可以通过在pom.xml文件添加相应的依赖来完成这一步骤。这些依赖库包括spring-core、spring-webmvc、mybatis等。 2.2 配置Web.xml文件: 在Web.xml文件,你需要配置Spring和SpringMvc的相关配置。这包括配置DispatcherServlet、ContextLoaderListener和字符编码过滤器等。可以通过在Web.xml文件添加相应的配置来完成这一步骤。 2.3 配置SpringMvc.xml文件: 在SpringMvc.xml文件,你需要配置SpringMvc的相关配置,包括扫描控制器、视图解析器、静态资源路径等。可以通过在SpringMvc.xml文件添加相应的配置来完成这一步骤。 2.4 配置mybatis.xml文件: 在mybatis.xml文件,你需要配置Mybatis的相关配置,包括数据库连接信息、Mapper扫描路径等。可以通过在mybatis.xml文件添加相应的配置来完成这一步骤。 2.5 配置applicationContext.xml文件: 在applicationContext.xml文件,你需要配置Spring的相关配置,包括扫描注解、声明事务管理器等。可以通过在applicationContext.xml文件添加相应的配置来完成这一步骤。 3. mybatis逆向工程: 如果你想要通过数据库表生成相应的实体类、Mapper接口和Mapper XML文件,可以使用mybatis逆向工程来完成。你需要配置相应的代码生成器,包括数据库连接信息、表名、生成路径等。 总结起来,SSM整合的思路是将Spring、SpringMvc和Mybatis三个框架整合在一起,通过配置文件和相关依赖库来实现。具体的配置过程包括引入依赖、配置Web.xml文件、配置SpringMvc.xml文件、配置mybatis.xml文件和配置applicationContext.xml文件。如果需要生成实体类和Mapper文件,可以使用mybatis逆向工程来完成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [SSM整合思路与配置详解](https://blog.csdn.net/slysxy/article/details/107710775)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SSM整合,手把手教程,详解思路讲解](https://blog.csdn.net/liyingjie2001/article/details/124809314)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值