初识Spring+SpringMVC+MyBatis框架(二)---spring.xml和spring-mvc.xml

本文详细介绍了SSM框架中spring-mvc.xml与spring.xml的配置方法。spring-mvc.xml主要负责配置视图解析器和扫描控制器,spring.xml则用于加载服务层组件和服务配置。文章还解释了如何通过配置文件来管理数据库连接。

        上一篇讲了ssm框架环境搭建时web.xml 的配置,下面分别看看spring.xml 和spring-mvc.xml,首先看spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context.xsd"> 	
  
    <context:component-scan base-package="me.lyshi.mvc.controllers" />
	
    <!-- InternalResourceViewResolver jsp -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
         p:prefix="/WEB-INF/views/"
         p:suffix=".jsp"
         p:contentType="text/html; charset=utf-8"
    />
</beans>


在上篇中提过,web.xml中配置的DispatchServlet初始化的时候,会加载spring-mvc.xml,会拦截匹配的请求,并将请求转发给Controller,所以,在spring-mvc.xml中,需要加载所有的控制器。context:component-scan 的含义即是扫描所有该包下的@Component @Controller@Service@Repository注解,并把这些类注册为bean。在me.lyshi.mvc.controllers包下面的都是@Controller。

InternalResourceViewResolver 是一种视图解析器ViewResolver。Controller返回的ModelAndView,在视图逻辑名前面加上prefix,后面加上suffix;例如:

     //显示首页
      @RequestMapping("/index")
     public String firstPage() {
	return "index";
     }     

ViewResolver把index解析为/WEB-INF/views/index.jsp,去显示页面。

要是Controller 返回一个ModelAndView:    return new ModelAndView('user', 'model', model); 

ViewResolver把index解析为/WEB-INF/views/user.jsp,去显示页面。

接下来,再看一下spring.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" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
	<!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config/config.properties" />
	<!-- 自动扫描(自动注入) -->
	<context:component-scan base-package="me.lyshi.mvc.service.*" />

</beans>

spring.xml 里context:component-scan还是将自动扫描@Service注解。
context:property-placeholder 标签 是配置一些不经常变动的参数配置信息,如下所示:

validationQuery=SELECT 1
jdbc_url=jdbc:mysql://localhost:3306/my_site_test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull
jdbc_username=root
jdbc_password=667788

配置好后,可以在其他的配置文件定义bean时,便可以调用配置文件中的键值,如下${jdbc_url},${jdbc_username},${jdbc_password}:

 <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
  <property name="url" value="${jdbc_url}"></property>
  <property name="username" value="${jdbc_username}"></property>
  <property name="password" value="${jdbc_password}"></property>
 </bean>

    下一篇,看一下spring-mybatis.xml如何配置的。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值