spring-mvc - 关于Spring和Spring MVC的一个问题

查看:168
本文介绍了spring-mvc - 关于Spring和Spring MVC的一个问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

刚开始学习Spring和Spring MVC,写了个DEMO

我只配置了Spring-mvc.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"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/context  http://www.springframework.org/schema/context/spring-context-3.0.xsd                 http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <!-- @Controller注解的使用前提配置  详细解释见(注.txt 1. )-->
    <mvc:annotation-driven />
    <!--  <context:annotation-config/> -->
    <!-- 对module包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
    <context:component-scan base-package="com.yang">
    </context:component-scan>

    <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
    <bean
            class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" />

    <!-- 定义视图解析器,在视图模型前后添加前缀后缀 暂时只支持jsp后缀-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" /><!-- 路径前缀 -->
        <property name="suffix" value=".jsp" /><!-- 后缀 -->
    </bean>

</beans>

为什么这样都可以访问成功呢?按道理不是需要Spring的IOC把helloController注入好才可以访问的吗?我只在pom.xml里面下了Spring,但是没有配置他啊

解决方案

<context:component-scan base-package="com.yang">

注意,你有这一句,这句意思是让Spring自动扫描 com.yang下面的所有类和所有子包下面的所有类,
然后你的类上注解了@Controller,这个是Spring的注解,Spring扫描到这个注解后会将这个类存放到容器中。

这篇关于spring-mvc - 关于Spring和Spring MVC的一个问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