zhangxiaoyu 2 gadi atpakaļ
vecāks
revīzija
1eac954463

+ 0 - 1
pom.xml

@@ -10,7 +10,6 @@
10 10
     <version>1.0-SNAPSHOT</version>
11 11
     <modules>
12 12
         <module>spring_annotation</module>
13
-        <module>spring_mvc</module>
14 13
         <module>springMvc</module>
15 14
     </modules>
16 15
 

spring_mvc/src/main/java/com/config/SpringConfig.java → springMvc/src/main/java/com/config/SpringConfig.java


spring_mvc/src/main/java/com/dao/UserDao.java → springMvc/src/main/java/com/dao/UserDao.java


+ 0 - 1
spring_mvc/src/main/java/com/dao/impl/UserDaoImpl.java

@@ -5,7 +5,6 @@ import org.springframework.stereotype.Repository;
5 5
 
6 6
 @Repository
7 7
 public class UserDaoImpl implements UserDao {
8
-    @Override
9 8
     public void save() {
10 9
         System.out.println("dao save");
11 10
     }

+ 22 - 0
springMvc/src/main/java/com/listener/ContextLoadListener.java

@@ -0,0 +1,22 @@
1
+package com.listener;
2
+
3
+import com.config.SpringConfig;
4
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
5
+
6
+import javax.servlet.ServletContext;
7
+import javax.servlet.ServletContextEvent;
8
+import javax.servlet.ServletContextListener;
9
+
10
+public class ContextLoadListener implements ServletContextListener {
11
+    public void contextInitialized(ServletContextEvent sce) {
12
+//        ServletContextListener.super.contextInitialized(sce);
13
+        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(SpringConfig.class);
14
+        ServletContext servletContext = sce.getServletContext();
15
+        servletContext.setAttribute("app",annotationConfigApplicationContext);
16
+        System.out.println("servletContextLoader");
17
+    }
18
+
19
+    public void contextDestroyed(ServletContextEvent sce) {
20
+//        ServletContextListener.super.contextDestroyed(sce);
21
+    }
22
+}

spring_mvc/src/main/java/com/service/UserService.java → springMvc/src/main/java/com/service/UserService.java


spring_mvc/src/main/java/com/service/impl/UserServiceImpl.java → springMvc/src/main/java/com/service/impl/UserServiceImpl.java


+ 29 - 0
springMvc/src/main/java/com/web/UserServlet.java

@@ -0,0 +1,29 @@
1
+package com.web;
2
+
3
+import com.config.SpringConfig;
4
+import com.service.UserService;
5
+import org.springframework.context.ApplicationContext;
6
+import org.springframework.context.annotation.AnnotationConfigApplicationContext;
7
+
8
+import javax.servlet.ServletContext;
9
+import javax.servlet.ServletException;
10
+import javax.servlet.http.HttpServlet;
11
+import javax.servlet.http.HttpServletRequest;
12
+import javax.servlet.http.HttpServletResponse;
13
+import java.io.IOException;
14
+import java.io.PrintWriter;
15
+
16
+public class UserServlet extends HttpServlet {
17
+    @Override
18
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
19
+        //super.doGet(req, resp);
20
+        ServletContext servletContext = req.getServletContext();
21
+        ApplicationContext app = (ApplicationContext)servletContext.getAttribute("app");
22
+        UserService bean = app.getBean(UserService.class);
23
+        bean.saveUser();
24
+        // Hello
25
+        PrintWriter out = resp.getWriter();
26
+        out.println("hello");
27
+
28
+    }
29
+}

+ 0 - 17
springMvc/src/main/java/web/UserServlet.java

@@ -1,17 +0,0 @@
1
-package web;
2
-
3
-import org.springframework.context.support.ClassPathXmlApplicationContext;
4
-
5
-import javax.servlet.ServletException;
6
-import javax.servlet.http.HttpServlet;
7
-import javax.servlet.http.HttpServletRequest;
8
-import javax.servlet.http.HttpServletResponse;
9
-import java.io.IOException;
10
-
11
-public class UserServlet extends HttpServlet {
12
-    @Override
13
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
14
-        //super.doGet(req, resp);
15
-        System.out.println("test");
16
-    }
17
-}

