Spring MVC与注解相关的一些配置的方法
在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与注解相关的一些配置的方法的更多相关文章
- spring mvc 基于注解 配置默认 handlermapping
spring mvc 是类似于 Struts 的框架.他们都有一个最主要的功能就是URL路由.URL路由能将请求与响应请求处理逻辑的类(在Struts中即是action,在spring mvc 中即是 ...
- Spring MVC学习总结(2)——Spring MVC常用注解说明
使用Spring MVC的注解及其用法和其它相关知识来实现控制器功能. 02 之前在使用Struts2实现MVC的注解时,是借助struts2-convention这个插件,如今我们使 ...
- Spring MVC内容协商实现原理及自定义配置【享学Spring MVC】
每篇一句 在绝对力量面前,一切技巧都是浮云 前言 上文 介绍了Http内容协商的一些概念,以及Spring MVC内置的4种协商方式使用介绍.本文主要针对Spring MVC内容协商方式:从步骤.原理 ...
- 基于spring mvc的注解DEMO完整例子
弃用了struts,用spring mvc框架做了几个项目,感觉都不错,而且使用了注解方式,可以省掉一大堆配置文件.本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mv ...
- spring mvc 基于注解的使用总结
本文转自http://blog.csdn.net/lufeng20/article/details/7598801 概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Sprin ...
- java spring mvc 全注解
本人苦逼学生一枚,马上就要毕业,面临找工作,实在是不想离开学校.在老师的教导下学习了spring mvc ,配置文件实在繁琐,因此网上百度学习了spring mvc 全注解方式完成spring的装配工 ...
- Spring MVC 全注解配置 (十一)
完整的项目案例: springmvc.zip 目录 实例 项目结构: 父级的pom配置: <?xml version="1.0" encoding="UTF-8&q ...
- SpringMVC4 + Spring + MyBatis3 基于注解的最简配置
本文使用最新版本(4.1.5)的springmvc+spring+mybatis,采用最间的配置方式来进行搭建. 1. web.xml 我们知道springmvc是基于Servlet: Dispatc ...
- Spring MVC 基础注解之@RequestMapping、@Controller、(二)
我现在学的是spring4.2 今天主要学习了Spring MVC注解 引入注解可以减少我们的代码量,优化我们的代码. @Controller:用于标识是处理器类: @RequestMapping:请 ...
随机推荐
- ThinkPHP5与JQuery实现图片上传和预览效果
内容正文 这篇文章主要为大家详细介绍了thinkphp上传图片功能,和jquery预览图片效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 先上效果图: html和js代码如下: <!DO ...
- python去除\ufeff、\xa0、\u3000
今天使用python处理一个txt文件的时候,遇到几个特殊字符:\ufeff.\xa0.\u3000,记录一下处理方法 代码:with open(file_path, mode='r') as f: ...
- c/c++ 获取数组长度
在C/C++中并没有提供直接获取数组长度的函数 c/c++ 获取数组长度其中一种方法是使用sizeof(array) / sizeof(array[0]). 在C语言中习惯上在使用时都把它定义成一个宏 ...
- golang 原子操作函数
golang中的原子操作在sync/atomic package中. 下文以比较和交换操作函数为例,介绍其使用. CompareAndSwapInt32 比较和交换操作是原子性的. // Compar ...
- Linux Centos配置ssh客户端工具putty
配置ssh客户端工具putty 1.直接点击putty.exe文件 2.配置putty 3.配置putty编码 4.配置putty字体大小 5.配置putty样式 6.保存配置信息 7.配置MTp ...
- Linux VMware安装VMTools工具
安装VMTools工具 2)先启动CentOS并成功登录如下图,发现底部提示且窗口中等大小,准备安装 3)选择虚拟机菜单栏--安装VMware tools 4)光驱自动挂载VMTools 5)右键解压 ...
- 无法连接到 OPC服务器[无此类接口支持(异常来自HRESULT:0x80004002(E_NOINTERFACE))]
来源:https://stackoverrun.com/cn/q/10400663 无法连接到ABB Freelance OPC服务器[无此类接口支持(异常来自HRESULT:0x80004002(E ...
- 黄聪:JS正则表达式验证数字
<script type="text/JavaScript"> function validate(){ var reg = new RegExp( ...
- BlockingQueue队列
1.BlockingQueue定义的常用方法如下 抛出异常 特殊值 阻塞 超时 插入 add(e) offer(e) put(e) offer(e,time,unit) 移除 remove() ...
- 纯css实现
实现此效果 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!DOCTYPE html><html lang=& ...