Bladeren bron

spring webdeng

zhang 2 jaren geleden
bovenliggende
commit
8b616df7e1

+ 2 - 0
pom.xml

@@ -10,6 +10,8 @@
10 10
     <version>1.0-SNAPSHOT</version>
11 11
     <modules>
12 12
         <module>spring_annotation</module>
13
+        <module>spring_mvc</module>
14
+        <module>springWeb</module>
13 15
     </modules>
14 16
 
15 17
     <properties>

+ 59 - 0
springWeb/pom.xml

@@ -0,0 +1,59 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!--
3
+  Licensed to the Apache Software Foundation (ASF) under one
4
+  or more contributor license agreements.  See the NOTICE file
5
+  distributed with this work for additional information
6
+  regarding copyright ownership.  The ASF licenses this file
7
+  to you under the Apache License, Version 2.0 (the
8
+  "License"); you may not use this file except in compliance
9
+  with the License.  You may obtain a copy of the License at
10
+
11
+   http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+  Unless required by applicable law or agreed to in writing,
14
+  software distributed under the License is distributed on an
15
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+  KIND, either express or implied.  See the License for the
17
+  specific language governing permissions and limitations
18
+  under the License.
19
+-->
20
+<!-- $Id: pom.xml 642118 2008-03-28 08:04:16Z reinhard $ -->
21
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
22
+
23
+  <modelVersion>4.0.0</modelVersion>
24
+  <packaging>war</packaging>
25
+
26
+  <name>springWeb</name>
27
+  <groupId>org.example</groupId>
28
+  <artifactId>springWeb</artifactId>
29
+  <version>1.0-SNAPSHOT</version>
30
+
31
+  <build>
32
+    <plugins>
33
+      <plugin>
34
+        <groupId>org.mortbay.jetty</groupId>
35
+        <artifactId>maven-jetty-plugin</artifactId>
36
+        <version>6.1.7</version>
37
+        <configuration>
38
+          <connectors>
39
+            <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
40
+              <port>8888</port>
41
+              <maxIdleTime>30000</maxIdleTime>
42
+            </connector>
43
+          </connectors>
44
+          <webAppSourceDirectory>${project.build.directory}/${pom.artifactId}-${pom.version}</webAppSourceDirectory>
45
+          <contextPath>/</contextPath>
46
+        </configuration>
47
+      </plugin>
48
+    </plugins>
49
+  </build>
50
+
51
+  <dependencies>
52
+    <!--dependency>
53
+      <groupId>org.example</groupId>
54
+      <artifactId>[the artifact id of the block to be mounted]</artifactId>
55
+      <version>1.0-SNAPSHOT</version>
56
+    </dependency-->
57
+  </dependencies>
58
+
59
+</project>

+ 1 - 1
spring_annotation/src/main/java/com/dao/imp/StudentDaoImpl.java

@@ -3,7 +3,7 @@ package com.dao.imp;
3 3
 import com.dao.StudentDao;
4 4
 import org.springframework.stereotype.Repository;
5 5
 
6
-//<bean id="studentDao" class="com.dao.imp.StudentDaoImpl"></bean>
6
+//<bean id="studentDao" class="com.com.dao.imp.StudentDaoImpl"></bean>
7 7
 @Repository
8 8
 public class StudentDaoImpl implements StudentDao {
9 9
     @Override

+ 32 - 0
spring_mvc/pom.xml

@@ -0,0 +1,32 @@
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
+        <dependency>
20
+            <groupId>javax.servlet</groupId>
21
+            <artifactId>javax.servlet-api</artifactId>
22
+            <version>4.0.1</version>
23
+            <scope>provided</scope>
24
+        </dependency>
25
+        <dependency>
26
+            <groupId>javax.servlet.jsp</groupId>
27
+            <artifactId>javax.servlet.jsp-api</artifactId>
28
+            <version>2.3.3</version>
29
+            <scope>provided</scope>
30
+        </dependency>
31
+    </dependencies>
32
+</project>

+ 10 - 0
spring_mvc/src/main/java/com/config/SpringConfig.java

@@ -0,0 +1,10 @@
1
+package com.config;
2
+
3
+import org.springframework.context.annotation.ComponentScan;
4
+import org.springframework.context.annotation.Configuration;
5
+
6
+@Configuration
7
+@ComponentScan({"com"})
8
+public class SpringConfig {
9
+
10
+}

+ 5 - 0
spring_mvc/src/main/java/com/dao/UserDao.java

@@ -0,0 +1,5 @@
1
+package com.dao;
2
+
3
+public interface UserDao {
4
+    void save();
5
+}

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

@@ -0,0 +1,12 @@
1
+package com.dao.impl;
2
+
3
+import com.dao.UserDao;
4
+import org.springframework.stereotype.Repository;
5
+
6
+@Repository
7
+public class UserDaoImpl implements UserDao {
8
+    @Override
9
+    public void save() {
10
+        System.out.println("dao save");
11
+    }
12
+}

+ 5 - 0
spring_mvc/src/main/java/com/service/UserService.java

@@ -0,0 +1,5 @@
1
+package com.service;
2
+
3
+public interface UserService {
4
+    void saveUser();
5
+}

+ 16 - 0
spring_mvc/src/main/java/com/service/impl/UserServiceImpl.java

@@ -0,0 +1,16 @@
1
+package com.service.impl;
2
+
3
+import com.dao.UserDao;
4
+import com.service.UserService;
5
+import org.springframework.beans.factory.annotation.Autowired;
6
+import org.springframework.stereotype.Service;
7
+
8
+@Service
9
+public class UserServiceImpl implements UserService {
10
+    @Autowired
11
+    private UserDao userDao;
12
+    @Override
13
+    public void saveUser() {
14
+        userDao.save();
15
+    }
16
+}

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

@@ -0,0 +1,20 @@
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
+}

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

@@ -0,0 +1,23 @@
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>

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

@@ -0,0 +1,18 @@
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
+}