您可以自由地使用任何标准的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. splash介绍及安装_mac

    一.splash介绍 Splash是一个Javascript渲染服务.它是一个实现了HTTP API的轻量级浏览器,基于Python3和Twisted引擎,可以异步处理任务,并发性能好. 二.spla ...

  2. 【398】COMP9021 - Polynomial

    构建 Polynomial 类,实现 +, -, , / and +=, -=, =, /= 参考:如何用python编程求解二元一次方程组.如x+y=3;x-y=1 参考:python对重载运算符的 ...

  3. js导出excel文件

    <div id="tablesDiv"> <table id="tabDiv1"> <tbody><tr> &l ...

  4. whereis命令详解

    1.简介: whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis ...

  5. inpuy只能输入数字,并且禁止输入e

    <el-input type="number" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" ...

  6. jenkins登录后页面显示为空的问题

    1.然后再打开一个新的窗口,输入网址http://localhost:8080/jenkins/pluginManager/advanced,输入网址打开后滑动到页面下方,最底下有个[升级站点],把其 ...

  7. Google开源软负载seesaw

    https://github.com/google/seesaw ------------------------ 在分布式系统中,负载均衡是非常重要的环节,通过负载均衡将请求派发到网络中的一个或多个 ...

  8. 29. Divide Two Integers (JAVA)

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  9. 圆形图片 ImageView

    package com.example.m_evolution; import android.content.Context; import android.graphics.Bitmap; imp ...

  10. 企业BGP网络规划案例(一)

    网络拓扑: 如上图为一家企业的办公网,分为总部AS6500,分公司AS65001和分公司AS65002,其中每个站点都有生产.办公和服务器区域的网络互访,分公司和总公司之间通过两条联通/电信的MSTP ...