日期:2014-05-18  浏览次数:20607 次

关于spring3 MVC ibatis没有注入的问题
controller:
package com.cssor.controller;
 

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.icreate.service.UserService;
 
@Controller
public class Main {
 
ApplicationContext context ;
UserService userService ;
//UserService userService;
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
System.out.println("我执行了。");
this.userService = userService;
}
@RequestMapping(value="/hello")
public ModelAndView handleRequest() {
 
ModelAndView mav = new ModelAndView("hello");
mav.addObject("what", "Hello World!");
 
//context = new FileSystemXmlApplicationContext("config/applicationContext.xml");
    userService = (UserService) context.getBean("userService");
        
        System.out.println("数据库中的记录条数");
        System.out.println("数据库中的记录条数:"  + userService.countAll());

return mav;
}
 
}


springMVC-servlet.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" 
    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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
    <context:component-scan base-package="com.cssor.controller"/>
    <mvc:annotation-driven />