环境搭建-Eclipse安装Spring-Tool-Sutite插件并创建简单的SpringBoot项目

作者: admin 分类: SpringBoot 发布时间: 2019-02-26 21:59  阅读: 472 views
一直在使用SpringMVC的结构在做项目,最近正好有时间看下SpringBoot对项目进行改造。

 

默认eclipse下是无法创建spring-boot项目的,
一、安装Eclipse插件
1. 先去应用市场查找安装下, 搜索 Spring Tool Sutite 插件;
2. 然后新建项目 new -> spring -> spring starter project项目;
2-1.当然可以使用官方的web界面,创建SpringBoot项目,可以选择版本+模块,很方便 https://start.spring.io/ 
二、处理项目异常信息
新建或导入项目之后,会发现pom.xml文件有✘ 
出现在文件的以下位置
<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.1.3.RELEASE</version>
	<relativePath></relativePath> <!-- lookup parent from repository -->
</parent>

鼠标移上去会看到

Project build error: Non-resolvable parent POM for com.example:demo:0.0.1-SNAPSHOT: Failure to find org.springframework.boot:spring-boot-starter-parent:pom:2.1.3.RELEASE in http://xxx.xx.xxx.xx:8090/nexus/content/groups/public/ was cached in the local repository, resolution will not be reattempted until the update interval of nexus has elapsed or updates are forced and 'parent.relativePath' points at no local POM

是项目依赖文件没有下载好,

如果是连接自己的仓库,请确定仓库中是否有spring-boot-starter-parent:pom:2.1.3.RELEASE包
如果没有,请换个仓库地址下载。或者将相关文件,添加至自己的仓库中。
我这里用的阿里云镜像,如下: 如要使用,请修改settings.xml文件。
<mirrors>
<mirror>
            <id>alimaven</id>
            <name>aliyun maven</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
</mirrors>

三、整个项目无错后启动项目(运行DemoApplication主程序文件)

报错如下
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-02-25 15:48:48.343  INFO 12667 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on chenhailongdeMacBook-Pro.local with PID 12667 (/Users/chenhailong/eclipse-workspace/demo/target/classes started by chenhailong in /Users/chenhailong/eclipse-workspace/demo)
2019-02-25 15:48:48.346  INFO 12667 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-02-25 15:48:49.298  WARN 12667 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example.demo]' package. Please check your configuration.
2019-02-25 15:48:49.333  INFO 12667 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-02-25 15:48:49.335  INFO 12667 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-02-25 15:48:49.356  INFO 12667 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8ms. Found 0 repository interfaces.
2019-02-25 15:48:49.737  INFO 12667 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$cae11407] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-02-25 15:48:50.111  INFO 12667 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-02-25 15:48:50.145  INFO 12667 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-02-25 15:48:50.146  INFO 12667 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-02-25 15:48:50.160  INFO 12667 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/chenhailong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-02-25 15:48:50.258  INFO 12667 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-25 15:48:50.259  INFO 12667 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1855 ms
2019-02-25 15:48:50.777  INFO 12667 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-25 15:48:50.925  WARN 12667 --- [           main] ConfigServletWebServerApplicationContext : 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
2019-02-25 15:48:50.926  INFO 12667 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2019-02-25 15:48:50.929  INFO 12667 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-02-25 15:48:50.946  INFO 12667 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-25 15:48:50.949 ERROR 12667 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

Action:

Consider the following:
	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

错误原因是:

引入了mybatis-spring-boot-starter这个jar包 ,Spring boot默认会加载org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration类,DataSourceAutoConfiguration类使用了@Configuration注解向spring注入了dataSource bean。

 

因为工程中没有关于dataSource相关的配置信息,当spring创建dataSource bean因缺少相关的信息就会报错。
有两种处理方式(由于是刚熟悉SpringBoot最简化启动看到效果为目的)

 

1. 增加注解, 禁止注入数据源相关配置信息
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})       //禁止注入
public class DemoApplication {
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

2. 项目中增加数据源相关配置信息(可以写*.properties文件或者*.yml文件【yaml文件不如yml简洁】)

application.yml中格式如下
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456

application.properties中格式如下
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456

这两种配置都可以启动成功

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-02-25 16:01:00.872  INFO 12686 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on chenhailongdeMacBook-Pro.local with PID 12686 (/Users/chenhailong/eclipse-workspace/demo/target/classes started by chenhailong in /Users/chenhailong/eclipse-workspace/demo)
2019-02-25 16:01:00.876  INFO 12686 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2019-02-25 16:01:01.894  INFO 12686 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2019-02-25 16:01:01.896  INFO 12686 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
2019-02-25 16:01:01.929  INFO 12686 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 18ms. Found 0 repository interfaces.
2019-02-25 16:01:02.646  INFO 12686 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-02-25 16:01:02.686  INFO 12686 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-02-25 16:01:02.686  INFO 12686 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-02-25 16:01:02.701  INFO 12686 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/chenhailong/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-02-25 16:01:02.825  INFO 12686 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-02-25 16:01:02.825  INFO 12686 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1875 ms
2019-02-25 16:01:03.286  INFO 12686 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-02-25 16:01:03.788  INFO 12686 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-02-25 16:01:03.791  INFO 12686 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 3.33 seconds (JVM running for 3.792)

四、通过浏览器访问新建的jsp页面

访问:localhost:8080/出现

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Feb 25 16:28:46 CST 2019
There was an unexpected error (type=Not Found, status=404).
No message available

 

 

这是地址没有访问正确,我在src/main/webapp目录下创建index.jsp后,再次访问localhost:8080/demo/index.jsp
浏览器会直接下载 index.jsp 。 这是由于官方不推荐使用jsp、会逐渐淘汰掉(可以用freemakerthymeleaf等)
如果需要使用JSP, 需要增加以下依赖。
<!-- JSP页面展示所需 -->
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
	<scope>provided</scope>
</dependency>

重新启动之后,再次访问 localhost:8080/demo/index.jsp 

可以看到访问成功

SpringBoot启动


https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/

尽量看原官方文档,讲解更权威、靠谱

由于公司最近做的项目是SpringMVC + Velocity结构,所以还要集成velocity,请看

SpringBoot2.1.3集成velocity2.0的环境配置及过程记录


   原创文章,转载请标明本文链接: 环境搭建-Eclipse安装Spring-Tool-Sutite插件并创建简单的SpringBoot项目

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注

更多阅读