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>

转载于:https://my.oschina.net/u/4108765/blog/3059603

猜你喜欢

转载自blog.csdn.net/weixin_34387284/article/details/92427524