一、Spring中装配bean的方式

1.在XML中显式配置

2.在Java中进行显式配置

3.隐士的bean发现机制和自动装配

二、自动装配示例

1.在需要装配到其他bean中的类中加入@Component注解

package study.spring.configure.auto;

import org.springframework.stereotype.Component;

/**
* 第一步:将该类声明成一个组件类,括号内的参数为组件类的id自定义名称,也可以使用@Named.
* spring会自动生成该类的bean
* @author wang
*
*/
@Component("lonelyHeartsClub")
public class SgtPeppers implements CompactDisc{ private String titil = "Sgt. Pepper's Lonely Hearts Club Band.";
private String artist = "The Beatles"; @Override
public void play() { System.out.println("Playing " + titil + " by " + artist);
} }

2.开启组件扫描

  i.使用java配置开启

package study.spring.configure.auto;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /*
* 使用@ComponentScan注解开启组件扫描,设置扫描的基础包名
* 参数basePackages
* 或者basePackageClasses
*/ @Configuration
@ComponentScan(basePackages={"study.spring.configure.auto","study.spring.configure.auto2"})
public class CDPlayerConfig { }

  ii.使用xml开启

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd"> <context:component-scan base-package="study.spring.configure.auto"></context:component-scan> </beans>

3.在注入的bean中选择三种方法,使用@Autowired对bean注入

  i.在属性上直接注入

  ii.在构造方法上注入

  iii.在Set方法上注入

package study.spring.configure.auto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class CDPleayer { @Autowired
private CompactDisc cd; public CompactDisc getCd() {
return cd;
} /*
* 不同的注入方法
* 1. set方法注入
* 2. 构造器注入
* 3. 属性上直接注入
*
* @Autowired与@Inject类似
*/ @Autowired
public void setCd(CompactDisc cd) {
this.cd = cd;
} @Autowired(required=false)
public CDPleayer(CompactDisc compactDisc) {
this.cd = compactDisc;
} public void play(){
cd.play();
}
}

  注:可以在任何方法上使用@Autowired注入

三、自动注入的局限性  

  虽然自动注入很方便,但是自动注入需要自动创建bean实例,但是对于第三方的jar包中的类文件而言,不能直接使用注解进行声明为组件,因此还需要xml配置。

【Spring】—— 自动装配的更多相关文章

  1. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  2. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

  3. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  4. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  5. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  6. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  7. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

  8. spring自动装配

    spring提供了自动装配(autowiring)和自动检测(autodiscovery)用来减少XML的配置数量. 自动装配bean属性 byName——把与Bean的属性具有相同名字(或ID)的其 ...

  9. Spring自动装配与扫描注解

    1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...

  10. Spring 自动装配及自动注册的相关配置

    Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...

随机推荐

  1. java 日期与时间类

    1.Date类:  https://www.cnblogs.com/huangminwen/p/5994927.html 2.DateFormat和SimpleDateFormat (simple简单 ...

  2. Sublime3 - 插件cssrem

    一个CSS的px值转rem值的Sublime Text 3自动完成插件. 插件效果如下: 安装: 1. 现在本地clone一份: git clone https://github.com/hyb628 ...

  3. 监控虚拟机跟外部的tcp连接

    1.监控虚拟机跟外部的tcp连接,如果连接数超过阈值,就在FORWARD把ip DROP ,并且发送邮件 root@InternetGateway:~# cat /root/scripts/check ...

  4. Vue-Vue列表渲染v-for

    v-for 指令需要以 site in sites 形式的特殊语法 一.v-for 循环数组 HTML代码 <div id="app"> <ul> < ...

  5. 基于tensorflow的躲避障碍物的ai训练

    import pygameimport randomfrom pygame.locals import *import numpy as npfrom collections import deque ...

  6. kafka学习1:kafka安装

    一.环境准备 1.jdk 如果不会安装linux下的jdk,参考这篇文章:http://www.cnblogs.com/gudi/p/7812033.html   2.kafka wget –c ht ...

  7. [Oacle][Partition]Partition操作与 Index, Global Index 的关系

    [Oacle][Partition]Partition操作与 Index, Global Index 的关系: ■ Regarding the local index and the global i ...

  8. 机器学习 第四篇:OLS回归分析

    变量之间存在着相关关系,比如,人的身高和体重之间存在着关系,一般来说,人高一些,体重要重一些,身高和体重之间存在的是不确定性的相关关系.回归分析是研究相关关系的一种数学工具,它能帮助我们从一个变量的取 ...

  9. YY:2018互联网创业公司应看清的事情

    潮流,技术,生活方式,盈利模式,消费人群几乎每年都在改变,2018,你看到的是怎样的一盘棋. 2018年是个很好的数字,很多互联网公司寄予希望在这个幸运数字年头奋起一搏,拿到一份可观的酬金.特别是一些 ...

  10. SC1243sensor噪点问题调试

    接手一块SC1243sensor的板子调试,仔细核对了原理图和PCB发现,PCB不是很好,电源处理不够好,但是出图了,问题是有噪点,麻点,根据经验要求软件修改了PCLK的极性噪点消失,问题解决. 1: ...