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" ...
随机推荐
- Python中的不同进制的语法和转换
不同进制的书写方式 八进制(Octal) 0o377 十六进制(Hex) 0xFF 二进制(Binary) 0b11111111 不同进制之间的转换 python提供了三个内置的函数,能够用来在不同进 ...
- 在windows上搭建redis集群
一 所需软件 Redis.Ruby语言运行环境.Redis的Ruby驱动redis-xxxx.gem.创建Redis集群的工具redis-trib.rb 二 安装配置redis redis下载地址 ...
- phpcms 列表页中,如何调用其下的所有子栏目?
{pc:content action="category" catid="$catid" num="99" order="list ...
- POJ1276:Cash Machine(多重背包)
题目:http://poj.org/problem?id=1276 多重背包模板题,没什么好说的,但是必须利用二进制的思想来求,否则会超时,二进制的思想在之前的博客了有介绍,在这里就不多说了. #in ...
- likely(x)与unlikely(x) __builtin_expect
本文讲的likely()和unlikely()两个宏,在linux内核代码和一些应用中可常见到它们的身影.实质上,这两个宏是关于GCC编译器内置宏__builtin_expect的使用. 顾名思义,l ...
- Delphi 正则表达式语法(7): 匹配转义字符
Delphi 正则表达式语法(7): 匹配转义字符 // ? 号的意义是匹配 0-1 次, 如果需要匹配 ? 怎么办 var reg: TPerlRegEx; begin reg := TPe ...
- redis的一些命令
字符串操作 EX在设置值的时候设置过期时间,ttl查看过期时间 expire能单独设置过期时间 查看所有的key key * 列表操作 lpush从列表左边添加值,rpush从列表右边添加值 lran ...
- 从零到一创建ionic移动app:应用anjularjs编写ionic项目
推荐两篇文章,带你入门 ionic中文项目(作为了解ionic基础结构用):http://blog.csdn.net/i348018533/article/details/47258449/ ioni ...
- r.js的build.js的详细配置解析
{ baseUrl: "../src",//当前文件的父目录的兄弟src目录,意思是这个目录会被完全复制到dir目录下面 mainConfigFile: '../src/init- ...
- for 循环与嵌套
循环:反复执行某段代码.循环四要素:初始条件,循环条件,循环体,状态改变 for(初始条件;循环条件;状态改变){ 循环体} 给出初始条件,先判断是否满足循环条件,如果不满足条件则跳过for语句,如果 ...