【Spring 基础】通过注解注入Bean
原课程:通过注解注入Bean
注入bean知识点思维导图

Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection
spring为什么推荐使用构造器注入
通过构造器和Set方法注入Bean

注意:通过构造器注入Bean时,如果只有一个构造器,可以不写@Autowired注解,详见17. Spring Beans and Dependency Injection
The following example shows a @Service Bean that uses constructor injection to obtain a required RiskAssessor 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;
}
// ...
}
If a bean has one constructor, you can omit the @Autowired, as shown in the following example:
@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}
通过属性直接注入Bean

实例化和注入时指定Bean的ID

List/Set注入
直接注入List实例

将多个泛型实例注入到List

Map注入
直接注入Map实例

将多个泛型实例注入到Map

Integer等包装类型直接赋值

Spring IoC容器内置接口实例注入

【Spring 基础】通过注解注入Bean的更多相关文章
- java 静态代码块和spring @value等注解注入顺序
java 静态代码块和spring @value等注解注入顺序 问题所在 先上代码 java方法 @Value("${mf.cashost}") public static S ...
- Spring中注解注入bean和配置文件注入bean
注解的方式确实比手动写xml文件注入要方便快捷很多,省去了很多不必要的时间去写xml文件 按以往要注入bean的时候,需要去配置一个xml,当然也可以直接扫描包体,用xml注入bean有以下方法: & ...
- Spring之使用注解实例化Bean并注入属性
1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...
- spring 注解注入bean
通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...
- Spring 3.0 注解注入详解
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Spring的几种注入bean的方式
在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入 这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的se ...
- Spring学习--通过注解配置 Bean (三)
组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...
- Spring 基础知识 - 依赖注入
所谓的依赖注入是指容器负责创建对象和维护对象间的依赖关系,而不是通过对象本身负责自己的创建和解决自己的依赖. 依赖注入主要目的是为了解耦,体现了一种“组合”的理念. 无论是xml配置.注解配置还是Ja ...
- Spring基础——IOC九种bean声明方式
Spring简介 Spring不是服务于开发web项目的功能,或业务.而是服务于项目的开发,方便各层间的解耦调用,方便对类的批量管理,是提高软件开发效率,降低后期维护成本的框架. Spring的核心思 ...
随机推荐
- Linux 目录共享
## 安装 nfs 和 rpc yum install -y nfs-utils rpcbind ## ubuntu 安装 nfs 和 rpc ## apt-get install nfs-kerne ...
- Servlet中的请求转发RequestDispatcher接口的forword与Include的区别
RequestDispatcher接口中具有两个方法: forward() 与 include() 均 可完成请求 的转发.区别如下: forword(): 使用该方法,则当前 的 Servlet 中 ...
- @RequestMapping的简单理解
@Controller public class ItemController { @Autowired private ItemService itemService; 获取路径参数.../item ...
- CSS3 box-sizing:content-box | border-box
box-sizing:content-box | border-box 默认值:content-box 适用于:所有接受width和height的元素 继承性:无 content-box: paddi ...
- js 获取 touch length
window.addEventListener("touchstart", touchHandler, false); function touchHandler(event){ ...
- Confluence 6.15 博客页面(Blog Posts)宏
博客页面宏允许你 Confluence 页面中显示博客页面.通过单击博客的标题将会把你链接到博客页面中. 使用博客页面宏 为了将博客页面宏添加到页面中: 从编辑工具栏中,选择 插入(Insert) ...
- A. Sea Battle
A. Sea Battle time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- hadoop yarn日志分离
根据hdfs的auditlog以及fsimage分析,yarn的日志文件占用了10%-20%的rpc请求以及文件量,这对namenode的性能有比较大的影响,特别是当集群规模越来越大,会影响生产业务. ...
- 开启两个线程,一个线程打印A~Z,一个线程打印1~52的数据
开启两个线程,一个线程打印A-Z,一个线程打印1-52的数据 import java.util.concurrent.locks.Condition; import java.util.concurr ...
- C# async await and state machine
Async Await and the Generated StateMachine https://www.codeproject.com/Articles/535635/Async-Await-a ...