Spring Boot 是由 Pivotal 團隊提供的全新框架。Spring Boot 是所有基於 Spring Framework 5.0 開發的項目的起點。Spring Boot 的設計是爲了讓你儘可能快的跑起來 Spring 應用程序並且儘可能減少你的配置文件。
準備工作
1.安裝maven
http://maven.apache.org/download.cgi
1.1下載對應的包名後(我使用的是3.5.3),解壓到當前的文件夾中。
1.2 配置環境變量
在~/.bash_profile
文件中增加環境變量
#JDK地址
#其他地址配置
#Maven庫地址
export M2_HOME='/dev/apache-maven-3.5.3'
export PATH="$PATH:$M2_HOME/bin"
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
1.3 配置完成以後輸入命令source ~./bash_profile
生效
1.4驗證是否正常安裝輸入命令mvn -v
Apache Maven 3.5.3 (3383c37e1f9e9b3bc3df5050c29c8aff9f295297; 2018-02-25T03:49:05+08:00)
Maven home: /dev/apache-maven-3.5.3
Java version: 1.8.0_162, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_162.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"
2.配置倉庫地址和鏡像
可以直接使用maven目錄下的settings.xml
文件,也可以使用一個新的本地倉庫地址。
<!--本地倉庫表示遠程拉取的庫都會存放在這個文件夾下-->
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/dev/nRepsository</localRepository>
<!--鏡像地址-->
<mirrors>
<mirror>
<id>nexus</id>
<mirrorOf>*</mirrorOf>
<name>internal nexus repository</name>
<url>www.baidu.com</url>
</mirror>
</mirrors>
3.在IDEA中配置倉庫File->Other Settings->Prefrence For New Projects
這樣後面新建的maven項目都會使用同一個倉庫
開始創建項目
1.1 創建 Spring Initializr
點擊next,輸入項目名稱:
勾選MyBatis ,MySQL,Web
最後選擇文件目錄以後成功創建一個demo工程。
在com.example.demo中新建一個類
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello this is my first SpringApplication";
}
}
然後點擊Run->Run’DemoApplication’
這樣就跑起來了,可以看到我們的應用跑在8080端口
遇到的問題
1.java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
原因:spring-boot的相關包的版本不一致導致的
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!--"其他依賴不復制,以下這些依賴包的版本保持一致"-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot</artifactId>
<version>1.4.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<version>1.4.2.RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.java.lang.ClassNotFoundException: org.springframework.context.event.GenericApplicationListener
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/GenericApplicationListener
原因:GenericApplicationListener是spring版本到4.2才加入的,所以將上面spring相關的包版本改到1.4.2以上。
3.java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
包沒有加載,遇到這種問題,需要clean後重新reimport
4.
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [META-INF/spring/community-context.xml]; nested exception is java.lang.NoSuchMethodError: org.springframework.beans.factory.support.DefaultListableBeanFactory.getDependencyComparator()Ljava/util/Comparator;
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at
包衝突引起,檢查包名
Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource
[org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate
[com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is
org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
原因:spring boot會默認加載org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration類
因爲新建的spring boot 工程中沒有dataSource相關的配置信息,所以一啓動就報錯
在Application類上加上如下:
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class}
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
6.Cannot determine embedded database driver class for database type NONE
同上