启动新Spring
项目
今天IDEA
更新到了2021.1
版,启动时候的图形更加花里胡哨了。正好也借此机会编写这个新项目,顺便好好的复习一下Spring Boot
的开发。
老样子,File - New -Project
然后选择 Spring Initializr
,配置如下:
Name=fms-java
Type=Maven
Language=Java
Group=cc.conyli
Artifact=fms-java
Package name=cc.conyli.fms
Java=11
Packaging=Jar
我的机器上装的是JDK 11
,所以选择Java 11
。Spring
是最新的2.4.4
版。
点击Next
进入选择组件的页面,选择如下组件:
Developer Tools - Spring Boot DevTools
Web - Spring Web
Template Engines - Thymeleaf
Security - Spring Security
SQL - Spring Data JPA(含有Hibernate)
SQL - PosetgreSQL Driver
I/O - Validation
I/O - Java Mail Sender
Ops - Spring Boot Actuator
其实有点遗憾的是没有用Redis
,这个留待以后功能更加强的话再使用吧。之后配置自动完成。IDEA
会显示HELP.md
的内容,还是得多读读官方文档才行。
配置数据库连接
spring.datasource.url=jdbc:postgresql://106.54.215.164:5432/fmsjava
spring.datasource.username=*******
spring.datasource.password=*******
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
主要是Hibernate
的几个配置,包括自动创建表,显示SQL
语句,方便调试。
顺便记录一下,我的VPS
上的PostgreSQL
是10.16
版本。
静态文件
把准备好的放有静态文件css
,js
,webfonts
,img
这四个文件夹复制到resources\static\
目录下。
启动项目
直接启动项目,日志如下:
2021-04-07 21:53:31.356 INFO 3792 --- [ restartedMain] cc.conyli.fms.FmsJavaApplication : Starting FmsJavaApplication using Java 11.0.9 on DESKTOP-4T01N2B with PID 3792 (C:\Coding\fms-java\target\classes started by Minko in C:\Coding\fms-java)
2021-04-07 21:53:31.374 INFO 3792 --- [ restartedMain] cc.conyli.fms.FmsJavaApplication : No active profile set, falling back to default profiles: default
2021-04-07 21:53:31.433 INFO 3792 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2021-04-07 21:53:31.433 INFO 3792 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2021-04-07 21:53:32.750 INFO 3792 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2021-04-07 21:53:32.770 INFO 3792 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2021-04-07 21:53:34.114 INFO 3792 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-04-07 21:53:34.130 INFO 3792 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-04-07 21:53:34.131 INFO 3792 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.44]
2021-04-07 21:53:34.363 INFO 3792 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-04-07 21:53:34.363 INFO 3792 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2927 ms
2021-04-07 21:53:34.756 INFO 3792 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-04-07 21:53:34.845 INFO 3792 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.29.Final
2021-04-07 21:53:34.989 INFO 3792 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-04-07 21:53:35.129 INFO 3792 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-04-07 21:53:35.297 INFO 3792 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-04-07 21:53:35.318 INFO 3792 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2021-04-07 21:53:35.645 INFO 3792 --- [ restartedMain] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-04-07 21:53:35.653 INFO 3792 --- [ restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-04-07 21:53:35.690 INFO 3792 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729
2021-04-07 21:53:35.739 WARN 3792 --- [ restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-04-07 21:53:35.858 INFO 3792 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-04-07 21:53:36.018 WARN 3792 --- [ restartedMain] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2021-04-07 21:53:36.253 INFO 3792 --- [ restartedMain] .s.s.UserDetailsServiceAutoConfiguration :
Using generated security password: 9e697d27-639d-408a-a877-8a13be46e702
2021-04-07 21:53:36.373 INFO 3792 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Will secure any request with [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@76ff7607, org.springframework.security.web.context.SecurityContextPersistenceFilter@6f756577, org.springframework.security.web.header.HeaderWriterFilter@7376ad84, org.springframework.security.web.csrf.CsrfFilter@3d5a979f, org.springframework.security.web.authentication.logout.LogoutFilter@b1cd8ff, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@61fbca05, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@1ad905dd, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@1d5c76b8, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@60a72e34, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@bbf412d, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@3fe70201, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@44d32da9, org.springframework.security.web.session.SessionManagementFilter@1019e9f0, org.springframework.security.web.access.ExceptionTranslationFilter@f784469, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@7a122bad]
2021-04-07 21:53:36.441 INFO 3792 --- [ restartedMain] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator'
2021-04-07 21:53:36.528 INFO 3792 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-04-07 21:53:36.559 INFO 3792 --- [ restartedMain] cc.conyli.fms.FmsJavaApplication : Started FmsJavaApplication in 5.65 seconds (JVM running for 7.983)
2021-04-07 21:53:36.992 INFO 3792 --- [192.168.100.190] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-07 21:53:36.993 INFO 3792 --- [192.168.100.190] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-04-07 21:53:36.998 INFO 3792 --- [192.168.100.190] o.s.web.servlet.DispatcherServlet : Completed initialization in 5 ms
从上到下能看到
java
启动信息- 没有配置
profile
,采用默认 Devtools
启动- 日志级别可设置为
DEBUG
以显示更多日志 Spring Data JPA
和Hibernate
,还有Hikari
连接池的启动信息Tomcat
启动WebApplicationContext
启动- 自动加载和重新启动项目的
LiveLoader
启动 - 启动线程池
Thymeleaf
的启动信息,此时没有配置模板所以提示找不到任何模板Spring Security
的启动信息,列出了其中全部的过滤器actuator
启动,暴露了两个端点Tomcat
启动完成- 启动
Spring DispatcherServlet
之后尝试访问localhost:8080
,会进入验证界面,输入用户名user
和日志中的密码,可以成功登录,由于目前还没有控制器,因此显示了错误页面。
至此项目启动OK了。马上推送到Github
上。
下一步,是编写用户认证登录,为此,先要系统的看一下Thymeleaf
的使用。