结合Autowired和Service注解

public interface IUser {
void say();
} @Service
public class Student implements IUser { @Override
public void say() {
System.out.println("I'm a student");
}
} @Component
@Order(value = 3)
public class Entry implements CommandLineRunner {
public Log log = LogFactory.getLog(Entry.class); @Autowired
IUser user; @Override
public void run(String... args) throws Exception {
user.say();
}
}

如果要在构造函数中就需要访问注入的变量,那么Autowired的位置就要放到构造函数上

@Component
@Order(value = 3)
public class TestService {
private final IUser user; @Autowired
public void TestService (IUser user) {
user.say();
}
}

自定义注入的扫描范围

要注意Springboot扫描包的时候默认是从启动类(一般是Application)目录开始往下扫描,也就意味着如果Bean不在Application目录的下层,是不会被扫描到的。

这种情况会提示:

Description:
Field xxx in xxxxxx required a bean of type 'xxxxxx' that could not be found. Action:
Consider defining a bean of type 'xxxxxxxxxxxxxx' in your configuration.

不过这也不是无法改变的,我们手动指定扫描范围即可:

@SpringBootApplication
@ComponentScan(basePackages={"springbootdemo.basic","anotherspringbootdemo.basic"})
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

范围列表中不要忘记添加原来的目录,及启动类的包范围。

另外,这个ComponentScan不是必须放到启动类上,只要可以被扫描到即可。

通过Configuration的方式

通过Configuration也可以实现“跨域”的注入方式(即package不在一个范围内)

/**
* Springboot会扫描标有Configuration注解的类
* 该类中标有Bean注解的方法,返回值会被作为被注入项
* 至于这个Bean的注入项,在方法里面return就是。
*/
@Configuration
public class TestConfig{ @Bean
public IUser user(){
return new Teacher();
} //有依赖关系的Bean也很简单
//这个IDepartment依赖IUser
@Bean
public IDepartment(){
return new Development(user());
}
} /*调用*/
public class TestClass{ @Autowired
IUser user; public void run(String... args) throws Exception {
user.say();
}
}

上面的Configuration虽然解决了“跨域”注入,但Configuration注解还是要求放到调用的项目中。

很多时候当我们需要依赖一个第三方jar包,并想要实现自动注入的时候,我们并不想再去手动写Configuration,毕竟如果多个地方引用这个jar包,每一处都需要这样处理。

能不能一劳永逸呢?

使用Springboot中的框架时,例如使用ES,我们发现虽然并没有声明ElasticSearchTemplate,但是却可以直接使用

这里有一篇不错的讲解 https://www.jianshu.com/p/346cac67bfcc

假设第三方项目是ProjectA,应用方是ProjectB

现在ProjectA有 类TestTemplate

package ProjectA;

public class TestTemplate{

    public void test() {
System.out.println("GO TEST");
}
}

ProjectB需要注入并使用TestTemplate,当然肯定要先添加maven的依赖(忽略),调用逻辑

package ProjectB;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import springbootdemo.common.TestTemplate; @Component
public class Entry implements CommandLineRunner { @Autowired
private TestTemplate aa; @Override
public void run(String... args) throws Exception {
aa.test();
}
}

这时候运行ProjectB的话肯定是会报错的,因为找不到TestTemplate的注入结果,即使在TestTemplate上添加注解也是一样。

我们直接给出一个简单的解决方案

①在ProjectA中新建一个自动配置类 TestAutoConfiguration

package ProjectA;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; @Configuration
public class TestAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public TestTemplate testTemplate(){
return new TestTemplate();
}
}

②在ProjectA的资源目录src/main/resources下创建目录META-INF/spring.factories

内容:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
ProjectA.TestAutoConfiguration

现在再执行,一切OK!

