Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。
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"
可以直接使用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>
这样后面新建的maven项目都会使用同一个仓库
点击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
同上