SpringBoot核心特性之组件自动装配
写在前面
spring boot能够根据依赖的jar包自动配置spring boot的应用,例如: 如果类路径中存在DispatcherServlet类,就会自动配置springMvc相关的Bean。spring boot的自动装配来源于spring的装配,功能也是随时spring的不断升级不断完善的,spring boot正是在spring的基础上实现的自动装配。
spring模式注解装配
模式注解介绍
模式注解是应用程序中用来标注组件的注解,例如:@Repository是spring框架中用来标注数据访问对象(DAO)的注解。@Component是用来标注被spring管理的通用的组件,@Component标注的类都可以被spring容器扫描到。并且任何标注@Component元注解的的注解也能被spring扫描到,比如@Service
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
@AliasFor(annotation = Component.class)
String value() default "";
}
下面是spring中常用的模式注解
| spring注解 | 使用场景 | 起始版本 |
|---|---|---|
@Repository |
数据仓储模式注解 | 2.0 |
@Component |
通用组件模式注解 | 2.5 |
@Service |
服务模式注解 | 2.5 |
@Controller |
Web 控制器模式注解 | 2.5 |
@Configuration |
配置类模式注解 | 3.0 |
装配方式
spring中通过配置扫描的包 ,就能扫描到注解的组件,有两种配置的方式:
XML配置
通过context:component-scan标签的base-package属性,配置需要扫描的包
<context:component-scan base-package="com.laoliangcode.service,com.laoliangcode.dao"/>
注解方式装配
@ComponentScan(basePackages = "com.laoliangcode.service,com.laoliangcode.dao")
自定义模式注解
可以通过在自定义注解上加上元注解的方式,自定义模式注解。例如:@UserRepository注解上加上元注解@Repository,这样@UserRepository也是模式注解了。这是由于注解具有派生性的特点,@UserRepository派生至@Repository,@Repository派生至@Component。
@Repository
public @interface UserRepository {
String value() default "";
}
spring @Enable模块装配
spring3.1开始支持@Enable模块装配,所谓模块是指,把具有相同功能的组件组合在一起,引入到项目中。比如@EnableWebMvc注解,就是把spring MVC相关的配置引入到项目中,而不需要其他配置,方便使用spring MVC。这种装配方式是通过@Import注解引入其他配置类来实现的,@EnableWebMvc通过引入DelegatingWebMvcConfiguration配置类,实现spring MVC的自动配置。
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}
引入的配置类有两种实现形式,一种是直接使用模式注解@Configuration的类,另一种是实现ImportSelector接口的selectImports方法,来引入配置类。
注解方式
@EnableWebMvc就是这种实现方式。
下面列举User模块的装配来具体说明实现的方式。可以看出EnableUserConfig是通过直接导入UserConfiguration来装配User模块的。
UserConfiguration配置类
@Configuration
public class UserConfiguration {
@Bean
public UserService userService(UserDao userDao){
return new UserService(userDao);
@Bean
public UserDao userDao() {
return new UserDao();
}
}
EnableUserConfig注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfiguration.class)
public @interface EnableUserConfig {
}
使用启动类
@EnableUserConfig
public class EnableUserConfigBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(EnableUserConfigBootstrap.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println("EnableUserConfigBootstrap.main" + userService.findBId(1));
context.close();
}
}
ImportSelector接口方式
spring中的EnableCaching就是这种实现方式。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(CachingConfigurationSelector.class)
public @interface EnableCaching {
}
下面列举User模块的装配来具体说明实现的方式。这种方式是通过UserConfigurationSelector来引入User的配置类UserConfiguration。
UserConfigurationSelector类用来导入UserConfiguration配置
public class UserConfigurationSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[] {UserConfiguration.class.getName()};
}
}
EnableUserSelector注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(UserConfigurationSelector.class)
public @interface EnableUserSelector {
}
使用启动类
@EnableUserSelector
public class EnableUserSelectorBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(EnableUserSelectorBootstrap.class);
UserService userService = context.getBean("userService", UserService.class);
System.out.println("EnableUserSelectorBootstrap.main" + userService.findBId(1));
context.close();
}
}
spring条件装配
spring3.1开始,spring引入了@Profile注解,可以根据环境Environment的不同引入不同的配置。spring4.0开始,Conditional注解可以更灵活的根据不同条件引入不同的配置。
@Profile注解方式的条件装配
使用User模块的不能dao装配来说明@Profile的条件装配。
UserProfileConfiguration配置
@Configuration
public class UserProfileConfiguration {
@Bean
public UserServiceForProfile userServiceForProfile(IUserDao userDao) {
return new UserServiceForProfile(userDao);
}
@Bean
@Profile("mysql")
public IUserDao userMysqlDao() {
return new UserMysqlDao();
}
@Bean
@Profile("oracle")
public IUserDao userOracleDao() {
return new UserOracleDao();
}
}
Environment激活使用
通过context.getEnvironment().setActiveProfiles("oracle");的方式,来激活不同的Profile
public class ProfileBootstrap {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext();
context.register(UserProfileConfiguration.class);
context.getEnvironment().setActiveProfiles("oracle");
context.refresh();
UserServiceForProfile userService = context.getBean("userServiceForProfile",
UserServiceForProfile.class);
System.out.println("ProfileBootstrap.main" + userService.findBId(1));
context.close();
}
}
@Conditional注解方式的条件装配
@Conditional注解方式,通过引入实现Condition接口的类,来判断条件是否成立,从而确定是否引入某个组件。这个类是通过实现matches方法来判断条件是否成立。spring4.0开始,由于引入了@Conditional注解,Profile也是通过@Conditional来实现的。
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ProfileCondition.class)
public @interface Profile {
String[] value();
}
spring boot中大量使用了@Conditional注解的方式,来自动装配不同的组件。@ConditionalOnClass用来表示类路径存在某些类时加载;@ConditionalOnMissingBean用来判断某些类的实例不存在时加载;ConditionalOnWebApplication用来判断某种应用类型时加载。例如webmvc的自动加载配置WebMvcAutoConfiguration:
@Configuration
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
@AutoConfigureAfter({ DispatcherServletAutoConfiguration.class,
TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
public class WebMvcAutoConfiguration {
}
下面的例子是根据系统变量的值来决定是否装配UserDao
OnSystemPropertyCondition 实现Condition接口的matches方法,用来判断是否符合条件
public class OnSystemPropertyCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionalOnSystemProperty.class.getName());
String propertyName = String.valueOf(annotationAttributes.get("name"));
String propertyValue = String.valueOf(annotationAttributes.get("value"));
String systemPropertyValue = System.getProperty(propertyName);
return propertyValue.equals(systemPropertyValue);
}
}
ConditionalOnSystemProperty注解
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnSystemPropertyCondition.class)
public @interface ConditionalOnSystemProperty {
String name();
String value();
}
启动类
public class ConditionalOnSystemPropertyBootstrap {
@ConditionalOnSystemProperty(name = "user.name", value = "Administrator")
@Bean
public UserDao userDao() {
return new UserDao();
}
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(ConditionalOnSystemPropertyBootstrap.class);
UserDao userDao = context.getBean("userDao", UserDao.class);
System.out.println("ConditionalOnSystemPropertyBootstrap.main" + userDao.findBId(1));
context.close();
}
}
如果value指定错误,就会报错:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userDao' available
spring boot自动装配
spring boot的自动装配结合了上面介绍的spring模式注解、@Enable模块装配和条件装配。另外,spring boot自身还使用了工厂加载机制,用SpringFactoriesLoader来装载配置类。
实现方法
- 实现自动装配的类
- 在
META-INF/spring.factories文件中配置第一步中的类 @EnableAutoConfiguration注解激活配置
下面以User模块的自动装配为例,来介绍具体的实现步骤
实现装配类UserAutoConfiguration
这里用到了前面介绍的@Enable模块装配和条件装配
@EnableUserSelector
@ConditionalOnSystemProperty(name = "user.name", value = "Administrator")
public class UserAutoConfiguration {
}
在META-INF/spring.factories文件中添加配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.laoliangcode.configuration.UserAutoConfiguration
SpringBoot核心特性之组件自动装配的更多相关文章
- springboot 2.0 自定义redis自动装配
首先创建maven项目 pom.xml: <?xml version="1.0" encoding="UTF-8"?> <project xm ...
- Spring Boot之从Spring Framework装配掌握SpringBoot自动装配
Spring Framework模式注解 模式注解是一种用于声明在应用中扮演“组件”角色的注解.如 Spring Framework 中的 @Repository 标注在任何类上 ,用于扮演仓储角色的 ...
- 一步步从Spring Framework装配掌握SpringBoot自动装配
目录 Spring Framework模式注解 Spring Framework@Enable模块装配 Spring Framework条件装配 SpringBoot 自动装配 本章总结 Spring ...
- 深入理解SpringBoot之自动装配
SpringBoot的自动装配是拆箱即用的基础,也是微服务化的前提.其实它并不那么神秘,我在这之前已经写过最基本的实现了,大家可以参考这篇文章.这次主要的议题是,来看看它是怎么样实现的,我们透过源代码 ...
- SpringBoot启动流程分析(五):SpringBoot自动装配原理实现
SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...
- Spring Boot 自动装配(二)
目录 目录 前言 1.起源 2.Spring Boot 自动装配实现 2.1.@EnableAutoConfiguration 实现 2.1.1. 获取默认包扫描路径 2.1.2.获取自动装配的组件 ...
- Spring(二)-生命周期 + 自动装配(xml) +自动装配(注解)
1.生命周期 **Spring容器的 bean **的生命周期: 1.1 默认生命周期 1.1.1 生命周期 调用构造方法,创建实例对象: set方法,给实例对象赋值: init 初始化方法 初始化对 ...
- 从源码中理解Spring Boot自动装配原理
个人博客:槿苏的知识铺 一.什么是自动装配 SpringBoot 定义了一套接口规范,这套规范规定:SpringBoot在启动时会扫描外部引用jar包中的META-INF/spring.factori ...
- 20、自动装配-@Autowired&@Qualifier&@Primary
20.自动装配-@Autowired&@Qualifier&@Primary 自动装配:Spring 利用依赖注入(DI),完成对IOC容器中各个依赖关系赋值 20.1 @Autowi ...
随机推荐
- 阶段3 3.SpringMVC·_05.文件上传_6 文件上传之跨服务器上传代码
参数HttpServletRequest也可以删掉 扩服务器的代码 注意选择jersey包下的 拿到文件资源.put过去. 最终代码 重新部署springMvc 图片服务器正常运行 客户端服务器 服 ...
- 性能测试的 Check List (不断更新中)
1. 开发人员是否提交了测试申请?2. 测试对象是否已经明确?3. 测试范围是否已经明确?4. 本次不被测试的范围是否已经明确?5. 测试目标是否已经明确?6. 何时开始性能测试?7. 何时终止一轮性 ...
- .net core中读取配置文件
1)先看丑陋的方法 读取 appsettings.json 然后在 Startup 的 ConfigureServices() 方法中进行注入: public IConfigurationRoot ...
- JavaScript基本入门03
目录 JavaScript 入门基础 03 JavaScript构造函数 常用事件和事件处理函数 小练习 数据类型之间的差异性 数组 介绍 创建 数组的常规使用 数组的length属性 数组当中常见的 ...
- Linux-把任务放到后台
公司用的服务器,只能ssh远程操作,每天都会自动退出账户,不知道怎么回事儿,很郁闷.所以每天早起重新登录后发现进程已经关闭了,因为你运行的任务是和terminal关联在一起的,terminal关闭后, ...
- 我理解的CLH
学而时习之,不亦说乎! --<论语> 原创,转载请附原文链接,谢谢. CLH 思路 保持时序的锁基本思路就是将等待获取锁的线程放入 ...
- ng-zorro等组件默认样式的修改
https://www.jianshu.com/p/8b887c2aac06 在项目中修改ng-zorro组件默认样式的一些方法: 类名等 前加::ng-deep: 类名等 前加:root: 类名等 ...
- 继续做一道linux的企业 面试题
把/dongdaxia目录及其子目录小所有以拓展名.sh结尾的文件中包含dongdaxia的字符串全部替换为dj. 解答:这道题还是用到了三剑客里的sed: 第一步:先在/dongdaxia目录及其子 ...
- Vue的快速入门
1 环境准备 1 下载安装Node 地址https://nodejs.org/en/download/ 完成后通过cmd打开控制台输入node -v 可以看到版本信息 2 通过该node自带的npm ...
- FFmpeg4.0笔记:封装ffmpeg的音频重采样功能类CSwr
Github https://github.com/gongluck/FFmpeg4.0-study/tree/master/Cff CSwr.h /************************* ...