目标,springboot 项目,访问返回 Hello SpringBoot !
准备
- 浏览器
- jdk8
- idea(自备 ide 技能均可)
新建项目
进入 Spring 官网
点击 【Start a new Spring Project】
直达
推荐选择 grandle, 配置文件清爽不少
选择如下
简单介绍
各个组件
Web: 集成 SpringMVC
Security:
JPA:
Actuator:
Thymeleaf:
Mysql:
Lombok:
HSQLDB:
Idea 导入
解压后到目录下改 build.gradle 中 elipse 为 idea,
这是因为我用的 idea, eclipse 的话应该不用改,不过我没试过。
然后直接在目录下执行
╰─$ ./gradlew idea
完成后便可以直接用 idea 打开了
记得要 使用 import
点击 Finish 完成,等 ok。。。
启动
运行 DaxueApplication.java
2017-01-05 09:01:27.132 WARN 53547 --- [ main] o.s.boot.SpringApplication : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available) 2017-01-05 09:01:27.138 ERROR 53547 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPLICATION FAILED TO START *************************** Description: Cannot determine embedded database driver class for database type NONE Action: If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
启动失败,问题是缺少数据库配置
**解决:**在 application.properties 里加数据库配置,自己在 MySQL 里新建一个数据库。
application.properties 中内容如下:
spring.datasource.url=jdbc:mysql://localhost:3306/daxue spring.datasource.username=root spring.datasource.password=123qwe # update 模式会自动建表,有改动后也会更新,不过不会删原来的东西 spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect spring.jpa.properties.hibernate.connection.release_mode=auto
OK,启动完毕看结果
此时目录结构如下
╰─$ tree -a . ├── .gitignore ├── .gradle │ └── 2.13 │ └── taskArtifacts │ ├── cache.properties │ ├── cache.properties.lock │ ├── fileHashes.bin │ ├── fileSnapshots.bin │ └── taskArtifacts.bin ├── build.gradle ├── daxue.iml ├── daxue.ipr ├── daxue.iws ├── gradle │ └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src ├── main │ ├── java │ │ └── cn │ │ └── dxs2s │ │ ├── DaxueApplication.java │ │ ├── domain │ │ ├── service │ │ └── web │ │ └── IndexController.java │ └── resources │ ├── application.properties │ ├── static │ └── templates └── test └── java └── cn └── dxs2s └── DaxueApplicationTests.java
没有 MySQL ? 木有关系,这么办
更改一下依赖再刷新即可
hslqdb 是一个内存数据库,一般用来测试用,在使用 JPA 的方式下可以无缝切换到 MySQL。
没有 MySQL 的环境可以暂时顶用一下。
再次启动
访问 http://localhost:8080
SpringSecurity 的默认基本安全配置,密码请在启动的日志中自行寻找。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于