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 studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:
今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...
- C++11 中的initialize_list
这就是一个简单的模板类,不过在C++中有了特殊的语法支持,定义的时候使用如下的格式: initialize_list<double> dl = {1.1, 1.2}; 或者: initia ...
- Php ArrayIterator的几个常用方法
搜索商低..从php.net找到 ,自己翻译一下 总结在一起 rewind() 指针回到初始位置 valid() 判断数组当前指针下是否有元素 key() 数组键 ...
- is和==
==判断两个对象值是否相等 is表示两个变量是否指向一块对象
- Python Tornado框架三(源码结构)
Tornado 是由 Facebook 开源的一个服务器“套装”,适合于做 python 的 web 或者使用其本身提供的可扩展的功能,完成了不完整的 wsgi 协议,可用于做快速的 web 开发,封 ...
- cdoj1334郭大侠与Rabi-Ribi
地址:http://acm.uestc.edu.cn/#/problem/show/1334 题目: 郭大侠与Rabi-Ribi Time Limit: 3000/1000MS (Java/Other ...
- 软件测试工程师的Linux之路(持续更新修正)
软件测试工程师成长必经之路—Linux学习. 测试工程师不同于运维工程师,所以在对不熟悉Linux的测试人员来说,先了解一些Linux系统的基本操作,能顺利开展测试工作即可,在强迫自己使用,熟悉命令行 ...
- javascript 的dateObj.getTime() 在为C#的获取方式
public string GetTime(DateTime dt) { Int64 retval = 0; DateTime st = new DateTime(1970, 1, 1); TimeS ...
- [国家集训队] Crash 的文明世界(第二类斯特林数)
题目 [国家集训队] Crash 的文明世界 前置 斯特林数\(\Longrightarrow\)斯特林数及反演总结 做法 \[\begin{aligned} ans_x&=\sum\limi ...
- JDK1.7之Fork/join
Fork/Join框架是Java 7提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.Fork/Join框架要完成两件事情: 1.任务分 ...