Skip to content

Commit 44d96e7

Browse files
committed
SpringBoot-HelloWorld test
1 parent 024ddac commit 44d96e7

File tree

10 files changed

+270
-21
lines changed

10 files changed

+270
-21
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ local.properties
3838
.scala_dependencies
3939
.worksheet
4040
将删除 .idea/
41+
将删除 SpringBoot-HelloWorld/HelloWorld1.iml
42+
将删除 SpringBoot-HelloWorld/src/main/java/com/
43+
将删除 SpringBoot-HelloWorld/src/main/resources/
44+
将删除 SpringBoot-HelloWorld/src/test/
45+
将删除 SpringBoot-HelloWorld/target/
46+
将删除 target/

README-HelloWorld.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# SpringBoot学习--HellWorld
2+
## 引入pom依赖
3+
--1. spring-boot-starter-parent可以提供依赖管理,引入以后其它的的starter依赖就可以不用配置版本号.
4+
```$xslt
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>1.4.0.RELEASE</version>
9+
</parent>
10+
```
11+
但是这里也有一个问题,如果我们已经有一个自己的parent,那么我们就不能在这么引用了.我们可以使用下面的方式:
12+
```$xslt
13+
<dependencyManagement>
14+
<dependencies>
15+
<dependency>
16+
<!-- Import dependency management from Spring Boot -->
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-dependencies</artifactId>
19+
<version>1.4.0.RELEASE</version>
20+
<type>pom</type>
21+
<scope>import</scope>
22+
</dependency>
23+
</dependencies>
24+
</dependencyManagement>
25+
```
26+
--2.spring-boot-starter-web提供了webmvc,tomcat等web开发相关模块
27+
```$xslt
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-web</artifactId>
32+
</dependency>
33+
</dependencies>
34+
35+
```
36+
##开始HelloWorld
37+
###一个错误的示例
38+
我们需要一个main作为入口来启动我们的项目,但这里我们需要注意我们启动的主类不能放在default包下,
39+
不然会遇到很多莫名的问题,比如下面这个:
40+
```$xslt
41+
2017-03-26 20:06:27.320 WARN 8832 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [HelloWorldAppTest1]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.boot.context.web.ErrorPageFilter]
42+
2017-03-26 20:06:27.330 ERROR 8832 --- [ main] o.s.boot.SpringApplication : Application startup failed
43+
44+
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [HelloWorldAppTest1]; nested exception is org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'errorPageFilter' for bean class [org.springframework.boot.web.support.ErrorPageFilter] conflicts with existing, non-compatible bean definition of same name and class [org.springframework.boot.context.web.ErrorPageFilter]
45+
```
46+
###编写我们的第一个HelloWorld
47+
```$xslt
48+
@SpringBootApplication
49+
@RestController
50+
public class HelloWorldApp2 {
51+
52+
@RequestMapping("/hello")
53+
public String hello() {
54+
return "hello world";
55+
}
56+
57+
@RequestMapping("/")
58+
public String index() {
59+
return "this is index page";
60+
}
61+
62+
/**
63+
* 用于启动项目
64+
*
65+
* @param args
66+
*/
67+
public static void main(String[] args) {
68+
SpringApplication.run(HelloWorldApp2.class, args);
69+
}
70+
71+
}
72+
```
73+
###启动项目
74+
我们只需要启动main方法就可以在浏览器访问localhost:8080/hello,当然我们也可以使用maven命令来启动,我们需要添加下面的插件
75+
```$xslt
76+
<build>
77+
<plugins>
78+
<plugin>
79+
<groupId>org.springframework.boot</groupId>
80+
<artifactId>spring-boot-maven-plugin</artifactId>
81+
<executions>
82+
<execution>
83+
<goals>
84+
<goal>repackage</goal>
85+
</goals>
86+
</execution>
87+
</executions>
88+
</plugin>
89+
</plugins>
90+
</build>
91+
```
92+
然后使用mvn spring-boot:run就可以启动项目了,除此之外,我们也可以使用jar包的方式来启动,
93+
```aidl
94+
java -jar target/SpringBoot-HelloWorld-2-1.0-SNAPSHOT.jar
95+
```
96+

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
# SpringBoot-Study
2-
SpringBoot学习
1+
#SpringBoot