Springboot依赖注入笔记的更多相关文章

  1. 跟我猜Spring-boot:依赖注入

    依赖注入 引&目标 本篇是<跟我猜Spring-Boot>系列的第二篇(Oh,我竟然已经写了10篇了,真不容易). 在上一篇中,我们实现了Bean的创建,但是仅仅是创建而已,并没有 ...

  2. Unity依赖注入(笔记)

    一.介绍 控制反转(Inversion of Control,简称IoC):整洁架构思想,不允许内部获知外部的存在,这就导致了我们必须在内层定义与外层交互的接口,通过依赖注入的方式将外层实现注入到内部 ...

  3. Springboot依赖注入 Service类中使用静态变量

    @Component public class ServerHandler extends IoHandlerAdapter { @Autowired protected HealthDataServ ...

  4. .NET CORE学习笔记系列(2)——依赖注入[7]: .NET Core DI框架[服务注册]

    原文https://www.cnblogs.com/artech/p/net-core-di-07.html 包含服务注册信息的IServiceCollection对象最终被用来创建作为DI容器的IS ...

  5. .NET CORE学习笔记系列(2)——依赖注入[6]: .NET Core DI框架[编程体验]

    原文https://www.cnblogs.com/artech/p/net-core-di-06.html 毫不夸张地说,整个ASP.NET Core框架是建立在一个依赖注入框架之上的,它在应用启动 ...

  6. .NET CORE学习笔记系列(2)——依赖注入[5]: 创建一个简易版的DI框架[下篇]

    为了让读者朋友们能够对.NET Core DI框架的实现原理具有一个深刻而认识,我们采用与之类似的设计构架了一个名为Cat的DI框架.在上篇中我们介绍了Cat的基本编程模式,接下来我们就来聊聊Cat的 ...

  7. .NET CORE学习笔记系列(2)——依赖注入[4]: 创建一个简易版的DI框架[上篇]

    原文https://www.cnblogs.com/artech/p/net-core-di-04.html 本系列文章旨在剖析.NET Core的依赖注入框架的实现原理,到目前为止我们通过三篇文章从 ...

  8. .NET CORE学习笔记系列(2)——依赖注入【3】依赖注入模式

    原文:https://www.cnblogs.com/artech/p/net-core-di-03.html IoC主要体现了这样一种设计思想:通过将一组通用流程的控制权从应用转移到框架中以实现对流 ...

  9. .NET CORE学习笔记系列(2)——依赖注入【2】基于IoC的设计模式

    原文:https://www.cnblogs.com/artech/p/net-core-di-02.html 正如我们在<控制反转>提到过的,很多人将IoC理解为一种“面向对象的设计模式 ...

随机推荐

  1. 入木三分学网络第一篇--VRRP协议详解第一篇(转)

    因为keepalived使用了VRRP协议,所有有必要熟悉一下. 虚拟路由冗余协议(Virtual Router Redundancy Protocol,简称VRRP)是解决局域网中配置静态网关时,静 ...

  2. Win32调试API原理

    在Win32中自带了一些API函数,它们提供了相当于一般调试器的大多数功能,这些函数统称为Win32调试API(Win32 Debug API).利用这些API可以做到加载一个程序或捆绑到一个正在运行 ...

  3. (0.2.2)如何下载mysql数据库(二进制、RPM、源码、YUM源)

    目录 1.基于Linux平台的Mysql项目场景介绍 2.mysql数据库运行环境准备-最优配置 3.如何下载mysql数据库 3.1. 二进制文件包 3.2.RPM文件 3.3.源码包 3.4.yu ...

  4. XTU1198:Candy(背包)

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/libin56842/article/details/26182519 题目描写叙述 Henry和Le ...

  5. 004-集成maven和Spring boot的profile功能打包

    参考地址:https://blog.csdn.net/lihe2008125/article/details/50443491 一.主要目标 1.通过mvn在命令行中打包时,可以指定相应的profil ...

  6. 1、初识JavaScript

    前端之 JavaScript 1.存在方式. <!-- 导入javascript脚本方法 --><script type="text/javascript" sr ...

  7. 取MAC地址 (含多网卡),最好的方法,支持Vista,Win7

    取MAC地址 (含多网卡),最好的方法,支持Vista,Win7 unit Unit1; interface usesWindows, Messages, SysUtils, Variants, Cl ...

  8. window7主题破解与恢复(复制)

    window7主题破解与恢复 1 2 3 分步阅读 windows7主题破解后可以换自己喜欢的主题,但也有一些弊端.这里帮助打家破解与恢复. 工具/原料 UniversalThemePatcher.e ...

  9. 两句话,实现android 4.4以上实现沉浸式状态栏

    效果图如下,就是状态栏和actionbar保持一致的颜色,非常漂亮 1:在Activity的OnCreate函数 if (Build.VERSION.SDK_INT >= Build.VERSI ...

  10. [2011-3-9 12:59 ]As3.0中的位图(Bitmap/BitmapData)用法

    1.位图使用(模糊)滤镜 //创建一个矩形区域的BitmapData var bmd:BitmapData = new BitmapData(80, 30, false, 0xefefef); //画 ...