您可以自由地使用任何标准的Spring框架技术来定义您的bean及其注入的依赖项。为简单起见,我们经常发现使用@ComponentScan(找到您的bean)和使用@Autowired(做构造函数注入)工作得很好。

如果您按照上面建议的方式构造您的代码(在根包中定位您的应用程序类),您可以添加@ComponentScan,而无需任何参数。您的所有应用程序组件(@Component、@Service、@Repository、@Controller等等)都将自动注册为Spring 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;
} // ... }
请注意,使用构造器注入需让riskAssessor字段被标记为final,表明它不能被随后更改。

Spring Beans和依赖注入的更多相关文章

  1. Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解

    一.关于Spring beans 和 依赖注入(Dependency Injection) spring boot 和 Spring 全家桶无缝衔接,开发过程中可以很轻松地使用 Spring 全家桶的 ...

  2. Spring Boot学习一之Spring Beans和依赖注入

    你可以自由地使用任何标准的Spring框架技术去定义beans和它们注入的依赖.简单起见,我们经常使用 @ComponentScan 注解搜索beans,并结合 @Autowired 构造器注入. 如 ...

  3. Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转

    Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...

  4. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  5. Spring学习(一)——Spring中的依赖注入简介【转】

      [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring. ...

  6. spring六种种依赖注入方式

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  7. Spring学习(一)——Spring中的依赖注入简介

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  8. spring几种依赖注入方式以及ref-local/bean,factory-bean,factory-method区别联系

    平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程 ...

  9. 谈谈自己了解的spring.NET的依赖注入

         spring.net里实现了控制反转IOC(Inversion of control),也即依赖注入DI(Dependency Injection),以达到解耦的目的,实现模块的组件化.程序 ...

随机推荐

  1. 第8章 IO库 自我综合练习

    要求: 文本内容: Tom  11144455 12345678998 Jone  88888888 99999999999 1.将文本文件中的内容读入,并显示到屏幕上: 2.手动输入“Mary 77 ...

  2. AlphaGo的前世今生(二)AlphaGo: Countdown to AI Revolution

    这是本专题的第二节,在这一节我们将以David Silver等人的Natrue论文Mastering the game of Go with deep neural networks and tree ...

  3. windows安装composer

    Composer 是 PHP 的一个依赖管理工具(不是一个包管理器).它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. 在windows下安装的方法 方法一:使用安装程序 这是将 Com ...

  4. xftp无法用root账号登录问题

    编辑vim /etc/ssh/sshd_config文件 把PermitRootLogin Prohibit-password 添加#注释掉 新添加:PermitRootLogin yes 更改Per ...

  5. Django框架的探索

    django框架的路由 django2 路由支持正则匹配,如: re_path(r'^category/(?P<category_id>\d+)/$',CourseCategoryView ...

  6. cv2.error: openCV报错

    运行openCV程序,出现了.cv2.error: OpenCV(4.1.0) D:\Build\OpenCV\opencv-4.1.0\modules\imgproc\src\color.cpp:1 ...

  7. linux环境下安装jmeter,启动执行脚本

    1.下载安装jmeter安装包 下载链接: https://pan.baidu.com/s/1KPhwNDsmTIAy41fEopHQEw 提取码: spwd 2.上传linux平台,解压jmeter ...

  8. 如何看iOS崩溃日志

    重点:Triggered by Thread这句话后边的线程号,快速定位问题出现在那个线程,是否是你的锅:Triggered by Thread所指的线程表示导致异常.崩溃的线程 下边内容转自简书 简 ...

  9. Spring--基础介绍一:IOC和DI

    前面学习了Struts2和Hibernate. Struts2主要是用来控制业务层面逻辑和显示,告诉你什么时候走哪个action,跑去运行哪个class的什么方法,后面调到哪个jsp. Struts2 ...

  10. 苹果手机input有圆角阴影的解决方法

    input[type=button], input[type=submit], input[type=file], button { cursor: pointer; -webkit-appearan ...