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)对象的创建 ...
随机推荐
- IDEA JSP中报错cannot resolve method println的解决方案
原因是没有导入Tomcat 库, 在 Project structure 添加
- js 实现去重
ES6 set去重 Array.from(new Set([1,2,3,3,4,4])) // [1,2,3,4] [...new Set([1,2,3,3,4,4])] // [1,2,3,4] 使 ...
- windows10安装.netframework3.5
先挂载,看看挂载到哪个盘了,假设是I盘 然后按住shift 点鼠标右键,打开powershell,运行下面命令: dism.exe /online /enable-feature /featurena ...
- redis列表-list
Redis的list类型其实就是一个每个子元素都是string类型的双向链表,链表的最大长度是2^32.list既可以用做栈,也可以用做队列. 常用命令: 1. lpush key value [va ...
- 对于文章的字母、单词、短语,(无用词表)的检索Java代码实现
日期:2019.5.9 博客期:073 星期四 今天软件工程课上,又做了测试,老师说我们的速度太慢了,实际上我也觉得自己很慢.老师说了这是我们的上一届的大二上半学期学习中的速度,所以呢?意思就是说我们 ...
- 树莓派学习笔记——Restful服务 采用slim php apache
0.前言 前些时间沉迷于Restful,采用PHP+Slim+MySQL实现了一些简单的API函数.但是这些工作都是在windows中实现(采用wamp server集成安装包),但是转到li ...
- PCS 7 V9.0 SP1安装过程截图
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 按钮:禁用状态
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- firewalld学习-zone
原文地址:http://www.excelib.com/article/290/show firewalld默认提供了九个zone配置文件: block.xml.dmz.xml.drop.xml.ex ...
- 第2节 网站点击流项目(下):6、访客visit分析
0: jdbc:hive2://node03:10000> select * from ods_click_stream_visit limit 2;+--------------------- ...