+ 19 - 39
springMvc/src/main/webapp/WEB-INF/applicationContext.xml

@@ -1,43 +1,23 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2
-<!--
3
-  Licensed to the Apache Software Foundation (ASF) under one or more
4
-  contributor license agreements.  See the NOTICE file distributed with
5
-  this work for additional information regarding copyright ownership.
6
-  The ASF licenses this file to You under the Apache License, Version 2.0
7
-  (the "License"); you may not use this file except in compliance with
8
-  the License.  You may obtain a copy of the License at
9 2
 
10
-      http://www.apache.org/licenses/LICENSE-2.0
11
-
12
-  Unless required by applicable law or agreed to in writing, software
13
-  distributed under the License is distributed on an "AS IS" BASIS,
14
-  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
-  See the License for the specific language governing permissions and
16
-  limitations under the License.
17
--->
18
-<!-- @version $Id: applicationContext.xml 561608 2007-08-01 00:33:12Z vgritsenko $ -->
19 3
 <beans xmlns="http://www.springframework.org/schema/beans"
20 4
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21
-       xmlns:util="http://www.springframework.org/schema/util"
22
-       xmlns:configurator="http://cocoon.apache.org/schema/configurator"
23
-       xmlns:avalon="http://cocoon.apache.org/schema/avalon"
24
-       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
25
-                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
26
-                           http://cocoon.apache.org/schema/configurator http://cocoon.apache.org/schema/configurator/cocoon-configurator-1.0.1.xsd
27
-                           http://cocoon.apache.org/schema/avalon http://cocoon.apache.org/schema/avalon/cocoon-avalon-1.0.xsd">
28
-
29
-  <!-- Activate Cocoon Spring Configurator -->
30
-  <configurator:settings/>
31
-
32
-  <!-- Configure Log4j -->
33
-  <bean name="org.apache.cocoon.spring.configurator.log4j"
34
-        class="org.apache.cocoon.spring.configurator.log4j.Log4JConfigurator"
35
-        scope="singleton">
36
-    <property name="settings" ref="org.apache.cocoon.configuration.Settings"/>
37
-    <property name="resource" value="/WEB-INF/log4j.xml"/>
38
-  </bean>
39
-
40
-  <!-- Activate Avalon Bridge -->
41
-  <avalon:bridge/>
42
-
43
-</beans>
5
+       xmlns:context="http://www.springframework.org/schema/context"
6
+       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
+       http://www.springframework.org/schema/beans/spring-beans.xsd
8
+       http://www.springframework.org/schema/context
9
+       https://www.springframework.org/schema/context/spring-context.xsd">
10
+  <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
+  <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
+  <!--    </bean>-->
13
+  <!--    加载外部 properties文件-->
14
+  <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
+  <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
+  <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
+  <!--        <property name="url" value="${jdbc.url}"></property>-->
18
+  <!--        <property name="username" value="${jdbc.username}"></property>-->
19
+  <!--        <property name="password" value="${jdbc.password}"></property>-->
20
+  <!--    </bean>-->
21
+  <!--    组件扫描-->
22
+  <context:component-scan base-package="com"></context:component-scan>
23
+</beans>

+ 5 - 2
springMvc/src/main/webapp/WEB-INF/web.xml

@@ -25,16 +25,19 @@
25 25
          xmlns="http://java.sun.com/xml/ns/j2ee"
26 26
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
27 27
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
28
+  <listener>
29
+    <listener-class>com.listener.ContextLoadListener</listener-class>
30
+  </listener>
28 31
 
29 32
   <!-- Servlet Filters ================================================ -->
30 33
   <servlet>
31 34
     <servlet-name>User</servlet-name>
32
-    <servlet-class>web.UserServlet</servlet-class>
35
+    <servlet-class>com.web.UserServlet</servlet-class>
33 36
     
34 37
   </servlet>
35 38
   <servlet-mapping>
36 39
     <servlet-name>User</servlet-name>
