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" ...
随机推荐
- mysql 建立表之间关系 一对一 练习2
创建db5数据库 create database db5 charset=utf8; use db5; 例二:一个管理员唯一对应一个用户 用户表: id user password 1 egon xx ...
- 【Navicat连接Oracle数据库】-Navicat连接Oracle数据库设置
1.navicat连接数据配置信息如下图所示: 点击"确定"按钮,进入到软件 按照图中所画的步骤顺序操作,最后重新启动navicat就可. 关于里面的这个文件夹 insta ...
- Boost scoped_ptr scoped_array 以及scoped_ptr与std::auto_ptr对比
boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放.下列代码演示了该指针的基本应用: #include <str ...
- mysql的两种常用的引擎
MyISAM引擎特点1.不支持事务(事务是指逻辑上的一组操作,组成这组操作的各个单元,要么全成功,要么全失败)2.表级锁定(数据更新时锁整个表):其锁定机制是表级锁定,这虽然可以让锁定的实现成本很小但 ...
- 浅谈WebService SOAP、Restful、HTTP(post/get)请求
http://www.itnose.net/detail/6189456.html 浅谈WebService SOAP.Restful.HTTP(post/get)请求 2015-01-09 19:2 ...
- s5_day9作业
# 1 编写 tail -f a.txt |grep 'error' |grep '404'命令,周一默写 # import time # def tail(filepath,encoding='ut ...
- python 课堂笔记-while
#Author:zyl age_of_oldboy = 56 count = 0 while count < 3: guess_age = int(input("guess age:& ...
- Python 中lambda 简单介绍
转自:https://www.cnblogs.com/AlwaysWIN/p/6202320.html 在学习python的过程中,lambda的语法经常出现,现在将它整理一下,以备日后查看. 1.l ...
- MD5key.java
代码如下: package com.lekou.utils; public class MD5key { ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; ; , , , , , , , , ...
- CPU、内存通俗概念
在计算机的组成结构中,有一个很重要的部分,就是存储器.存储器是用来存储程序和数据的部件,对于计算机来说,有了存储器,才有记忆功能,才能保证正常工作.存储器的种类很多,按其用途可分为主存储器和辅助存储器 ...