Spring 自动装配;方法注入
通过配置defalut—autowire属性,Spring IOC容器可以自动为程序注入Bean;默认是no(不启用自动装配)。
default—autowire的类型有:
byName:通过名称自动进行匹配
byType:通过属性自动进行匹配
示例如下:
一个实体类people

public class People{
private int id;
private String name;
private int age;
private Dog dog;
}

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"
default-autowire="byName">
<!-- byName通过属性名,byType通过类型只要类型与属性类型相同就可以自动装配 --> <bean id="dog" class="com.maya.model.Dog">
<property name="name" value="jack"></property>
</bean>
<bean id="dog1" class="com.maya.model.Dog">
<property name="name" value="tom"></property>
</bean>
<!-- 在这里不需要,手动进行注入bean,因为people中的属性名是dog,那么它会自动装配id是dog的类 -->
<bean id="people1" class="com.maya.model.People">
<property name="id" value="1"></property>
<property name="name" value="小明"></property>
<property name="age" value="15"></property>
</bean>
</beans>

建议:自动装配机制慎用,它屏蔽了装配细节,容易产生潜在的错误;
方法注入:
Spring 管理的bean的作用域默认是单例的singleton; 但是可以通过配置prototype,实现多例;
那么就需要用到方法注入:lookup-method
如果我想让Spring管理的bean在我每次调用的时候都是新的,那么就需要如下配置(但是这样做的前提条件是:我没有手动将这条狗注入到people中)
<bean id="dog" class="com.maya.model.Dog" scope="prototype"><!-- 将scope属性设置为prototype -->
<property name="name" value="jack"></property>
</bean>
如果将dog手动注入到了people中的话,这样做是无法改变其单例的模式,依然会是同一条狗
Spring 自动装配;方法注入的更多相关文章
- 23、自动装配-Aware注入Spring底层组件&原理
23.自动装配-Aware注入Spring底层组件&原理 Aware 接口,提供了类似回调函数的功能 自定义组件想要使用Spring 容器底层的一些组件(Application Context ...
- Spring自动装配之依赖注入(DI)
依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- 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自动装配
尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...
- Spring自动装配(二)
为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...
- Spring自动装配歧义性笔记
Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...
- spring 自动装配 default-autowire="byName/byType"
<PRE class=html name="code">spring 自动装配 default-autowire="byName/byType" ...
随机推荐
- 我的Android进阶之旅------>Android无第三方Jar包的源代报错:The current class path entry belongs to container ...的解决方法
今天使用第三方Jar包afinal.jar时候.想看一下源码,无法看 然后像加入jar相应的源代码包.也无法加入相应的源代码,报错例如以下:The current class path entry b ...
- OpenSSL和https原理
https原理: 浏览器请求服务端的公钥证书,server将注冊的证书发送给client. client向办法机构验证证书的合法性,证书 包含公钥,server网址及一些信息. 验证完成,client ...
- FPGA电源设计
LDO(低压差线性稳压器),FPGA需要3.3V.2.5V和1.2V,可选用凌力尔特LINEAR:LT1083/84/85,低压差正压可调稳压器. 应用电路如图所示: 输入端加10UF电解电容,输出端 ...
- JS连等赋值的坑
cnblogs标题: JS连等赋值的坑 关于JS连等赋值有个经典的笔试题: var a = {n: 1}; var b = a; a.x = a = {n: 2}; console.log(a.x); ...
- 使用Berkeley Parser进行句法分析
一.句法分析 1.定义 句法分析判断输入的单词序列(一般为句子)的构成是否合乎给定的语法,并通过构造句法树来确定句子的结构以及各层次句法成分之间的关系,即确定一个句子中的哪些词构成一个短语,哪些词是动 ...
- c++ 11 bind function
Year 2011陈 良乔C++11 FAQ std::function 和 std::bind 标准库函数bind()和function()定义于头文件中(该头文件还包括许多其他函数对象),用于处理 ...
- xlrd、xlwt操作execl表格
在python中操作execl进行数据读写的时候,可以使用xlrd进行文件的读取,使用xlwt将数据写入execl中. 1.xlrd xlwt用来读取execl中的数据,常见的用法如下. (1)打开e ...
- Excel常见操作,重复数据,去除数据关联
Eecel对一个数据进行操作后按住右下角的十字架往下拉就可以对下面的操作进行相同 的操作,所以只需先对一个数据进行操作,再拉下来就可以了 通过公式处理的数据跟其它数据有关联 需要对这些数据进行去除它们 ...
- ceph存储安装配置
1.修改yum源: 1.安装yum源:sudo yum install -y yum-utils sudo yum-config-manager --add-repo https://dl.fedor ...
- Java内置锁synchronized的可重入性
学习自 https://blog.csdn.net/aigoogle/article/details/29893667 对我很有帮助 感谢作者