37
-    <url-pattern>/</url-pattern>
40
+    <url-pattern>/user</url-pattern>
38 41
   </servlet-mapping>
39 42
 
40 43
 </web-app>

+ 0 - 21
spring_mvc/pom.xml

@@ -1,21 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-<project xmlns="http://maven.apache.org/POM/4.0.0"
3
-         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5
-    <parent>
6
-        <artifactId>spring_demo</artifactId>
7
-        <groupId>org.example</groupId>
8
-        <version>1.0-SNAPSHOT</version>
9
-    </parent>
10
-    <modelVersion>4.0.0</modelVersion>
11
-
12
-    <artifactId>spring_mvc</artifactId>
13
-
14
-    <properties>
15
-        <maven.compiler.source>8</maven.compiler.source>
16
-        <maven.compiler.target>8</maven.compiler.target>
17
-    </properties>
18
-    <dependencies>
19
-
20
-    </dependencies>
21
-</project>

+ 0 - 20
spring_mvc/src/main/java/com/web/UserServlet.java

@@ -1,20 +0,0 @@
1
-package com.web;
2
-
3
-import com.service.UserService;
4
-import org.springframework.context.support.ClassPathXmlApplicationContext;
5
-
6
-import javax.servlet.ServletException;
7
-import javax.servlet.http.HttpServlet;
8
-import javax.servlet.http.HttpServletRequest;
9
-import javax.servlet.http.HttpServletResponse;
10
-import java.io.IOException;
11
-
12
-public class UserServlet extends HttpServlet {
13
-    @Override
14
-    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
15
-        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("context.xml");
16
-        UserService bean = classPathXmlApplicationContext.getBean(UserService.class);
17
-        bean.saveUser();
18
-        //super.doGet(req, resp);
19
-    }
20
-}

+ 0 - 23
spring_mvc/src/main/resources/context.xml

@@ -1,23 +0,0 @@
1
-<?xml version="1.0" encoding="UTF-8"?>
2
-
3
-<beans xmlns="http://www.springframework.org/schema/beans"
4
-       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5
-       xmlns:context="http://www.springframework.org/schema/context"
6
-       xsi:schemaLocation="http://www.springframework.org/schema/beans
7
-       http://www.springframework.org/schema/beans/spring-beans.xsd
8
-       http://www.springframework.org/schema/context
9
-       https://www.springframework.org/schema/context/spring-context.xsd">
10
-    <!--    <bean id="userService1" class="com.service.impl.UserServiceImpl">-->
11
-    <!--&lt;!&ndash;        <constructor-arg name="userDao" ref="UserDao"></constructor-arg>&ndash;&gt;-->
12
-    <!--    </bean>-->
13
-    <!--    加载外部 properties文件-->
14
-    <!--    <context:property-placeholder location="jdbc.properties"></context:property-placeholder>-->
15
-    <!--    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">-->
16
-    <!--        <property name="driverClassName" value="${jdbc.driver}"></property>-->
17
-    <!--        <property name="url" value="${jdbc.url}"></property>-->
18
-    <!--        <property name="username" value="${jdbc.username}"></property>-->
19
-    <!--        <property name="password" value="${jdbc.password}"></property>-->
20
-    <!--    </bean>-->
21
-    <!--    组件扫描-->
22
-    <context:component-scan base-package="com"></context:component-scan>
23
-</beans>

+ 0 - 18
spring_mvc/src/test/java/SpringUnitTest.java

@@ -1,18 +0,0 @@
1
-import com.config.SpringConfig;
2
-import com.service.UserService;
3
-import org.junit.Test;
4
-import org.junit.runner.RunWith;
5
-import org.springframework.beans.factory.annotation.Autowired;
6
-import org.springframework.test.context.ContextConfiguration;
7
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
8
-
9
-@RunWith(SpringJUnit4ClassRunner.class)
10
-@ContextConfiguration(classes = {SpringConfig.class})
11
-public class SpringUnitTest {
12
-    @Autowired
13
-    UserService userService;
14
-    @Test
15
-    public void test() {
16
-        userService.saveUser();
17
-    }
18
-}