博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot依赖注入笔记
阅读量:4537 次
发布时间:2019-06-08

本文共 3410 字,大约阅读时间需要 11 分钟。

结合Autowired和Service注解

public interface IUser {    void say();}@Servicepublic 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就是。*/@Configurationpublic 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,但是却可以直接使用

这里有一篇不错的讲解 

假设第三方项目是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;@Componentpublic 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;@Configurationpublic 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!

 

转载于:https://www.cnblogs.com/TiestoRay/p/8891469.html

你可能感兴趣的文章
chrome 调试
查看>>
luoguP2774 方格取数问题
查看>>
tcp/ip协议各层的理解与
查看>>
python中的setdefault()方法
查看>>
转 VSFTP用户权限管控
查看>>
poj2420 A Star not a Tree? 模拟退火
查看>>
微信小程序--登录授权,一键获取用户微信手机号并登录
查看>>
[转载] C#面向对象设计模式纵横谈——13. Proxy代理模式
查看>>
JqueryEasyUI浅谈---视频教程公布
查看>>
ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致”...
查看>>
Javaweb之 servlet 开发详解1
查看>>
Restore IP Addresses
查看>>
DWR框架简单应用
查看>>
KMP 学习心得-----转
查看>>
time.strftime:格式化字符串中含中文报错处理
查看>>
模态窗口缓存无法清除怎么办? 在地址上加个随机数吧"&rd=" + new Date().getTime()
查看>>
阿里的weex框架到底是什么
查看>>
Tesis enDYNA
查看>>
FxZ,C#开发职位面试测试题(30分钟内必须完成)
查看>>
[HNOI2007]分裂游戏
查看>>