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 />
 
    <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="prefix" value="/WEB-INF/view/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>
web.xml<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>springMVC</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
 
<servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:config/applicationContext.xml</param-value>
    </context-param>
    
    <!-- 配置上下文监听 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
</web-app>

解决方案 »

  1.   

    applicationContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <!--
        Copyright 2010 The myBatis Team    Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at        http://www.apache.org/licenses/LICENSE-2.0    Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.
    --><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:jdbc="http://www.springframework.org/schema/jdbc"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-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/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 
       
         <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"></property>
            <property name="url" value="jdbc:sqlserver://192.168.0.55:1433;databaseName=YQBlog"></property>
            <property name="username" value="sa"></property>
            <property name="password" value="Yx123456"></property>
        </bean> 
     
       <bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource" />        
        </bean>
        
        <!-- define the SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:mybatis-config.xml"/>
           
        </bean>   
        
        <bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
             <property name="mapperInterface" value="com.icreate.dao.UserDao"></property>
             <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean>           
        
        <bean id="UserServiceImpl" class="com.icreate.services.Impl.UserServiceImpl">
            <property name="userDao" ref="userDao"></property>
        </bean>
        
        <bean class="com.cssor.controller.Main" lazy-init="default" autowire="default">
            <property name="userService" ref="UserServiceImpl"></property>
        </bean>
    </beans>
      

  2.   

    @Autowired
    UserService userService ;
    这样,你spring配置文件里已经<mvc:annotation-driven />
      

  3.   

    同3楼       @Autowired