SpringBoot-HelloWorld-1/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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>SpringBoot-Study</artifactId>
7+
<groupId>groupId</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<packaging>jar</packaging>
12+
13+
<artifactId>SpringBoot-HelloWorld-1</artifactId>
14+
<dependencyManagement>
15+
<dependencies>
16+
<dependency>
17+
<!-- Import dependency management from Spring Boot -->
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-dependencies</artifactId>
20+
<version>1.4.0.RELEASE</version>
21+
<type>pom</type>
22+
<scope>import</scope>
23+
</dependency>
24+
</dependencies>
25+
</dependencyManagement>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
</dependencies>
33+
34+
<build>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-maven-plugin</artifactId>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
43+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import org.springframework.boot.autoconfigure.SpringBootApplication;
2+
import org.springframework.web.bind.annotation.RestController;
3+
4+
/**
5+
* 这是一个错误的使用方法
6+
*
7+
* @author lsj <[email protected]>
8+
* @date 17-3-26
9+
*/
10+
@SpringBootApplication
11+
@RestController
12+
public class HelloWorldAppTest1 {
13+
//
14+
// @RequestMapping("/hello")
15+
// public String hello() {
16+
// return "hello world";
17+
// }
18+
//
19+
// @RequestMapping("/")
20+
// public String index() {
21+
// return "this is index page";
22+
// }
23+
//
24+
// /**
25+
// * 用于启动项目
26+
// *
27+
// * @param args
28+
// */
29+
// public static void main(String[] args) {
30+
// SpringApplication.run(HelloWorldAppTest1.class, args);
31+
// }
32+
33+
}

SpringBoot-HelloWorld-2/pom.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>SpringBoot-Study</artifactId>
7+
<groupId>groupId</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<groupId>groupId</groupId>
13+
<artifactId>SpringBoot-HelloWorld-2</artifactId>
14+
<packaging>jar</packaging>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<!-- Import dependency management from Spring Boot -->
20+
<groupId>org.springframework.boot</groupId>
21+
<artifactId>spring-boot-dependencies</artifactId>
22+
<version>1.4.0.RELEASE</version>
23+
<type>pom</type>
24+
<scope>import</scope>
25+
</dependency>
26+
</dependencies>
27+
</dependencyManagement>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
</dependencies>
35+
36+
<build>
37+
<plugins>
38+
<plugin>
39+
<groupId>org.springframework.boot</groupId>
40+
<artifactId>spring-boot-maven-plugin</artifactId>
41+
<executions>
42+
<execution>
43+
<goals>
44+
<goal>repackage</goal>
45+
</goals>
46+
</execution>
47+
</executions>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
53+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com;
2+
3+
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
/**
10+
* @author lsj <[email protected]>
11+
* @date 17-3-26
12+
*/
13+
@SpringBootApplication
14+
@RestController
15+
public class HelloWorldApp2 {
16+
17+
@RequestMapping("/hello")
18+
public String hello() {
19+
return "hello world";
20+
}
21+
22+
@RequestMapping("/")
23+
public String index() {
24+
return "this is index page";
25+
}
26+
27+
/**
28+
* 用于启动项目
29+
*
30+
* @param args
31+
*/
32+
public static void main(String[] args) {
33+
SpringApplication.run(HelloWorldApp2.class, args);
34+
}
35+
36+
}

SpringBoot-HelloWorld/README.md

Lines changed: 0 additions & 2 deletions
This file was deleted.

SpringBoot-HelloWorld/pom.xml

Lines changed: 0 additions & 15 deletions
This file was deleted.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<packaging>pom</packaging>
1010
<version>1.0-SNAPSHOT</version>
1111
<modules>
12-
<module>SpringBoot-HelloWorld</module>
12+
<module>SpringBoot-HelloWorld-1</module>
13+
<module>SpringBoot-HelloWorld-2</module>
1314
</modules>
1415

15-
1616
</project>

0 commit comments

Comments
 (0)