【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 ...
随机推荐
- PAT A1021 Deepest Root (25 分)——图的BFS,DFS
A graph which is connected and acyclic can be considered a tree. The hight of the tree depends on th ...
- gitlab分支代码本地拉取及jenkins关联gitlab分支
git本地拉取 git init git remote add origin http://47.*.*.*:8089/back_dev/claimeureka.git git fetch origi ...
- java 桥接模式
桥接(Bridge)是用于把抽象化与实现化解耦,使得二者可以独立变化,它通过提供抽象化和实现化之间的桥接结构,来实现二者的解耦. 1)适配器:改变已有的两个接口,让他们相容. 2)桥接模式:分离抽象化 ...
- Omi框架学习之旅 - 插件机制之omi-transform及原理说明
给omi-transform插件做个笔记,使用起来也很爽. transform.js这个库,一直想写一篇帖子的,可是,数学不好,三维矩阵和二位矩阵理解的不好,所以迟迟没写了, 这也是一个神库,反正我很 ...
- abp 取消权限校验
在abp中,通过ABP_PERMISSIONS表来存储定义appService中的方法权限校验.设置方式如下: [AbpAuthorize(PermissionNames.Pages_Users)] ...
- arm那些事
ARM简介 ARM的商业模式: ARM只负责设计IC,并且出卖自己的设计IP(版权). ARM自己不生产芯片,而是把设计IP授权给其他半导体厂商来生产芯片. 严格地说,ARM并不是一家半导体厂商. ...
- Python基础语法复习
1.数据类型 List 列表 函数 append(): 在列表末尾追加. count(): 计算对象在列表中出现的次数. extend():将列表内容添加到列表中. index(): 计算对象在列表中 ...
- BZOJ2125 最短路 圆方树、倍增
传送门 对仙人掌建立圆方树,然后对边定权 对于圆点和圆点之间的边,是原来仙人掌上的桥,边权保持不变 对于圆点和方点之间的边,将圆方树看做以一个圆点为根的有根树之后,一个方点的父亲一定是一个圆点.对于这 ...
- Luogu2164 SHOI2007 交通网络 期望、BFS、拓扑排序
传送门 题目还算不难吧 首先我们枚举点$i$,将其他所有点到这个点的最短路求出来 然后我们在这一次建出的最短路$DAG$的反图上进行拓扑排序.假设我们算到了点$j$,点$j$的人流量为$t_j$,点$ ...
- (转)Putty server refused our key的三种原因和解决方法
原文 上一篇博文介绍了使用Putty免密码登录,我后面试了另一台虚拟机,结果putty显示错误server refused our key(在linux下则表现为仍需要输入密码),搜索了下,很多人都遇 ...