通过配置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 自动装配;方法注入的更多相关文章

  1. 23、自动装配-Aware注入Spring底层组件&原理

    23.自动装配-Aware注入Spring底层组件&原理 Aware 接口,提供了类似回调函数的功能 自定义组件想要使用Spring 容器底层的一些组件(Application Context ...

  2. Spring自动装配之依赖注入(DI)

    依赖注入发生的时间 当Spring IOC 容器完成了Bean 定义资源的定位.载入和解析注册以后,IOC 容器中已经管理类Bean定义的相关数据,但是此时IOC 容器还没有对所管理的Bean 进行依 ...

  3. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  4. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  5. Spring自动装配----注解装配----Spring自带的@Autowired注解

    Spring自动装配----注解装配----Spring自带的@Autowired注解 父类 package cn.ychx; public interface Person { public voi ...

  6. Spring系列七:Spring 自动装配

    相思相见知何日?此时此夜难为情. 概述 在Spring框架中,在配置文件中声明bean的依赖关系是一个很好的做法,因为Spring容器能够自动装配协作bean之间的关系.这称为spring自动装配. ...

  7. 【spring 注解驱动开发】spring自动装配

    尚学堂spring 注解驱动开发学习笔记之 - 自动装配 自动装配 1.自动装配-@Autowired&@Qualifier&@Primary 2.自动装配-@Resource& ...

  8. Spring自动装配(二)

    为什么Spring要支持Autowire(自动装配) 先写几个类,首先定义一个Animal接口表示动物: 1 public interface Animal { 2 3 public void eat ...

  9. Spring自动装配歧义性笔记

    Spring自动装配歧义性笔记 如果系统中存在两个都实现了同一接口的类,Spring在进行@Autowired自动装配的时候,会选择哪一个?如下: // 一下两个类均被标记为bean @Compone ...

  10. spring 自动装配 default-autowire=&quot;byName/byType&quot;

    <PRE class=html name="code">spring 自动装配 default-autowire="byName/byType"   ...

随机推荐

  1. linux下Tomcat shutdown无效

    问题: linux下Tomcat shutdown无效 linux下关闭tomcat后,发现重新启动Tomcat后.port号提示被占用, 原因: 这时可能是项目中的后台线程或者socket依旧在执行 ...

  2. tensorflow 的 tutorial 的卷积神经网络的例子 convolutional.py

    具体的网址在这里: https://github.com/tensorflow/tensorflow/tree/r0.12/tensorflow/models 一个卷积神经网络用于股票分析的例子:  ...

  3. 如何使用django中的cookie和session?

    1.Cookie 介绍 Cookie是由服务器端生成,发送给User-Agent(一般是浏览器),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站时就发送该Co ...

  4. Mycat教程---数据库的分库分表

    mycat介绍 介绍在官方网站上有比较详细的介绍,在这里复制粘贴没什么意思,大家到官网上看 官网链接 前置条件 本教程是在window环境下运行的,实际生产推荐在Linux上运行. 必备条件(自行安装 ...

  5. Selenium WebDriver 工作原理

    WebDriver与之前Selenium的js注入实现不同:Selenium通过JS来定位元素处理元素(基本上所有元素都可以定位到)WebDriver通过WebDriver API定位处理元素:通过浏 ...

  6. Java面向对象—多态

    概述:同一个事物,在不同的时刻表现出不同的状态. 代码中如何体现: 要有继承, 要有方法重写, 父类引用指向子类对象 多态的成员访问特点 成员变量:编译看左边(父类), 运行看左边 成员方法:编译看左 ...

  7. #define只有一个参数

    define后面只有一个名字,那么这个宏是空的,不会对代码产生影响. 用来便于阅读的

  8. Spring_事务-注解代码

    applicationContext.xml <?xml version="1.0" encoding="UTF-8"?><beans xml ...

  9. cf780c

                                                                                             C. Andryush ...

  10. Registering Components-->Autofac registration(include constructor injection)

    https://autofaccn.readthedocs.io/en/latest/register/registration.html Registration Concepts  (有4种方式来 ...