在spring mvc中,注解是需要通过配置文件去开启的,一般简单的项目可分为两个配置文件,这里姑且叫做spring-mvc.xml与spring-context.xml。其中spring-mvc.xml作为servlet的配置文件,主要扫描Controller的注解,另一个则作为全局的配置文件,可用来注册bean。

在spring的配置文件中,可以通过

<context:annotation-config />

来开启注解扫描的功能,但是这种方法会使系统默认扫描所有的注解,包括@Controller、@Service等等,但是如此配置将会导致事务失效,原因请参照这里:点击跳转大佬的博客

因此需要两个配置文件,分别加载@Controller和其他的注解。

1、先看看web.xml是如何配置配置文件的

<!DOCTYPE web-app PUBLIC
        "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/spring-context.xml</param-value>
    </context-param>

    <servlet>
        <servlet-name>mvc-DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

2、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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--载入配置文件owlforest.properties-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:owlforest.properties" />

    <!--只扫描Controller-->
    <context:component-scan base-package="com.owlforest.www" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>

3、spring-context.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--载入配置文件owlforest.properties-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:owlforest.properties" />

    <!--不扫描Controller-->
    <context:component-scan base-package="com.owlforest.www">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>

Spring MVC与注解相关的一些配置的方法的更多相关文章

  1. spring mvc 基于注解 配置默认 handlermapping

    spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...

  2. Spring MVC学习总结(2)——Spring MVC常用注解说明

        使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02     之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...

  3. Spring MVC内容协商实现原理及自定义配置【享学Spring MVC】

    每篇一句 在绝对力量面前,一切技巧都是浮云 前言 上文 介绍了Http内容协商的一些概念,以及Spring MVC内置的4种协商方式使用介绍.本文主要针对Spring MVC内容协商方式:从步骤.原理 ...

  4. 基于spring mvc的注解DEMO完整例子

    弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...

  5. spring mvc 基于注解的使用总结

    本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...

  6. java spring mvc 全注解

    本人苦逼学生一枚,马上就要毕业,面临找工作,实在是不想离开学校.在老师的教导下学习了spring mvc ,配置文件实在繁琐,因此网上百度学习了spring mvc 全注解方式完成spring的装配工 ...

  7. Spring MVC 全注解配置 (十一)

    完整的项目案例: springmvc.zip 目录 实例 项目结构: 父级的pom配置: <?xml version="1.0" encoding="UTF-8&q ...

  8. SpringMVC4 + Spring + MyBatis3 基于注解的最简配置

    本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...

  9. Spring MVC 基础注解之@RequestMapping、@Controller、(二)

    我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...

随机推荐

  1. Redis 可视化界面工具:Fastoredis

    下载地址:https://sourceforge.net/projects/fastoredis/

  2. 代码阅读笔记:【C-COT】

    [C-COT]:Danelljan M, Robinson A, Khan F S, et al. Beyond correlation filters: Learning continuous co ...

  3. NET设计模式 第二部分 创建型模式(6):创建型模式专题总结(Creational Pattern)

    创建型模式专题总结(Creational Pattern) ——.NET设计模式系列之七 Terrylee,2006年1月 概述 创建型模式,就是用来创建对象的模式,抽象了实例化的过程.它帮助一个系统 ...

  4. C# MD5位加密

    /// <summary> /// 方法一:通过使用 new 运算符创建对象 /// </summary> /// <param name="strSource ...

  5. Java封装案例

    ---恢复内容开始--- 在类的getter/setter方法中在getter方法中要先实列话 在构造方法中无参构造(必须要构造)好处很多!!! 描述学生信息及相关联的专业信息 把Subject和St ...

  6. 修改machine.config遇到System.Net.ServicePointManager 的类型初始值设定项引发异常

    <system.net>节点应该在</configuration>上面添加,即config页尾. 而不是在<configuration> 后面添加. 在</s ...

  7. 黄聪:在.NET中使用GeckoFX 29

    GeckoFX is a .NET control, that works similarly to “System.Windows.Forms.WebBrowser” Control, while ...

  8. <亲测>centos安装 .net core 2.1

    https://www.microsoft.com/net/learn/get-started-with-dotnet-tutorial#install .NET Tutorial - Hello W ...

  9. 【maven】之使用import scope解决maven继承(单)问题

    想必大家在做SpringBoot应用的时候,都会有如下代码: <parent> <groupId>org.springframework.boot</groupId> ...

  10. C++进阶--拥有资源句柄的类(浅拷贝,深拷贝,虚构造函数)

    // Person通过指针拥有string class Person { public: Person(string name) { pName_ = new string(name); } ~Per ...