Framework integration-steps and advantages of Spring and SpringMVC framework integration, springspringmvc

Source: Internet
Author: User

Framework integration-steps and advantages of Spring and SpringMVC framework integration, springspringmvc
Spring and SpringMVC integration!

Q: Does SpringMVC actually run in the Spring environment and need to be integrated? SpringMVC and Spring both have IOC containers. Do they both need to be retained?

The answer is: Normally, other frameworks like data sources, transactions, and integration are put in the spring configuration file (rather than in the SpringMVC configuration file ), in fact, there are services and Dao in the IOC container corresponding to the Spring configuration file. springMVC also runs its own IOC container, and only configures its own Handler (Controller) Information in SpringMVC container. Therefore, it is necessary to integrate the two. SpringMVC is responsible for receiving requests sent from pages, and the Spring framework is responsible for organizing the intermediate requirement logic and sending operation requests to the database, currently, database operations are performed using the JdbcTemplate in the Spring framework.

1. Import all jar packages of Spring and SpringMVC

C3p0-0.9.1.2.jar

Com.springsource.net. sf. cglib-2.2.0.jar

Com.springsource.org. aopalliance-1.0.0.jar

Com.springsource.org. aspectj. weaver-1.6.8.RELEASE.jar

Commons-logging-1.1.3.jar

Mysql-connector-java-5.1.37-bin.jar

Spring-aop-4.0.0.RELEASE.jar

Spring-aspects-4.0.0.RELEASE.jar

Spring-beans-4.0.0.RELEASE.jar

Spring-context-4.0.0.RELEASE.jar

Spring-core-4.0.0.RELEASE.jar

Spring-expression-4.0.0.RELEASE.jar

Spring-jdbc-4.0.0.RELEASE.jar

Spring-orm-4.0.0.RELEASE.jar

Spring-tx-4.0.0.RELEASE.jar

Spring-web-4.0.0.RELEASE.jar

Spring-webmvc-4.0.0.RELEASE.jar

 

2. Configure SpringMVC and Spring in the web. xml file respectively.

  

