使用IDEA创建SpringBoot1.3.5运行于jdk1.6
程序员文章站
2024-03-22 23:57:22
...
问题描述:官方文档,默认Spring boot 1.3.5 要求 Java7版本,而当前项目要求 Java6,而且在idea工具中创建项目时,如果采用选项“Spring Initializr”来创建工程的话,发现最低只能创建java7的版本,本地环境又只配置了jdk6,所以idea检测不正确版本,无法创建工程,经过一番小尝试,使idea能够运行Spring boot 1.3.5 与jdk6版本。
1、环境参数
(1)IDE:IntelIJ IDEA 2017.3.3
(2)JDK:1.6.0_45
(3)Maven:3.0.5
(4)Spring Boot:1.3.5
2、打开idea,创建maven工程
3、填写包名,选择已安装的maven路径
这里我改为本地的maven路径
4、创建成功后,目录结构如下
5、打开工程下的pom.xml,参考以下配置,替换代码
<?xml version="1.0" encoding="UTF-8"?>
<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>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>springboota</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<dependencies>
<!-- Spring Boot -->
<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>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</pluginManagement>
<defaultGoal>compile</defaultGoal>
</build>
</project>
其中,注意要加上java版本与tomcat标签
<java.version>1.6</java.version> <tomcat.version>7.0.79</tomcat.version>
6、打开file >Project Structure,配置jdk6
7、打开file >Settings,搜索 java compiler,选择项目编译为1.6版本
8、完成以上配置后,新建以下目录、java文件与资源文件,这里使用yml,简洁,易于描述
9、以下分别编写main与controler类,添加注解,完成最简单的spring boot的启动
package com.demo.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* spring boot 运行入口
*/
@SpringBootApplication
public class App {
public static void main( String[] args ) {
SpringApplication.run(App.class,args);
}
}
package com.demo.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello,this is a spring-boot demo";
}
}
10、在main中右键“run”,启动spring boot,效果如下