Spring-快速开始

maven构建Spring的简单样例

pom.xml

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
    <modelVersion>4.0.0</modelVersion>
6
7
    <groupId>org.example</groupId>
8
    <artifactId>SpringTest</artifactId>
9
    <version>1.0-SNAPSHOT</version>
10
    <dependencies>
11
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
12
        <dependency>
13
            <groupId>commons-logging</groupId>
14
            <artifactId>commons-logging</artifactId>
15
            <version>1.1.1</version>
16
        </dependency>
17
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
18
        <dependency>
19
            <groupId>org.springframework</groupId>
20
            <artifactId>spring-aop</artifactId>
21
            <version>4.1.6.RELEASE</version>
22
        </dependency>
23
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
24
        <dependency>
25
            <groupId>org.springframework</groupId>
26
            <artifactId>spring-aspects</artifactId>
27
            <version>4.1.6.RELEASE</version>
28
        </dependency>
29
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
30
        <dependency>
31
            <groupId>org.springframework</groupId>
32
            <artifactId>spring-beans</artifactId>
33
            <version>4.1.6.RELEASE</version>
34
        </dependency>
35
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
36
        <dependency>
37
            <groupId>org.springframework</groupId>
38
            <artifactId>spring-context</artifactId>
39
            <version>4.1.6.RELEASE</version>
40
        </dependency>
41
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
42
        <dependency>
43
            <groupId>org.springframework</groupId>
44
            <artifactId>spring-context-support</artifactId>
45
            <version>4.1.6.RELEASE</version>
46
        </dependency>
47
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
48
        <dependency>
49
            <groupId>org.springframework</groupId>
50
            <artifactId>spring-core</artifactId>
51
            <version>4.1.6.RELEASE</version>
52
        </dependency>
53
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-expression -->
54
        <dependency>
55
            <groupId>org.springframework</groupId>
56
            <artifactId>spring-expression</artifactId>
57
            <version>4.1.6.RELEASE</version>
58
        </dependency>
59
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument -->
60
        <dependency>
61
            <groupId>org.springframework</groupId>
62
            <artifactId>spring-instrument</artifactId>
63
            <version>4.1.6.RELEASE</version>
64
        </dependency>
65
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-instrument-tomcat -->
66
        <dependency>
67
            <groupId>org.springframework</groupId>
68
            <artifactId>spring-instrument-tomcat</artifactId>
69
            <version>4.1.6.RELEASE</version>
70
        </dependency>
71
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
72
        <dependency>
73
            <groupId>org.springframework</groupId>
74
            <artifactId>spring-jdbc</artifactId>
75
            <version>4.1.6.RELEASE</version>
76
        </dependency>
77
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
78
        <dependency>
79
            <groupId>org.springframework</groupId>
80
            <artifactId>spring-jms</artifactId>
81
            <version>4.1.6.RELEASE</version>
82
        </dependency>
83
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-messaging -->
84
        <dependency>
85
            <groupId>org.springframework</groupId>
86
            <artifactId>spring-messaging</artifactId>
87
            <version>4.1.6.RELEASE</version>
88
        </dependency>
89
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
90
        <dependency>
91
            <groupId>org.springframework</groupId>
92
            <artifactId>spring-orm</artifactId>
93
            <version>4.1.6.RELEASE</version>
94
        </dependency>
95
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
96
        <dependency>
97
            <groupId>org.springframework</groupId>
98
            <artifactId>spring-oxm</artifactId>
99
            <version>4.1.6.RELEASE</version>
100
        </dependency>
101
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
102
        <dependency>
103
            <groupId>org.springframework</groupId>
104
            <artifactId>spring-test</artifactId>
105
            <version>4.1.6.RELEASE</version>
106
            <scope>test</scope>
107
        </dependency>
108
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
109
        <dependency>
110
            <groupId>org.springframework</groupId>
111
            <artifactId>spring-tx</artifactId>
112
            <version>4.1.6.RELEASE</version>
113
        </dependency>
114
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
115
        <dependency>
116
            <groupId>org.springframework</groupId>
117
            <artifactId>spring-web</artifactId>
118
            <version>4.1.6.RELEASE</version>
119
        </dependency>
120
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
121
        <dependency>
122
            <groupId>org.springframework</groupId>
123
            <artifactId>spring-webmvc</artifactId>
124
            <version>4.1.6.RELEASE</version>
125
        </dependency>
126
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc-portlet -->
127
        <dependency>
128
            <groupId>org.springframework</groupId>
129
            <artifactId>spring-webmvc-portlet</artifactId>
130
            <version>4.1.6.RELEASE</version>
131
        </dependency>
132
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-websocket -->
133
        <dependency>
134
            <groupId>org.springframework</groupId>
135
            <artifactId>spring-websocket</artifactId>
136
            <version>4.1.6.RELEASE</version>
137
        </dependency>
138
139
    </dependencies>
140
141
</project>

创建Java类

HelloWorld

1
package com.spring.test;
2
3
public class HelloWorld {
4
    private String message;
5
    public void setMessage(String message) {
6
        this.message = message;
7
    }
8
    public void getMassage() {
9
        System.out.println("Message :" + message);
10
    }
11
}

Main

1
package com.spring.test;
2
3
import org.springframework.context.ApplicationContext;
4
import org.springframework.context.support.ClassPathXmlApplicationContext;
5
6
public class Main {
7
    public static void main(String[] args) {
8
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
9
        HelloWorld obj = (HelloWorld)context.getBean("helloWorld");
10
        obj.getMassage();
11
    }
12
}

编写Beans.xml

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
       xsi:schemaLocation="http://www.springframework.org/schema/beans
6
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
7
8
    <bean id="helloWorld" class="com.spring.test.HelloWorld">
9
        <property name="message" value="Hello World!"/>
10
    </bean>
11
12
</beans>

问题:找不到Beans.xml

报BeanDefinitionStoreException异常

解决办法:pom.xml添加如下:

1
<build>
2
        <resources>
3
            <resource>
4
                <directory>src/main/java</directory>
5
                <includes>
6
                    <include>**/*.xml</include>
7
                </includes>
8
            </resource>
9
            <resource>
10
                <directory>src/main/resources</directory>
11
            </resource>
12
        </resources>
13
    </build>

总结

文件目录

1
pom.xml // maven配置文件,导入依赖、设置build文件
2
src
3
--main
4
  --java // 存放Java类
5
    --com.spring.test
6
      --HelloWorld.java
7
      --Main.java
8
  --resources // 存放资源文件
9
    --Beans.xml
10
--test
11
target // 存放编译后的文件

xml文件中头部的一些元素的解释

  • xmlns:默认命名空间
  • xmlns:xsi:业界默认的用于 XSD((XML Schema Definition) 文件的命名空间
  • xsi:schemaLocation:XSD文件的位置
  • XSD文件:XSD 文件(也常常称为 Schema 文件)是用来定义 xml 文档结构的

参考资料

https://www.w3cschool.cn/wkspring/dgte1ica.html

https://blog.csdn.net/lengxiao1993/article/details/77914155

  • © 2020 QSH
  • Powered by Hexo Theme Ayer
  • PV: UV:

请我喝杯咖啡吧~

支付宝
微信