<! -- ContextLoaderListener loads the IOC container. The bottom layer of the Spring framework is listener --> <context-param> <param-name> contextConfigLocation </param-name> <! -- Specify the path and name of the Spring configuration file --> <param-value> classpath: Spring. xml </param-value> </context-param> <! -- Bootstraps the root web application context before servlet initialization --> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <! -- SpringMVC configuration file SpringMVC is servlet at the underlying layer --> <servlet-name> springDispatcherServlet </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <! -- Specify the path and name of the SpringMVC framework configuration file --> <param-value> classpath: SpringMVC. xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Map all requests to the DispatcherServlet for handling --> <servlet-mapping> <servlet-name> springDispatcherServlet </servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <! -- Set encoding to handle post request garbled characters --> <filter-name> CharacterEncodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> </filter> <filter-mapping> <filter-name> CharacterEncodingFilter </filter-name> <url-pattern>/* </url-pattern> </filter- mapping> <! -- Post requests are converted to put and delete --> <filter-name> HiddenHttpMethodFilter </filter-name> <filter-class> org. springframework. web. filter. hiddenHttpMethodFilter </filter-class> </filter> <filter-mapping> <filter-name> HiddenHttpMethodFilter </filter-name> <url-pattern>/* </url-pattern> </filter-mapping>

  

3. Configure the spring configuration file and spring MVC configuration file:

 

<! -- Configure the scan package --> <context: component-scan base-package = "com. neuedu"> </context: component-scan> <! -- Load information in the properties file --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure the data source --> <bean id = "comboPooledDataSource" class = "com. mchange. v2.c3p0. comboPooledDataSource "> <property name =" user "value =" $ {jdbc. user} "> </property> <property name =" password "value =" $ {jdbc. passowrd} "> </property> <property name =" jdbcUrl "value =" $ {jdbc. url} "> </property> <property name =" driverClass "value =" $ {jdbc. driver} "> </property> </bean> <! -- Configure the bean corresponding to JdbcTemplate and assemble the dataSource data source Attributes --> <bean id = "jdbcTemplate" class = "org. springframework. jdbc. core. jdbcTemplate "> <property name =" dataSource "ref =" comboPooledDataSource "> </property> </bean> <! -- To execute an SQL statement with a specified parameter, You need to configure NamedParameterJdbcTemplate --> <! -- The NamedParameterJdbcTemplate class does not have a parameter-free constructor. You need to input a JdbcTemplate object or data source object [DataSource] --> <bean id = "namedParameterJdbcTemplate" class = "org. springframework. jdbc. core. namedparam. namedParameterJdbcTemplate "> <! -- The property tag cannot be used for configuration. --> <constructor-arg ref = "jdbcTemplate"> </constructor-arg> </bean>

 

  

 

Springmvc configuration file:
<! -- Configure the scan package --> <context: component-scan base-package = "com. neuedu"> </context: component-scan> <! -- Configure the view parser --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/views/"> </property> <property name =" suffix "value = ". jsp "> </property> </bean> <! -- Can process static resources --> <mvc: default-servlet-handler/> <! -- You can directly request a page to reach another page without using controller --> <! -- <Mvc: view-controller path = "/" view-name = "/"/> --> <! -- If only mvc: default does not have mvc: annotation-driven, normal requestMapping cannot be accessed, so the two are standard --> <mvc: annotation-driven> </mvc: annotation-driven>

  

Add the jdbc. properties file:
jdbc.user=root
jdbc.passowrd=123456
jdbc.url=jdbc:mysql://localhost:3306/jdbc_template
jdbc.driver=com.mysql.jdbc.Driver

  

4. Create a Controller class and a Service class, and create a no-argument constructor for the two classes to output a sentence respectively! 5. Start the project and you will find that both the controller constructor and the service constructor have been executed twice!

Problem: If Spring's IOC container and SpringMVC's IOC container scan overlap, some beans will be created twice!

Solution:

1. Make the Spring IOC container scan package and SpringMVC IOC container scan package have no overlap part!

The controller layer is in the controller package, and the service layer is in the service package.

2. However, sometimes it is developed by modules, which is not easy to achieve. Therefore:

You can use the following sub-labels under the component-scan label to specify the annotation that can only be scanned:

<context:component-scan base-package="com.neuedu">  <context:exclude-filter type="annotation" expression=""/>  <context:include-filter type="annotation" expression=""/></context:component-scan>

  

Therefore, in the springMVC configuration file, we can follow the following configuration to scan only the controller and ControllerAdvice Annotations:

<Context: component-scan base-package = "com. neuedu" use-default-filters = "false"> <! -- Scan annotation for @ Controller class --> <context: include-filter type = "annotation" expression = "org. springframework. stereotype. Controller"/> <! -- ControllerAdvice annotation is used to handle global exceptions and can be marked on the class. Therefore, the scan annotation is the @ ControllerAdvice class --> <context: include-filter type = "annotation" expression = "org. springframework. web. bind. annotation. controllerAdvice "/> </context: component-scan>

  

In the spring configuration file:

<Context: component-scan base-package = "com. neuedu"> <! -- Scan the class annotated with @ Controller --> <context: exclude-filter type = "annotation" expression = "org. springframework. stereotype. Controller"/> <! -- Scan the class with the annotation @ ControllerAdvice --> <context: exclude-filter type = "annotation" expression = "org. springframework. web. bind. annotation. controllerAdvice "/> </context: component-scan>

Restart the project and you will find that both spring and springmvc objects have been created!

 

6. Relationship Between Spring IOC container and SpringMVC IOC container

Outside beans. xml is the Spring configuration file, the yellow square Spring-mvc.xml is the SpringMVC configuration file

Note:

1. the bean in the SpringMVC container can reference the bean in the Spring container, that is, we can inject the service layer object into the Controller. [You can print the service object in the requestMapping method of the controller layer ]!

2. vice versa. That is to say: the service layer of spring scan cannot reference the handler (Controller) object of springmvc. [Note a small example and an error will occur when the project is started]

3. In fact, the Spring container has a parent-child relationship with the Spring container. [Reference picture] Just like a son can inherit the father's genes, his father cannot inherit the son's genes!

In addition, the Handler (Controller) can depend on the Service layer, but the Service layer cannot depend on the Handler (Controller) layer!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.