关于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,但是没有配置他啊

阅读 2.8k
1 个回答
<context:component-scan base-package="com.yang">

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题