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>
相關文章
相關標籤/搜索