三大配置文件对于ssm集成思路

三大配置文件对于ssm集成思路

web程序的核心web.xml

1.wbe.xml 里的主要内容包括:
启动Spring核心配置文件
1)监听器
2)加载核心配置文件
启动前端控制器-Spring-mvc
1)加载核心配置文件 Spring-mvc
2)启动加载
处理字符编码问题在进入前端处理器之前
2.解析
①对于一个web程序而言web.xml的重要性不言而喻,在集成Spring框架时,我需要一个监听器来监听服务器是否已经启动,启动则框架也必须跟随启动完成Spring核心配置文件applicationContext.xml文件的加载。
②启动Spring-mvc也在web.xml里面,包括前端控制器,加载配置文件,加载启动。这一步是对于Spring-mvc框架的支持,完成了框架功能的加载。
③接收处理和返回请求都是web程序在处理,所以处理字符编码问题在web.xml中完成。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  
  
  <!-- 加载spring的核心配置文件 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
		<!-- 服务器启动的时候加载spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
  
  
  <!-- 前端控制器 -->
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- 加载核心配置文件 Spring-mvc -->
		<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>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
  	<!-- 处理字符编码问题在进入前端处理器之前 -->
  	<filter>
  		<filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name>
  		<url-pattern>/*</url-pattern>
  	</filter-mapping>
  
</web-app>

Spring的核心配置文件applicationContext.xml

applicationContext.xml包含几大内容:

  1. 加载数据库配置文件jdbc.properties ,在配置文件中找到我们需要的信息一一匹配,如最重要的数据源四大金刚:①username; ②password ;③url; ④driverClassName;
  2. Mybaits是一种不完全的ORM框架,就是我们操作数据库的工具,Mybaits的核心sqlSessionFactory需要交给Spring来管理我们就不需要麻烦的自己创建工厂获得session对象。
  3. 在web后台程序中我们把I*Mapper.java接口交给Spring管理,我们可以直接获得I*Mapper.java接口的代理对象,这样我们就不用去再去创建一个实体类了。
  4. 开启注解扫描功能扫描service层@Autowired注解可以直接注入I*Mapper.java接口的代理对象。
  5. 在service层中,有注解@Service存在,所以在类交给Spring管理的时候,只有框架写好的类交需要在applicationContext.xml中配置,我们自己的类直接注解注入就行了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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/mvc
 					 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 					 ">
 	
 	<!-- 加载jdbc.properties文件 -->
 	<context:property-placeholder location="classpath:jdbc.properties"/>
 	
 	<!-- 数据源   四大金刚-->
 	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
 		<property name="username" value="${jdbc.username}"></property>
 		<property name="password" value="${jdbc.password}"></property>
 		<property name="url" value="${jdbc.url}"></property>
 		<property name="driverClassName" value="${jdbc.driverClassName}"></property>
 	</bean>
 	
 	<!-- spring管理sqlSessionFactory -->
 	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 数据源 -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 配置别名 -->
		<property name="typeAliasesPackage" value="cn.itsource.domain"></property>
		<!-- 加载mapper.xml映射文件 -->
		<property name="mapperLocations" value="classpath:cn/itsource/mapper/*Mapper.xml"></property>
	</bean>
	<!-- mapper	接口交给spring管理   获取的就是代理对象-->
 	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 		<property name="basePackage" value="cn.itsource.mapper"></property>
 	</bean>
 	
 	<!-- 开启扫描包注解   service层 -->
 	<context:component-scan base-package="cn.itsource.service"></context:component-scan>
 	
 	<bean id="now" class="java.util.Date"></bean>
</beans>

Spring-mvc配置文件spring-mvc.xml

  1. Spring-mvc主要控制controller层,实际上web程序其他层也可以控制,但是我们强调职责分离,所以只控制了controller层。
  2. Spring-mvc.xml中有:①开启注解扫描只能访问jsp;②开启静态资源的访问 ;③使@RequestMapping生效; ④ 视图解析器;
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
		xmlns:context="http://www.springframework.org/schema/context"
		xmlns:mvc="http://www.springframework.org/schema/mvc"
		
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 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/mvc
 					 http://www.springframework.org/schema/mvc/spring-mvc.xsd
 					 ">
 	
 	<!-- 开启注解扫描只能访问jsp -->
 	<context:component-scan base-package="cn.itsource.controller"></context:component-scan>
 	<!-- 开启静态资源的访问 -->
 	<mvc:default-servlet-handler/>
 	<!-- 使@RequestMapping生效-->
 	<mvc:annotation-driven />
 	
 	<!-- 视图解析器 -->
 	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 		<property name="prefix" value="/WEB-INF/views/"></property>
 		<property name="suffix" value=".jsp"></property>
 	</bean>
</beans>

Mybaits的sql配置文件

这里配置文件其实就是配置映射I*Mapper.java接口,和匹配实体类获得相应属性的功能,这就不多赘述了。

看待ssm框架集合思路

在ssm框架集合的过程中,我们可以看到,框架功能集合实际上就是三个框架间接或直接地与web程序建立关联,靠各自的核心配置文件来完成各自框架的功能。通过这三大配置文件我们再结合我们分层管理,关于ssm集成的思路就比较清晰了!
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值