Day 1 SpringBoot 环境搭建
一、准备工作如下:
①使用工具 (版本: IntelliJ IDEA 2017.2.2 x64)
②Maven环境配置完成 (版本: apache-maven-3.5.3)
③JDK环境配置完成 (版本: jdk-1.8.2)
④JDK为本机所对应版本,setting.xml文件配置如下: (文件位置:如 apache-maven-3.5.3 --> conf)
<profiles>
<profile>
<id>jdk-1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
</profiles>
二、使用Maven工程
(一)、pom.xml 文件配置
<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-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <!-- 打包成jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>(二)、实例
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用 */ @SpringBootApplication public class HelloWorldApplication { public static void main(String[] args) { // Spring应用启动起来 SpringApplication.run(HelloWorldApplication.class, args); } }
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloWorldController { @RequestMapping("/hello") @ResponseBody public String hello () { return "HelloWorld"; } }
(三)、关于打成jar问题
Maven Projects ---> 本项目工程 ----> Lifecycle ----> package 进行打成jar操作
jar 位置: 本工程target目录下
重要注解
@SpringBootApplication 将所在包及所有子包所有的组件扫描到Spring容器
SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值,
将这些值作为自动配置类导入到容器中, 自动配置类就生效, 帮助我们进行自动配置工作
三、快速创建SpringBoot工程
(一)、创建SpringBoot (须: 联网)
(二)、目录结构
resources文件夹
static: 保存所有静态资源; js css imagestemplates: 保存所有的模板页面; (Spring Boot 默认jar包使用嵌入式的Tomcat, 默认不支持JSP页面);可以使用模板引擎(freemarker、thymeleaf);
application.properties: Spring Boot应用配置文件; 可以修改一些默认设置 例: tomcat 端口