---
title: "spring di2"
author: "冰诺莫语"
date: "2015-01-13T11:24:15Z"
tags: []
categories: ["开源软件"]
slug: "spring-di2"
draft: false
lastmod: "2015-01-13T11:38:02Z"
id: 2175390
url: "https://alex-lip.iteye.com/blog/2175390"
---

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!-- 使用Spring的命名空间p装配属性，配置p --&gt;

&lt;beans xmlns="http://www.springframework.org/schema/beans"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context" 

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"

default-autowire="byType"&gt;

&lt;!-- 如上两行，为容器中所有的Bean设置初始化和销毁方法 --&gt;

&lt;!--

通过&lt;context:annotation-config /&gt;告诉spring我们通过注解进行自动装配。

spring支持集中不同的用于自动装配的注解：

spring自带的@Autowired注解

JSR-330的@Inject注解

JSR-250的@Resource注解

 

@Autowired

对set方法使用该注解，spring尝试使用byType进行装配

该注解可以标注任意方法，包括构造器

当使用该注解直接标注属性时，即可删除setter方法，同时该注解不会受限于private关键字。

@Qualifier("guitar")指定名为guitar的注入

 --&gt;

&lt;context:annotation-config /&gt;

 

&lt;!--

&lt;context:component-scan&gt;除了具有 &lt;context:annotation-config /&gt;一样的工作，还允许spring自动

检测Bean和定义Bean

base-package:扫描包

扫描情况下，如何知道是否应该注册为spring Bean呢？

@Component：通用的构造型注解，表示该类是spring组件，如果@Component("aa")标识，则代表bean的id为aa，否则默认类名首字母小写

@Controller：标识将该类定义为Spring MVC Controller

@Repository：标识将该类定义为数据仓库

@Service：标识将该类定义为服务

--&gt;

&lt;context:component-scan base-package="com.bean"&gt;

&lt;/context:component-scan&gt;

&lt;!-- 

在扫描获得bean时，通过为&lt;context:component-scan&gt;配置&lt;context:include-filter&gt;和/或者&lt;context:exclude-filter&gt;

--&gt;

&lt;context:component-scan base-package="com.bean"&gt;

&lt;context:include-fileter type="assignable" expression="com.bean.Instrument"/&gt;

&lt;/context:component-scan&gt;

 

 

&lt;!-- Bean declarations go here --&gt;

 

&lt;!-- 

自动装配(autowiring)：有助于减少甚至消除配置&lt;property&gt;元素和&lt;constructor-arg&gt;元素，让spring自动识别如何装配Bean的依赖关系。

自动检测(autodiscovery)：比自动装配更进一步，让spring能够自动识别哪些类需要被装配成spring Bean，从而减少对&lt;bean&gt;元素的使用

 

4种类型的自动装配：

byName：把与Bean的属性具有相同名字(或者ID)的其他bean自动装配到Bean的对应属性中。如果没有跟属性的名字相匹配的Bean，则该属性不进行装配。

byType：把与Bean的属性具有相同类型的其他bean自动装配到Bean的对应属性中。如果没有跟属性的类型相匹配的Bean，则该属性不被装配。

constructor：把与Bean的构造器入参具有相同类型的其他Bean自动装配到Bean构造器的对应入参中

autodetect：首先尝试使用constructor进行自动装配。如果失败，再尝试使用byType进行自动装配。

byName限制：需要架设Bean的名字与其他Bean的属性的名字一样

byType限制：如果spring徐找到多个Bean，他们的类型与需要自动装配的属性的类型都相匹配，在这种场景下，spring不会猜测哪一个Bean更适合自动装配，而是选择抛出异常。

                   设置首选Bean，默认bean的primary属性都为true，相当于都是首选Bean，所以需要将不需要的都设置为false。

    排除某些Bean，设置bean的autowire-candidate为false

constructor:局限性和byType相同，不会选择。

 

--&gt;

&lt;bean id="keyny" class="com.bean.Instrumentalist" autowire="byName"&gt;

&lt;property name="song" value="Jingle Bells"/&gt;

&lt;/bean&gt;

&lt;bean id="keyny2" class="com.bean.Instrumentalist" autowire="byType" primary="false" autowire-candidate="false"&gt;

&lt;property name="song" value="Jingle Bells"/&gt;

&lt;/bean&gt;

&lt;bean id="keyny3" class="com.bean.Instrumentalist" autowire="constructor"&gt;

&lt;property name="song" value="Jingle Bells"/&gt;

&lt;/bean&gt;

&lt;bean id="keyny4" class="com.bean.Instrumentalist" autowire="autodetect"/&gt;

 

&lt;/beans&gt;