SpringBoot基础梳理
1.入口类和@SpringBootApplication注解:
SpringBoot通常有一个名为*Application的入口类,入口类里面有main方法,我们可以通过启动main方法启动springboot应用
@SpringBootApplication是SpringBoot的核心注解,他是一个组合注解,源码如下:
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = { TypeExcludeFilter.class }) })
public @interface SpringBootApplication {
Class<?>[] exclude() default {};
String[] excludeName() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
}
如果我们不使用@SpringBootApplication,我们可以直接在入口类上使用
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
其中@EnableAutoConfiguration 是让Springboot根据类路劲中的jar包依赖为当前项目进行自动配置
例如:我们添加了spring-boot-starter-web依赖后,会自动添加Tomcat和SpeingMVC的依赖,那么Spring Boot会对Tomcat和SpringMVC进行自动配置
2.使用xml配置Springboot
我们可以通过@ImportResource来加载xml配置
例如:
@ImportResource("classpath:some-context.xml")
3.基于安全的配置类型
Springboot可以通过@ConfigurationProperties将properties和一个Bean及其属性关联,从而实现安全的配置
例如:
@Component
@ConfigurationProperties(prefix="author")//通过@ConfigurationProperties加载指定的properties文件内容
//通过prefix属性指定properties的配置前缀,通过location指定properties文件的位置,例如:
//@ConfigurationProperties(prefix="author",locations="classpath:config/author.properties") 本例不需要配置location
public class AuthorSettings {
private String name;
private Long age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getAge() {
return age;
}
public void setAge(Long age) {
this.age = age;
}
}
配置文件如下
author.name=xmz
author.age=23
SpringBoot基础梳理的更多相关文章
- (二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用
一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf ...
- SpringBoot基础系列一
SpringBoot基础知识概览 特性 核心理念:约定优于配置 特点: 1. 开箱即用,根据项目依赖自动配置 2. 功能强大的服务体系,如嵌入式服务.安全 3. 绝无代码生成,不用写.xml配置,用注 ...
- SpringBoot基础系列-SpringCache使用
原创文章,转载请标注出处:<SpringBoot基础系列-SpringCache使用> 一.概述 SpringCache本身是一个缓存体系的抽象实现,并没有具体的缓存能力,要使用Sprin ...
- SpringBoot基础系列-使用日志
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996897.html SpringBoot基础系列-使用日志 概述 SpringBoot ...
- SpringBoot基础系列-使用Profiles
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9996884.html SpringBoot基础系列-使用Profile 概述 Profi ...
- SpringBoot基础系列-SpringBoot配置
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9990680.html SpringBoot基础系列-SpringBoot配置 概述 属性 ...
- java面试总躲不过的并发(一): 线程池ThreadPoolExecutor基础梳理
本文核心:线程池ThreadPoolExecutor基础梳理 一.实现多线程的方式 1.继承Thread类,重写其run方法 2.实现Runnable接口,实现run方法 3.实现Callable接口 ...
- SpringBoot 基础01
SpringBoot 基础 pom.xml <!-- Spring Boot 依赖版本控制 --> <parent> <groupId>org.springfram ...
- java基础梳理--朝花夕拾(三)
1.了解面向对象的编程思想以及面向对象的特性: 对象: EveryThing is Object: 万物皆是对象,在程序中,我们可以将类.接口.方法.属性等都认为是对象: 面向对象: 是一种程序设计方 ...
随机推荐
- Python的多线程编程
提到多线程,很多人就会望而却步,本文将由浅入深地带你攻克python多线程编程,并防止你跳入深坑, 首先看一段简单的代码: from time import ctime,sleep def play_ ...
- Android 串口设置校验位、速率、停止位等参数
Android 串口通讯设置校验位.速率.停止位等参数 最近业余时间有个android项目需要和硬件进行通信,通讯方式都是通过串口,android串口通讯google有个开源的demo 和很多人一样我 ...
- Kafka 源代码分析之LogSegment
这里分析kafka LogSegment源代码 通过一步步分析LogManager,Log源代码之后就会发现,最终的log操作都在LogSegment上实现.LogSegment负责分片的读写恢复刷新 ...
- [leetcode-556-Next Greater Element III]
Given a positive 32-bit integer n, you need to find the smallest 32-bit integer which has exactly th ...
- spring的Convert机制
spring.core包内有Converter接口,方法是T convert(S source);从一个类型转为内容一个类型的实际转化器:converters可能非常多,需要一个注册器来集中管理使用, ...
- 配置tomcat的远程调试
参考文档: http://jingyan.baidu.com/article/0320e2c1f4ef6b1b87507b06.html http://flyer2010.iteye.com/blog ...
- 什么是Hadoop
配上官方介绍 What Is Apache Hadoop? The Apache™ Hadoop® project develops open-source software for relia ...
- Struts框架之 执行流程 struts.xml 配置详细
1.执行流程 服务器启动: 1. 加载项目web.xml 2. 创建Struts核心过滤器对象, 执行filter → init() struts-default.xml, 核心功能的初 ...
- Akka(13): 分布式运算:Cluster-Sharding-运算的集群分片
通过上篇关于Cluster-Singleton的介绍,我们了解了Akka为分布式程序提供的编程支持:基于消息驱动的运算模式特别适合分布式程序编程,我们不需要特别的努力,只需要按照普通的Actor编程方 ...
- Java Swing intro
Java Swing intro 如果有Android app开发经验,快速上手Swing不是问题.UI方面有相似的地方. 简单的几行代码就能抛出一个框框,记录一下操作过程 1.先显示一个框框 Era ...