Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解
SpringBoot2 系列往期回顾:
- SpringBoot2(001):入门介绍、官网参考和博客汇总
- SpringBoot2(002):手动创建第1个 SpringBoot2 简单应用——“HelloWorld” web 工程
- SpringBoot2(003):简要回顾“HelloWorld” web 工程
- SpringBoot2(004):关于 Build Systems (构建系统)
- SpringBoot2(005):关于工程代码结构的建议
- SpringBoot2(006):关于配置类(Configuration Classes)和自动配置(Auto-configuration)
本文就 Spring Beans、依赖注入 和 @SpringBootApplication 注解的使用等进行说明,分别参考官方文档: 17. Spring Beans and Dependency Injection 和 18. Using the @SpringBootApplication Annotation 。本文的目录结构如下:
1、关于Spring beans 和 依赖注入(Dependency Injection)
spring boot 和 SpringFramework 全家桶无缝衔接,开发过程中可以很轻松地使用 SpringFramework 全家桶的技术来定义 beans 及其需要注入的依赖(their injected dependencies)。最常用的比如@ComponentScan (用于扫描和查找 beans ) 和 @Autowired (用于构造器注入),效果都是杠杠的,省去了很多配置。
如果工程项目按照建议的代码结构来布局(主配置类在 root package 上),不用增加其他的配置参数,单独的 @ComponentScan 就会进行自动扫描,把 root package 下所有有注解声明的 beans (@Component,@Service, @Repository, @Controller 等)都当作 spring beans 自动注册进来。
当然,很多时候会引入他项目的接口包,而且包结构往往和本项目的包结构会有些差异,这个时候可能就需要配置扫描路径了。比如下面这个例子,主类 com.wpbxin.HelloWorldExample 默认会扫描 com.wpbxin(也就是 root package)下的所有有注解声明的 beans,所以我们就不用专门去配置需要扫描的路径了。但是我引用的其他项目的一个接口包,而这个包里面的路径是 com.wpb.mapper ,很明显不在约定扫描范围内,这个时候就需要使用 @ComponentScan("com.wpb.mapper") 进行导入了,这里建议直接新起一个配置类,比如在根包下的子包 com.wpbxin.config.MapperConfig 。这样就会进行间接的默认扫描了。
package com.wpbxin; import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*; @RestController
@EnableAutoConfiguration
public class HelloWorldExample {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldExample.class, args);
}
} /// 通过配置类的形式导入外部需要扫描的包
package com.wpbxin.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.wpb.mapper")
public class MapperConfig { }
下面这个例子,一个 @Service Bean 通过使用构造器注入获取所需的 RiskAssessor bean:
package com.example.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor; @Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
} // ... }
如果 bean 中含有一个构造器,可以忽略 @Autowired:
@Service
public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
} // ... }
小提示:final 表示赋值之后无法修改只想其他引用了。
2、关于 @SpringBootApplication 注解
很多开发者在 spring boot 应用中使用 auto-configuration、 component scan,并且可以在应用类(application class)中定义额外的配置。其实直接使用 @SpringBootApplication 注解就可以开启这3个特性了:
- @EnableAutoConfiguration:开启 spring boot 的自动配置机制,参考官网说明 16. Auto-configuration 或者笔者博客 SpringBoot2(006):关于配置类(Configuration Classes)和自动配置(Auto-configuration)
- @ComponentScan:默认开启 root package 下的 @Component 扫描(简单理解为 bean 扫描)
- @Configuration:允许注册其他 bean 到上下文 context,或者引入其他的配置类
也即是说 @SpringBootApplication 注解在功能上对等于同时使用 @Configuration、@EnableAutoConfiguration 和 @ComponentScan ,而且这3个注解使用默认配置。例子如下:
package com.example.myapplication; 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);
} }
注意点1:@SpringBootApplication 也提供了别名来指定 @EnableAutoConfiguration 和 @ComponentScan 的属性配置。
注意点2:这几个特性都不是强制性的,可以根据需要进行替换或者直接不开启。例如,不想使用 component scan 功能的话,可以这样写:
package com.example.myapplication; import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }
上面这个例子除了有 @Component 注解的类不会被自动扫描检测外,和其他 spring boot 应用没啥两样,并且用户自定义的 beans 需要被显示 import 进来(@Import)。
Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解的更多相关文章
- SSM框架之Spring(3)IOC及依赖注入(基于注解的实现)
		Spring(3)IOC及依赖注入(基于注解的实现) 学习基于注解的 IoC 配置,大家脑海里首先得有一个认知,即注解配置和 xml 配置要实现的功能都是一样 的,都是要降低程序间的耦合.只是配置的形 ... 
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)
		使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ... 
