【JavaEE】Springmvc+Spring整合及example

这一篇在前一篇Springmvc的基础上,加上Spring。Spring的主要用途叫做控制反转(依赖注入,IoC/DI)和面向切面的编程(AOP),本文只介绍IoC,因为AOP主要的应用场景是记录日志,暂时不需要,等我要整合的几个框架都整合在一起的时候再加上。

pom.xml不需要新添加任何东西,因为spring-core等包都在导入spring-webmvc的时候作为依赖项被导入了,所以直接来看配置。

1. web.xml

Spring要在程序需要某个对象的时候,把这个对象的实例注入进去,默认情况下,Spring以单例的形式维护了所有需要注入的对象的实例,哪里需要就把对应的实例给哪里,Spring自己对实例、程序运行的管理构成了Spring自己的容器,第一步就是要在web.xml中注册,初始化这个容器:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:/META-INF/applicationContext.xml</param-value>
</context-param>
    
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

这里注册一个ContextLoaderListener,并指定spring配置文件的位置,在类路径下的/META-INF/applicationContext.xml,现在在maven的resources资源包下,即src/main/resources下面创建目录META-INF,在这个目录下创建applicationContext.xml,这个文件就是spring的配置文件。

2. applicationContext.xml

applicationContext.xml是spring的核心配置文件,spring4和之前版本的一个很大的区别,就是推荐情况下bean不是在xml文件中配置,而是通过扫描固定annotation的类,根据对象的类型或者名字自动加载,以前在applicationContext.xml需要配置大量的bean,现在不用了,但是仍然要告诉spring,去哪个包下面找这些带着annotation等着被扫描的类:

<?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:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:p="http://www.springframework.org/schema/p"
    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
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">

    <context:component-scan base-package="org.zhangfc.demo4ss.service"/>

</beans>

前面这一堆东西先不用管,直接粘贴好了,其实很多现在还用不到,不过也没有关系,先放在这好了,真正有用的配置就一句话,需要spring管理的类,请到org.zhangfc.demo4ss.service下面去找。

3. UserService.java

下面写一个服务类,用来获得注册用户列表,创建package:org.zhangfc.demo4ss.service,在下面创建接口UserService,写一个方法获取所有用户名。

public interface UserService {
    public List<String> getAllUsernames();
}

再创建一个类UserServiceImpl,实现这个接口:

@Service
public class UserServiceImpl implements UserService {

    public List<String> getAllUsernames() {
        List<String> users = new ArrayList<String>();
        users.add("zhangsan");
        users.add("lisi");
        users.add("wangwu");
        return users;
    }

}

这里比较关键的就是@Service这个annotation,它告诉Spring,我是一个Service,需要你来管理我。

4. HomeController

回到控制器里,上一篇文章我写了一个方法叫json来返回一个json对象,现在改一下这个方法,通过前面写的UserService来获取用户列表并返回给客户端。

首先定义一个类的全局变量UserService:

@Autowired
private UserService userService;

这里要注意的是Autowire这个annotation,它是为了告诉spring,这个对象没有实例化,需要你来注入一个UserService的实例,那问题就是,UserSerivce是一个接口,不指定谁知道你想用的实现类是哪个,Spring会首先看自己的容器里有没有一个叫做userService的对象(刚才那个UserServiceImpl的对象名字就叫做userServiceImpl),如果找不到就在配置文件里配置的路径下面寻找UserService的实现类,找到了就把它的对象拿过来,除此之外刚才Service那个annotation还可以指定一个value:

@Service("userService")

这样一来,对于UserServiceImpl这个类的实例,Spring给它起的名字就不是userServiceImpl了,而是userService,如果某个接口的实现类有多个,就可以使用这种方法来指定用哪个实现类,个人认为,如果每个接口都只有一个实现类,那么这么做确实很方便,但如果有多个实现类并且可能会更换的话,就不如配置文件明了了(当然了,现在这种方式也可以把参数抽出来放到配置文件里,不过还是麻烦一些)。

然后修改json方法:

@RequestMapping("/json")
@ResponseBody
public List<String> json(){
    return userService.getAllUsernames();
}

Spring在这里的作用就是把HomeController需要的userService注入进来,运行程序,访问http://localhost:8080/demo4springmvc-spring/json:

["zhangsan","lisi","wangwu"]

源码下载

Spring的另一个功能AOP暂且不用,等需要记录日志的时候再写,当然,Spring本身也有很多复杂的机制,后面在具体问题中慢慢介绍。

原文地址:https://www.cnblogs.com/smarterplanet/p/4085620.html