【Spring】—— 自动装配
一、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】—— 自动装配的更多相关文章
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring自动装配----注解装配----Spring自带的@Autowired注解
Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...
- Spring系列七:Spring 自动装配
相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- spring自动装配
spring提供了自动装配(autowiring)和自动检测(autodiscovery)用来减少XML的配置数量. 自动装配bean属性 byName——把与Bean的属性具有相同名字(或ID)的其 ...
- Spring自动装配与扫描注解
1 javabean的自动装配 自动注入,减少xml文件的配置信息. <?xml version="1.0" encoding="UTF-8"?> ...
- Spring 自动装配及自动注册的相关配置
Spring支持好几种自动装配(Autowiring)的方式,以及自动扫描并注册Bean的配置(在beans.xml中配置). 下文我们进行一个小结. 1. <context: annotati ...
随机推荐
- 关于pyquery小知识点
#表示的是取html中的id元素, . 表示的是取html中的class元素. 如果是标签,就直接用标签名 而它们之间的空格,则表示嵌套关系 单冒号(:)用于CSS3伪类,双冒号(::)用于CSS3伪 ...
- Dubbo高级篇4
https://blog.csdn.net/moonpure/article/details/52842115 线程模型 http://dubbo.io/User+Guide-zh.htm 用户指南& ...
- python基础学习第一天
def用法 函数定义的基本格式如下: def function(params): somthing return values 说明:return语句可选,出现return语句表示函数 ...
- centos7搭建kibana
上一节elasticsearch搭建地址 https://www.cnblogs.com/mutong1228/p/10181544.html 学习了上一篇的搭建,理解了命令的含义之后,本节就非常方便 ...
- Objective-C 性能监控
1.内存监控 2.卡顿监控 3.fps监控
- [julia]本地离线安装package
1.引言 julia最近十分受关注,其结合了python的通用性,Ruby的动态性,C的代码运行速度,R的包管理和数据分析功能,perl的字符串处理能力,lisp的宏能力,matlab的矩阵计算规则, ...
- odoo11新开发功能模块测试指南
根据实际业务需要,我们开发了一些生产实务中一些功能模块,作为制造行业管理信息化解决方案的基础,并应部分客户需求,做了测试系统,现将测试方式公布如下: 一.测试环境 服务器地址 http://106.1 ...
- Ionic App中嵌入外部网页的问题
在app中不可避免的要引用第三方的页面,那么在Ionic中是如何实现呢? 1.设计引用外部页面的html框架页面,分3部分,表头有2个按钮,中间是引用的页面,底部隐藏分享相关按钮,具体页面如下: &l ...
- 干货分享:vue2.0做移动端开发用到的相关插件和经验总结(2)
最近一直在做移动端微信公众号项目的开发,也是我首次用vue来开发移动端项目,前期积累的移动端开发经验较少.经过这个项目的锻炼,加深了对vue相关知识点的理解和运用,同时,在项目中所涉及到的微信api( ...
- BGFX 渲染引擎中着色器代码的调试方法
在实时渲染的图形开发中,着色器代码(Shader)越来越复杂,于是单纯的靠经验和不断试错的开发和调试方法早已不能满足实际需求.使用调试工具进行调试,成为开发中重要的方法.Bgfx 是一款跨平台.抽象封 ...