上一篇讲了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,去显示页面。
<?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如何配置的。
本文详细介绍了SSM框架中spring-mvc.xml与spring.xml的配置方法。spring-mvc.xml主要负责配置视图解析器和扫描控制器,spring.xml则用于加载服务层组件和服务配置。文章还解释了如何通过配置文件来管理数据库连接。
1495

被折叠的 条评论
为什么被折叠?



