---
title: "spring的自动化装配bean"
author: "bbwang8088"
date: "2018-07-13T09:11:44Z"
tags: []
categories: ["互联网"]
slug: "spring-bean"
draft: false
lastmod: "2018-07-13T09:23:29Z"
id: 2426722
url: "https://bbwang8088.iteye.com/blog/2426722"
---

### Spring的自动化装配bean

 

Spring从两个角度来实现自动化装配：

 

1、组件扫面(**component-scanning**):Spring会自动发现应用上下文中所创建的bean。  
     例如：通过xml标签**&lt;context:component-scan&gt;**启动组件扫描功能

```
<context:component-scan base-package="tech.bbwang.async" />
```

    再如：通过**@ComponentScan**注解启动组件扫描功能

```
@ComponentScan
public class SnsSender{
...
}
```

 

 

2、自动装配(**autowiring**):Spring自动满足bean之间的依赖。

   例如：通过**@Autowire**的注解完成bean依赖,required属性为false时标识如果没有找到对应的bean，则留空。和本注解比较类似的还有**@Inject，**

```
@Autowired(required=false)
private ClientService clientService;
```

 

**摘录自Spring实战(第4版)35页。**