Using WEB through Spring on the basis of Maven

The difference between WEB and JAVA in Spring (based on maven):

  1. Different jar packages:
  • Normal java project:
 <dependency>                              
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>
  • WEB:
<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

2. Creation of IOC container:

  1. Direct creation of non WEB application in main method
  2. When the WEB application loads the WEB container (that is, the server), it creates an ICO container: that is, it creates an IOC container in the Listener's contextInitialized(ServletContextEvent servletContextEvent) method

3. Access to IOC container:

  1. In web, after IOC is created in context initialized (servletcontextevent servletcontextevent), it can be placed in ServletContext (application domain or global object of web (my understanding))

4. The name and location of the spring configuration file should also be configurable. It is more suitable to configure it into the current web application initialization parameters

Steps:

1. Configure pom.xml:

  <dependencies>
<!--servlet-->

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!--jsp-->
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
    </dependency>
<!--spring stay web Configuration-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.2.3.RELEASE</version>
    </dependency>

  </dependencies>

2. For convenience, configure through web.xml
web.xml:

<!--To configure Spring Name and location of the profile-->

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath*:application.xml</param-value>
</context-param>


  <listener>
    <listener-class>Com.Listener.SpringServletListener</listener-class>
  </listener>

application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="testBean" class="Com.Beans.testBean">
        <property name="a" value="2"></property>
    </bean>
</beans>

Listner:

public class SpringServletListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //0. Get the Spring configuration file

        ServletContext servletContext = servletContextEvent.getServletContext();


        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");



        //1. Create the IOC when the servletContextEvent is created at the beginning of the server

        //2. create IOC
        ApplicationContext ctx=new ClassPathXmlApplicationContext(contextConfigLocation);


        servletContext.setAttribute("ApplicationContext", ctx);





    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {


    }
}

Then we can get it

Through the Listener provided by Spring:

web.xml: Get through the shortcut getWebApplicationContext (I got it here in the domain of Jsp)
Jsp shortcut object:

Published 6 original articles, won praise 0, visited 259
Private letter follow

Tags: Spring xml JSP Java

Posted on Sat, 14 Mar 2020 09:09:58 -0700 by scrypte