整合SpringIOC和SpringMVC

来源:互联网 发布:淘宝引流推广工具 编辑:程序博客网 时间:2024/06/05 14:50

第一次写博客,不知道从何下手,还是直接上代码吧


在src目录下的springIOC配置文件applicationContext.xml中

<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="com.sve.test">
<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>

</beans>

(防止SpringMVC和SpringIOC对同一个对象的管理集合,需要在springmvc的配置文件中也配置一下上面的内容)


在com.sve.test包中建有实体类User.java


public class User {

private int id;

@NotEmpty
private String name;

@NotEmpty
@Past
@DateTimeFormat(pattern="yyyy-MM-dd")
private Date birth;


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 + "]";
}


public User(int id, String name, Date birth) {
super();
this.id = id;
this.name = name;
this.birth = birth;
}

public User() {

}

}


业务逻辑类UserService.java

@Component
public class UserService {

private UserService() {
System.out.println("UserService Constructor....\n\n\n");
}

public void save() {
System.out.println("save");
}

}


和控制类UserController.java

@Controller
@RequestMapping("/integrate")
public class UserController {

@Autowired
     UserService userService;

@RequestMapping("/user")
public String saveUser(@RequestBody @ModelAttribute User u) {
System.out.println(u);
userService.save();
return "hello";
}
}


补充一下在web.xml中的配置

<!-- 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>
  
  <!-- springmvc的配置 -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc-servlet.xml</param-value>
  </init-param>
  </servlet>
  <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
  </servlet-mapping>

直接访问http://localhost:8080/项目名/integrate/user?id=3&name=zty&birth=1999-12-1,就可以在控制上看到效果了


最后给一张使用包的截图



原创粉丝点击