SpringBoot -> @Import使用
@Import 注解出自spring-context包中
package org.springframework.context.annotation; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}
该注解主要是用作导入一个配置类(Configuration),正常我们创建一个配置文件,都是在 @SpringBootApplication 的可扫描范围内,然后业务在使用到这个配置的时候,直接 @Autowired 注入就可以正常使用。

如上图所示,我们在使用order的时候直接注入是没有问题的,因为OrderConfig配置类,在启动主类的默认扫描范围内。
@SpringBootApplication 默认扫描范围是,扫描当前程序启动主类所在的包,及其当前包下的所有子包。如果需要扫描父类包中的配置,需要在启动类上使用 @ComponentScan("com.**") 注解来指明扫描的包路径。
如果配置类所在路径不在主类所在包,及其子包下,如下:

这种情况下,如果直接注入UserConfig的话,程序在启动的时候就会报错
Field user in com.example.security.securitydemo.SecurityDemoApplication required a bean of type 'com.example.security.securitydemo.bean.User' that could not be found. The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
解决办法
- 在启动类上加 @ComponentScan("com.**") 注解,直接搞定
- 使用 @Import(UserConfig.class) 注解引入
方式一简单粗暴,直接解决问题,但是,这种解决方式,仅仅只适合你知道该类的包名,以com开头的,假如包名是org呢,就尴尬了,在此基础之上,就延伸出了 @Import。
@Import 就相当于原始spring xml中使用 <import/> 标签一样,二者没有差异。
SpringBoot -> @Import使用的更多相关文章
- 5分钟创建一个SpringBoot + Themeleaf的HelloWord应用
第一步:用IDE创建一个普通maven工程,我用的eclipse. 第二步:修改pom.xml,加入支持SpringBoot和Themeleaf的依赖,文件内容如下: <?xml version ...
- SpringBoot Demo
Spring Boot,微框架,确实不错,很方便. 相关网站链接: http://www.tuicool.com/articles/veUjQba https://www.gitbook.com/bo ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
- springboot学习(一)——helloworld
以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给 ...
- SpringBoot初步
1.创建maven 项目 quickstart类型 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...
- SpringBoot Hello World
本文首发于我的github博客 前言 SpringBoot是Spring MVC升级版,基于『约定优于配置』的原则,快速开发出web程序. 环境 本系列笔记环境如下: Sun JDK1.8.0_20 ...
- SpringBoot学习之基础篇
在前面的博文中,已经演示过springboot与Mybatis集成的实例,本篇再来探讨一下SpringBoot的基础. 一.关于SpringBoot SpringBoot可以基于Spring轻松创建 ...
- SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件
参考:https://www.cnblogs.com/bjlhx/p/8325374.html 1.需要有一个默认的配置文件,然后一个正式的配置文件,一个测试的配置文件.激活配置项,默认的配置文件ap ...
- SpringBoot动态配置加载
1.SpringBoot对配置文件集中化进行管理,方便进行管理,也可以使用HttpClient进行对远程的配置文件进行获取. 创建一个类实现EnvironmentPostProcessor 接口,然后 ...
随机推荐
- SpringMVC 三种异常处理方式
SpringMVC 三种异常处理方式 在 SpringMVC, SpringBoot 处理 web 请求时, 若遇到错误或者异常,返回给用户一个良好的错误信息比 Whitelabel Error Pa ...
- Servlet,过滤器和监听器的配置和使用
一.什么是Servlet Servlet使用Java语言实现的程序,运行于支持Java语言的Web服务器或者应用服务器中.Servlet先于JSP出现,提供和客户端动态交互的功能.Servlet可以处 ...
- 爬虫工程师分享:三步就搞定 Android 逆向
本文源于我近期的一次公司内部分享,通过逆向某款 APP 来介绍逆向过程.由于仅作为学习用途,APP 的相关信息会被遮盖,敬请理解. 关于逆向 逆向--包括但不限于通过反编译.Hook 等手段,来解析一 ...
- 【Appium】Appium+Python环境搭建
环境准备: 1.jdk 2.android-sdk 3.python 4.Node.js 5.appium 6.Appium-Python-Client 1. 下载jdk1.7:http://www. ...
- vmware上安装的ubuntu连接不上网络
解决方法如下: 1.右键点击ubuntu,选择“设置” 2.网络适配器设置为“仅主机模式”:与主机共享的专用网络.点击“确定”即可.
- HDU - 2121 Ice_cream’s world II 无根最小树形图
HDU - 2121 :http://acm.hdu.edu.cn/showproblem.php?pid=2121 比较好的朱刘算法blog:https://blog.csdn.net/txl199 ...
- codeforces 816 E. Karen and Supermarket(树形dp)
题目链接:http://codeforces.com/contest/816/problem/E 题意:有n件商品,每件有价格ci,优惠券di,对于i>=2,使用di的条件为:xi的优惠券需要被 ...
- lightoj 1028 - Trailing Zeroes (I)(素数筛)
We know what a base of a number is and what the properties are. For example, we use decimal number s ...
- Unity 3D,地形属性
Terrain Width 地形高度 Terrain Height 地形宽度 Terrain Lenght 地形长度 HeughtMap Resolution 地形高度图的分辨率 Detail Re ...
- mybatis转义
SELECT * FROM test WHERE 1 = 1 AND start_date <= CURRENT_DATE AND end_date >= CURRENT_DATE 在执行 ...