1 Spring下载与安装
在第一篇《Spring 3.x+Spring MVC 3.x+MyBatis 3.x 整合(一)Spring MVC 环境搭建》中已经加入了spring的所有jar包,这里不再说明。
2 修改Web配置文件
为web.xml添加如下配置:
<!-- Spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<!-- 可以把配置文件放在WEB-INF下,然后使用如下配置
<param-value>/WEB-INF/applicationContext.xml</param-value> -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
完整的web.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name></display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>whowii.website2</param-value>
</context-param>
<!-- log4j 配置 -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!-- Spring 配置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
<!-- 可以把配置文件放在WEB-INF下,然后使用如下配置
<param-value>/WEB-INF/applicationContext.xml</param-value> -->
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring mvc 配置-->
<servlet>
<!-- 该名称将影响 xxx-servlet.xml 配置文件名称 -->
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- load-on-startup:表示启动容器时初始化该Servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<!--
url-pattern:表示哪些请求交给Spring Web MVC处理, "/"是用来定义默认servlet映射的。
也可以如"*.html"表示拦截所有以html为扩展名的请求。
-->
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 默认首页配置 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
3 增加spring配置文件
在src目录下创建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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" 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/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 自动扫描业务包 -->
<context:component-scan base-package="com.whowii.core.service.impl" />
</beans>
4 修改java类
本测试代码最终达到的目标是,为Action注入Service实例。
A.新增服务接口:com.whowii.core.service.UserService,内容如下:
package com.whowii.core.service;
public interface UserService {
public String getMsg();
}
package com.whowii.core.service.impl;
import org.springframework.stereotype.Service;
import com.whowii.core.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
public String getMsg() {
return "UserServiceImpl";
}
}
注意Service注解。
C.修改EnterController类,增加类型为UserService的属性userService,最终的代码如下:
package com.whowii.core.action;
import javax.annotation.Resource;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.whowii.core.service.UserService;
@Controller
@RequestMapping(value = "/manage/enter")
public class EnterController {
private Logger logger = Logger.getLogger(getClass());
private UserService userService;
@Resource(name = "userService")
public void setUserService(UserService userService) {
this.userService = userService;
}
// 访问URL:website_java2/enter/index.do
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index(String id, Model model) {
logger.info("访问了index方法,id=" + id);
model.addAttribute("title", "系统登录");
model.addAttribute("date", this.userService.getMsg());
return "manage/enter/index";
}
}
这里通过Resource注解,注入UserService实例。
4 测试访问
启动tomcat,打开浏览器访问:http://localhost:8180/website_java2/manage/enter/index.do
打开的页面应该是这样的: