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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.example</groupId><artifactId>myproject</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.2.7.RELEASE</version></parent><!-- Additional lines to be added here... --></project>
spring-boot 提供了很多的启动器 POM,这使得你很容易添加jar到classpath,spring-boot-starter-parent是一个特殊的启动器提供有用的maven默认设置,它也同时提供了一个dependency-management
节点,所以你可以省略 dependencies的version
节点。
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.stereotype.*; import org.springframework.web.bind.annotation.*;@RestController @EnableAutoConfiguration public class Example {@RequestMapping("/")String home() {return "Hello World!";}public static void main(String[] args) throws Exception {SpringApplication.run(Example.class, args);}}
@RestController对于spring来讲,这个类扮演了一个很特殊的角色。在这个案例中,我们的类是一个web@Controller
,所以当处理web requests时,Spring会考虑它。
@RequestMapping注解提供了“routing”信息,它告诉spring任何的HTTP请求的路径是“/”应该映射到home
方法。@RestController注解告诉Spring直接将结果返回给调用者。
@EnableAutoConfiguration注解经常使用在你的main类中,并且它默认的定义在“Search package”。例如,如果你写一个JPA应用,@EnableAutoConfiguration注解的类将会被用于搜索@Entity条目。
使用一个根包结构同时也允许@ComponentScan注解使用不需要指定特定的包属性,你也可以使用@SpringBootApplication在你的根包结构中的main类中,下面是典型的结构:
com+- example+- myproject+- Application.java|+- domain| +- Customer.java| +- CustomerRepository.java|+- service| +- CustomerService.java|+- web+- CustomerController.java
通常我们建议你的主要资源是一个 @Configuration类,通常这个类定义了 main
方法,这个方法也是一个作为首要@Configuration的候选者。
但是你不需要把所有的@Configuration放到一个单一的类中。@Import
注解可以用于导入其他的配置类,其次,你还可以使用@ComponentScan自动导入所有的Spring组件,包括@Configuration类。
如果你必须使用基于XML的配置文件,我们建议你仍然使用@Configuration类,然后你可以使用额外的@ImportResource注解导入XML配置文件。
Spring Boot 自动配置尝试自动配置你的Spring应用基于你已经添加的jar。比如,如果HSQLDB在你的classpath中,你不需要手动的配置数据库,然后spring将会自动配置内存的数据库。
你需要通过添加@EnableAutoConfiguration
或者@SpringBootApplication注解在一个你的@Configuration类中,然后就可以自动配置。
许多Spring Boot开发者总是有他们自己的main类使用@Configuration
,@EnableAutoConfiguration
and@ComponentScan
因为这些注解经常被一起使用,Spring Boot 提供了一个方便的@SpringBootApplication。
@SpringBootApplication注解就相当于使用@Configuration
,@EnableAutoConfiguration
and@ComponentScan
package com.example.myproject;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
SpringApplication 类提供了一个方便的方法启动一个Spring 应用那就是将会通过一个main方法启动。在很多情况下你可以使用
静态的SpringApplication.run 方法。
public static void main(String[] args) {SpringApplication.run(MySpringConfiguration.class, args); }
当你的应用启动时,你应该可以看到类似于下面的内容:
. ____ _ __ _ _/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \\\/ ___)| |_)| | | | | || (_| | ) ) ) )' |____| .__|_| |_|_| |_\__, | / / / /=========|_|==============|___/=/_/_/_/:: Spring Boot :: v1.2.7.RELEASE2013-07-31 00:08:16.117 INFO 56603 --- [ main] o.s.b.s.app.SampleApplication : Starting SampleApplication v0.1.0 on mycomputer with PID 56603 (/apps/myapp.jar started by pwebb) 2013-07-31 00:08:16.166 INFO 56603 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; root of context hierarchy 2014-03-04 13:09:54.912 INFO 41370 --- [ main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080 2014-03-04 13:09:56.501 INFO 41370 --- [ main] o.s.b.s.app.SampleApplication : Started SampleApplication in 2.992 seconds (JVM running for 3.658)
banner 是在启动中被打印的,可以通过添加一个banner.txt文件在你的classpath中来改变,或者通过设置banner.location 。如果这个文件有一个不同的字符集你可以设置banner.charset
你可以使用以下变量在你的banner.txt文件中:
VariableDescription
${application.version}
The version number of your application as declared in MANIFEST.MF
. For example 1.0
.
${application.formatted-version}
The version number of your application as declared in MANIFEST.MF
formatted for display (surrounded with brackets and prefixed with v
). For example (v1.0)
.
${spring-boot.version}
The Spring Boot version that you are using. For example 1.2.7.RELEASE
.
${spring-boot.formatted-version}
The Spring Boot version that you are using formatted for display (surrounded with brackets and prefixed with v
). For example (v1.2.7.RELEASE)
.
如果你想通过程序生成可以banner可以通过SpringBootApplication.setBanner(…) 。使用org.springframework.boot.Banner接口然后实现你自己的printBanner()方法。
如果SpringApplication
默认的不能满足你,你可以创建自己的实例并定制它。比如:关闭banner你可以写:
public static void main(String[] args) {SpringApplication app = new SpringApplication(MySpringConfiguration.class);app.setShowBanner(false);app.run(args); }
另外Spring Framework事件,比如 ContextRefreshedEvent
一个SpringApplication
发送一些额外的应用事件。一些事件真正的在ApplicationContext
创建前触发。
应用程序的事件一以下的顺序被发送,当你的应用程序运行时:
- An
ApplicationStartedEvent
is sent at the start of a run, but before any processing except the registration of listeners and initializers. - An
ApplicationEnvironmentPreparedEvent
is sent when theEnvironment
to be used in the context is known, but before the context is created. - An
ApplicationPreparedEvent
is sent just before the refresh is started, but after bean definitions have been loaded. - An
ApplicationFailedEvent
is sent if there is an exception on startup.
spring-boot学习相关推荐
- Spring Boot学习笔记-实践建言
2019独角兽企业重金招聘Python工程师标准>>> 本文延续<Spring Boot学习笔记-快速示例>,从开发指南中摘出一些实践经验可供参考.这也是笔者看到的眼前一 ...
- 八个开源的 Spring Boot 学习资源,你值得拥有
点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:什么?你还在使用fastjson,性能太差了个人原创+1博客:点击前往,查看更多 转载自:牧马小子 Spring ...
- Spring Boot学习笔记-进阶(3)
文章目录 Spring Boot学习笔记-进阶(3) 一.Spring Boot与缓存 二.Spring Boot与消息 三.Spring Boot与检索 四.Spring Boot与任务 异步任务 ...
- Spring Boot学习笔记-基础(2)
Spring Boot学习笔记-基础(2) Spring Boot 优点: – 快速创建独立运行的Spring项目以及与主流框架集成 – 使用嵌入式的Servlet容器,应用无需打成WAR包 – st ...
- Spring Boot学习笔记(1)
文章目录 Spring Boot学习笔记(1) Spring Boot 整合 JSP Spring Boot HTML Thymeleaf 常用语法 Spring Boot 数据校验 Spring B ...
- Vue + Spring Boot 学习笔记02:引入数据库实现用户登录功能
Vue + Spring Boot 学习笔记02:引入数据库实现用户登录功能 在学习笔记01里,我们利用跨域打通了前端的Vue与后端的Spring Boot,实现了用户登录功能,但是后台的登录控制器在 ...
- Vue + Spring Boot 学习笔记01:实现用户登录功能
Vue + Spring Boot 学习笔记01:实现用户登录功能 一.创建后端Spring Boot项目Book Management 二.创建前端Vue项目bm-vue 三.修改后端项目Book ...
- Spring Boot学习总结(16)——为什么说Java程序员到了必须掌握Spring boot的时候了?
分享一个大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到人工智能的队伍中来!点击浏览教程 Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热, ...
- spring boot 学习之五(日志配置)
想直接看这里的我建议先看了我的spring boot 学习之四.因为日志的配置要用到properties. 先了解一下springBoot的日志系统然后再进行配置. springboot默认采用的是s ...
- Spring Boot学习——统一异常处理
Spring Boot学习--统一异常处理 参考文章: (1)Spring Boot学习--统一异常处理 (2)https://www.cnblogs.com/aston/p/7258834.html ...
最新文章
- Cassandra 1.2 发布,NoSQL 数据库
- XML 标签 首字母转换为大写
- JQuery跨站脚本漏洞
- JAVAEE框架之Spring AOP
- 金算盘高手论坛资料中心_3D006期 菜鸟论坛精英PK专栏 速来围观!!
- “智企云中享“,首届SAP中国云大会召开
- 微信录音滑动撤销 html5,微信中这个被取消的功能悄悄上线了,将语音上滑即可转换成文字...
- js 拉勾网效果_借助JShaman,建立自己的JS代码混淆平台
- java datastream
- Confluence 6 恢复一个空间
- 【转】C#字符串转换为日期
- 极客大学架构师训练营 框架开发 设计原则 设计模式 反应式编程框架 上课总结 第五课
- shell脚本文件使用教程
- array函数python_python中如何使用numpy.array函数创建数组?
- [爬虫系列(三)]用多线程爬取百度贴吧默认表情
- 「Computer Vision」Note on Seamless Nudity Censorship(裸体审查)
- 抗击疫情,程序员在家免费学这些!
- win10添加网络打印机_win10系统连接网络打印机
- php红包退回通知,PHP红包算法类(已运用实际项目)
- 论如何写好一篇需求报告(或者说产品报告)