Spring MVC与注解相关的一些配置的方法

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

在spring的配置文件中,能够经过web

<context:annotation-config />

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

所以须要两个配置文件,分别加载@Controller和其余的注解。express

一、先看看web.xml是如何配置配置文件的spring-mvc

<!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>

二、spring-mvc.xmlmvc

<?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>

三、spring-context.xmlapp

<?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>
相关文章
相关标签/搜索