springMVC-integrated with spring

1. Basic introduction        

        In project development, Spring manages Service and Respository , SrpingMVC manages Controller and ControllerAdvice, and the division of labor is clear.

When we configure application.xml and springDispatcherServlet-servlet.xml at the same time, the annotated object will be created twice, so it must be distinguished.

2. Specific practices

1. Write the annotation, @Repository @Service @Controller, look at the picture

2. Configure spring in web.xml (including spring's loading listener) and springMVC's respective configuration files.

<web-app>
<!--  配置spring-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
<!--  配置spring的加载监听器-->
<!--  在启动Web容器时,自动装配applicationContext.xml的配置信息-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--    假如不在默认位置(WEB-INF下,默认名称必须是 springDispatcherServlet-servlet.xml),
可以使用以下代码自定义-->
    <!--    <init-param>-->
    <!--      <param-name>contextConfigLocation</param-name>-->
    <!--      <param-value>classpath:WEB-INF/springDispatcherServlet-servlet.xml</param-value>-->
    <!--    </init-param>-->
    <!--    表示tomcat加载servlet的顺序,数字越小越先加载-->
    <load-on-startup>1</load-on-startup>


  </servlet>
  <servlet-mapping>
    <servlet-name>springDispatcherServlet</servlet-name>
    <!--    这里注意这种url请求形式配置成/这样支持restfulT-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>






</web-app>

3. In applicationContext.xml, configure as follows

<context:component-scan base-package="com.bin.springMVC.*">
        <context:exclude-filter type="annotation"
                  expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
          expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

4. In  springDispatcherServlet-servlet.xml (note that it must be this name, the first paragraph can be changed, if the location is not specified, then the location of the spirngMVC configuration file must be under WEB-INF) , the configuration is as follows

<!--配置自动扫描包-->
    <context:component-scan base-package="com.bin.springMVC.*" use-default-filters="false">
        <context:include-filter type="annotation" 
                                expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation"
             expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
<!--配置用 InternalResourceViewResolve
    如果我们在目标方法返回一个"loginoK”则真实的资源就是
    prefix/名字/suffixloginOK => /WEB-INF/pages/loginOk.jsp-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
        </bean>
<!--    加入两个常规配置,才会加载.js .css文件-->
    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:default-servlet-handler/>


5. Effect: After starting the project, each annotation object is only created once.

3. Bean reference

Pay attention to the mutual reference relationship between beans, that is, the controller will refer to the service, but the service will not refer to the controller.

Guess you like

Origin blog.csdn.net/qq_36684207/article/details/135163429