01 Spring HelloWorld

环境:

IDEA 2019.2

jdk 1.8及以上

Maven 3以上

  1. 创建Maven的Web项目,然后导入依赖:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>club.swzdl.spring</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>spring Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<!--测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>

<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>

</dependencies>

<build>
<finalName>spring</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
  1. 在src目录下新建java文件夹,作为java代码的存放文件夹,新建Spring xml描述文件,名为applicationContext.xml

    applicationContext

  2. 在src目录下新建包Hello,在包下新建类HelloWorld.java

    HelloWorld.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    package hello;/*
    * @Author Sheng WenZeng
    * @Date 2019/8/4 15:22
    * @Version 1.0
    */

    /**
    * @author Sheng Wenzeng
    * @ClassName HelloWorld
    * @Description TODO
    * @Date 2019/8/4 15:22
    * @Version 1.0
    */
    public class HelloWorld {
    private String name_1;
    private String name_2;

    void hello() {
    System.out.println(name_1 + name_2);
    }

    public void setName_1(String name_1) {
    this.name_1 = name_1;
    }

    public void setName_2(String name_2) {
    this.name_2 = name_2;
    }
    }
  3. 在applicationContext.xml中写入bean

    applicationContext.xml:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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-->
    <bean id="helloWord" class="hello.HelloWorld">
    <property name="name_1" value="Hello"/>
    <property name="name_2" value="World"/>
    </bean>
    </beans>
  4. 新建Hello类

    Hello.java:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    package hello;
    /*
    * @Author Sheng WenZeng
    * @Date 2019/8/4 15:24
    * @Version 1.0
    */

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
    * @author Sheng Wenzeng
    * @ClassName Hello
    * @Description TODO
    * @Date 2019/8/4 15:24
    * @Version 1.0
    */
    public class Hello {
    public static void main(String[] args) {
    //这一步会自动执行HelloWorld的构造方法,并将name值通过其中的set方法赋给name
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWord");
    helloWorld.hello();
    }
    }
  5. 运行Hello.java

    1
    2
    3
    4
    "C:\Program Files\Java\jdk1.8.0_211\bin\java.exe" ...
    HelloWorld

    Process finished with exit code 0

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×