位置:首页 > > 整合SpringMVC和SpringIOC

整合SpringMVC和SpringIOC

1.创建一个test.SpringMVC.integrate的包用来演示整合,并创建各类

2.User实体类

public class User {
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirth() {
        return birth;
    }
    public void setBirth(Date birth) {
        this.birth = birth;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", birth=" + birth + "]";
    }    
    private int id;
    @NotEmpty
    private String name;
 
    @Past
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date birth;
}
3.UserService类

@Component
public class UserService {
    public UserService(){
        System.out.println("UserService Constructor...\n\n\n\n\n\n");
    }
     
    public void save(){
        System.out.println("save");
    }
}
4.UserController

@Controller
@RequestMapping("/integrate")
public class UserController {
    @Autowired
    private UserService userService;
     
    @RequestMapping("/user")
    public String saveUser(@RequestBody @ModelAttribute User u){
        System.out.println(u);
        userService.save();
        return "hello";
    }
}

5.Spring配置文件

在src目录下创建SpringIOC的配置文件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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/util 
        http://www.springframework.org/schema/util/spring-util-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        "
        xmlns:util="http://www.springframework.org/schema/util"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"   
        >
    <context:component-scan base-package="test.SpringMVC.integrate">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>        
    </context:component-scan>
     
</beans>
在Web.xml中添加配置

<!-- configure the springIOC -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>
6.在SpringMVC中进行一些配置,防止SpringMVC和SpringIOC对同一个对象的管理重合

<!-- scan the package and the sub package -->
    <context:component-scan base-package="test.SpringMVC.integrate">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>