- 轻松了解Spring中的控制反转和依赖注入(二)
		紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类 ... 
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载
		使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ... 
- Spring 源码分析之 bean 依赖注入原理(注入属性)
		最近在研究Spring bean 生命周期相关知识点以及源码,所以打算写一篇 Spring bean生命周期相关的文章,但是整理过程中发现涉及的点太多而且又很复杂,很难在一篇文章中把Spri ... 
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
		Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的.然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择.Spring 3.0 的出现改变了这一状 ... 
- SSM框架之Spring(2)IOC及依赖注入
		Spring(2)IOC及依赖注入 基于xml配置文件的实现 1.IOC (控制反转-Inversion Of Control) 控制反转(Inversion of Control,缩写为IoC),是 ... 
- Spring学习笔记(8)——依赖注入
		spring依赖注入使用构造器注入使用属性setter方法注入使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发 ... 
- Spring 04: IOC控制反转 + DI依赖注入
		Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ... 
随机推荐
- redhat 7.6 apache  服务简单安装-01
			rpm -qa | grep httpd //该命令查看apache是否安装,下面图片是已安装,未安装不会显示任何内容 yum install httpd -y ... 
- IDEA工具java开发之  常用插件 git插件 追加提交 Code Review==代码评审插件  撤销提交 撤销提交 关联远程仓库 设置git 本地操作
			◆git 插件 请先安装git for windows ,git客户端工具 平时开发中,git的使用都是用可视化界面,git命令需要不时复习,以备不时之需 1.环境准备 (1)设置git (2)本地操 ... 
- A. You Are Given Two Binary Strings…
			A. You Are Given Two Binary Strings… You are given two binary strings x and y, which are binary repr ... 
- 【剑指Offer面试编程题】题目1504:把数组排成最小的数--九度OJ
			题目描述: 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. 输入: 输 ... 
- video-player
			1. VLC 2. MPlayer 3. FFmpeg 4. 显示媒体信息 5. 视频播放器软件比较 1. VLC https://www.videolan.org/vlc/ https://en.w ... 
- 注册模块上线前安全测试checklist
			许多应用系统都有注册模块,正常用户通过注册功能,获得应用系统使用权限:而非法用户通过注册模块,则是为了达到不可告人的目的,非法用户可以通过注册模块与服务端进行交互(一切用户输入都不可信),因此系统上线 ... 
- php 实现店铺装修7
			type_id=0的情况 type_id=1的情况 type_id=2的情况 /** * @title 店铺装修--商品分类 * @param type ... 
- 【高软作业4】:Tomcat 观察者模式解析 之 Lifecycle
			一. 预备 如果你是Windows用户,使用Eclipse,并且想自行导入源码进行分析,你可能需要:Eclipse 导入 Tomcat 源码 如果你已遗忘 观察者模式,那么你可以通过该文章回顾:设计模 ... 
- 【LOJ2540】「PKUWC2018」随机算法
			题意 题面 给一个 \(n\) 个点 \(m\) 条边的无向图.考虑如下求独立集的随机算法:随机一个排列并按顺序加点.如果当前点能加入独立集就加入,否则不加入.求该算法能求出最大独立集的概率. \(n ... 
- leetcode200 Number of Islands
			""" Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. ... 
