2、Spring Boot-创建一个Web项目

发布时间:2018-05-21 作者:大扑棱蛾子 阅读次数:
版权声明:未经允许不得转载至微信公众号,转载至个人博客请注明出处。 阅读原文

2.1 POM配置

  • 配置方式一
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
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codestd</groupId>
<artifactId>spring-boot-test</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>spring-boot-test Maven Webapp</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.M3</version>
</parent>

<dependencies>
<!-- web项目支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- junit测试支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

</dependencies>

<build>
<finalName>spring-boot-test</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
  • 配置方式二
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
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codestd</groupId>
<artifactId>spring-boot-test</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>spring-boot-test Maven Webapp</name>
<url>http://maven.apache.org</url>

<dependencies>
<!-- web项目支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- junit测试支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.0.M3</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<finalName>spring-boot-test</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

这两种配置的区别在于Spring Boot的引入方式的不用。一种是将SpringBoot作为父项目引入,另一种是只引入SpringBoot依赖。推荐使用第二种

2.2 配置Web项目

SpringBoot抛弃了一切XML配置,使用全注解配置。

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 com.codestd.springboot.app;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

/**
* 主配置
* @author wangchengwei
* @since 1.0.0
*/
@SpringBootApplication(scanBasePackages = "com.codestd.springboot")
public class Application {

private static Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

}

@SpringBootApplication等同于@Configuration,@EnableAutoConfiguration,@ComponentScan,需要通过scanBasePackages来指定要扫描的包。
等同于下面的配置。

1
2
3
4
5
6
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.codestd.springboot")
public class Application {
...
}

Controller定义

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 com.codestd.springboot.controller;

import com.codestd.springboot.base.ResponseMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
* 第一个Springboot控制器
*
* @author wangchengwei
* @since 1.0.0
*/
@RestController
public class MainController {

private static Logger logger = LoggerFactory.getLogger(MainController.class);

@ResponseBody
@RequestMapping("/")
public ResponseMessage index(){
logger.info("The first spring boot application");

return ResponseMessage.success();
}

}

直接运行Applicaton就可以启动项目,默认使用Tomcat,端口号是8080。通过 http://localhost:8080 即可访问项目。

2.3 修改Web容器端口

  • 方法一

新建一个application.properties的配置文件,然后加入下面的配置

1
server.port=8888

然后启动项目,SpringBoot会自动扫描application.properties中的配置,然后以8888端口启动项目。server.port这个key是固定的不能变。

  • 方法二

通过实现EmbeddedServletContainerCustomizer接口。如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Application implements EmbeddedServletContainerCustomizer{

private static Logger logger = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}

@Override
public void customize(ConfigurableEmbeddedServletContainer configurableEmbeddedServletContainer) {
//在这里指定项目端口
configurableEmbeddedServletContainer.setPort(888);
}
}

这种方法的优先级要高于配置文件的方式。

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×