一.建立maven工程
①File——New——Other
②选择Maven Project后,点击Next
③
④.输入Group Id、Artifact Id
⑤.点击Finish后,建立工程如下:
二、配置工程:
①.鼠标右键点击工程目录,新增Package,起名为resources (这个文件夹放配置文件用 ,其实不用这个spring boot也能启动)
②.在resources文件夹下,增加文件application.properties,内容如下
server.port是启动的端口。
同样建立conf目录
把resouces和conf目录加到资源目录中
③.更改pom.xml文件
<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>com.demo.springboot</groupId>
<artifactId>spring-boot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- 打jar包时候用 -->
<build>
<resources>
<resource>
<directory>resources</directory>
<!-- 打包包含 -->
<includes>
<include>application.properties</include>
</includes>
<!-- 打包排除 -->
<excludes>
<exclude>*.xml</exclude>
<exclude>*.yaml</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 资源文件拷贝到jar以外 -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<!-- 拷贝目标目录,打包时候,拷贝到jar外边的conf,这样该配置的时候就不用更改jar了 -->
<outputDirectory>${project.build.directory}/conf</outputDirectory>
<resources>
<resource>
<!-- 配置文件目录 -->
<directory>conf</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
④.右键点工程Maven——Update Project
⑤.新建Application.java作为main入口,@RequestMapping("/")作http访问用根目录地址
package com.neusoft;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@EnableAutoConfiguration
@ComponentScan
public class Application implements EmbeddedServletContainerCustomizer{
//implements EmbeddedServletContainerCustomizer
public static void main(String[] args) {
// http://localhost:8080/
SpringApplication.run(Application.class, args);
}
public void customize(ConfigurableEmbeddedServletContainer arg0) {
//arg0.setPort(8081);
}
@RequestMapping("/")
public String hello(){
return "Greetings from Spring Boot!";
}
@RequestMapping("/first.do")
String home() {
return "Hello World!";
}
}
保存后,运行
打开浏览器,访问