一、实例化Bean

1. 通过默认构造方法实创建Bean

public class Bean1 {

    public Bean1() {
System.out.println(this.getClass().getSimpleName() + " has been created");
}
}

Bean1

<bean id="bean1" class="com.imooc.springClass2.inject.Bean1"/>

2. 通过静态工厂方法创建Bean

public class Bean1Factory {
public static Bean1 createBean1() {
return new Bean1();
}
}
<bean id="bean1" class="com.imooc.springClass2.inject.Bean1Factory" factory-method="createBean1"/>

3. 通过工厂实例方法创建Bean

public class Bean2Factory {
public Bean2 createBean2() {
return new Bean2();
}
}
<bean id="bean2Factory" class="com.imooc.springClass2.inject.Bean2Factory"/>
<bean id="bean2FromFactory" factory-bean="bean2Factory" factory-method="createBean2"/>

二、注入Bean

public class Bean3 {

    private final Bean1 bean1;
private final String stringValue1;
private final Integer integerValue1;
private Bean2 bean2;
private String stringValue2;
private Integer integerValue2;
private List<String> stringList;
private List<Bean2> bean2List;
private Map<String, Integer> simpleMap;
private Map<Bean1, Bean2> ObjectMap;
private Set<String> stringSet;
private Set<Bean2> bean2Set;
private Properties properties;
private String stringValue3; public Bean3(Bean1 bean1, String stringValue1, Integer integerValue1) {
} public Bean3(Bean1 bean1, String stringValue1, Integer integerValue1) {
this.bean1 = bean1;
this.stringValue1 = stringValue1;
this.integerValue1 = integerValue1;
System.out.println(this.getClass().getSimpleName() + " has been created");
} //get/set ......
}

1. 通过构造方法注入Bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<constructor-arg index="0" name="bean1" type="com.imooc.springClass2.inject.Bean1" ref="bean1"/>
<constructor-arg index="1" name="stringValue1" type="java.lang.String" value="aaaaa"/>
<constructor-arg index="2" name="integerValue1" type="java.lang.Integer" value="11111"/>
</bean>

其中,index、name、type无需全部都有,可区分是哪个参数即可。

简化版

<bean id="bean3FromSimple" class="com.imooc.springClass2.inject.Bean3" c:bean1-ref="bean1" c:stringValue1="aaaaa" c:integerValue1="11111"/>

2. 通过set方法注入Bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="bean2" ref="bean2"/>
<property name="stringValue2" value="bbbbb"/>
<property name="integerValue2" value="22222"/>
</bean>

简化版

<bean id="bean3FromSimple" class="com.imooc.springClass2.inject.Bean3" p:bean2-ref="bean2" p:stringValue2="bbbbb" p:integerValue2="22222"/>

3. 集合类型注入Bean

List

<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringList">
<list>
<value>ccccc</value>
<value>ddddd</value>
</list>
</property>
<property name="bean2List">
<list>
<ref bean="bean2"/>
<ref bean="bean2"/>
</list>
</property>
</bean>

Map

<bean id="bean1" class="com.imooc.springClass2.inject.Bean1"/>
<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="simpleMap">
<map>
<entry key="eeeee" value="33333"/>
<entry key="fffff" value="44444"/>
</map>
</property>
<property name="objectMap">
<map>
<entry key-ref="bean1" value-ref="bean2"/>
</map>
</property>
</bean>

Set

<bean id="bean2" class="com.imooc.springClass2.inject.Bean2"/>
<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringSet">
<set>
<value>eeeee</value>
<value>fffff</value>
</set>
</property>
<property name="bean2Set">
<set>
<ref bean="bean2"/>
</set>
</property>
</bean>

Properties

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="properties">
<props>
<prop key="key1">value1</prop>
<prop key="key2">value2</prop>
</props>
</property>
</bean>

4. null值注入Bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="stringValue3">
<null/>
</property>
</bean>

5. 注入内部bean

<bean id="bean3" class="com.imooc.springClass2.inject.Bean3">
<property name="bean2">
<bean class="com.imooc.springClass2.inject.Bean2"/>
</property>
</bean>

Spring入门之二-------SpringIoC之实例化Bean以及注入Bean的更多相关文章

  1. Spring入门学习(二)三种实例化bean的方法

    前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...

  2. Spring学习总结二——SpringIOC容器二

    一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...

  3. Spring入门(二)

    Spring IOC&DI 控制反转(inversion of control):控制什么?什么反转? 我们都知道,传统的程序中,如果A类需要使用B类对象,会在程序中直接创建B类对象实例,此时 ...

  4. Spring入门(二)——DI

    1. DI Dependency Injection,依赖注入.当对象里有属性或对象的时候,就需要为这些属性或对象赋值 2. 流程 这里介绍两种方式 set方法 注解方式 2.1 set方法 Bean ...

  5. spring实战一:装配bean之注入Bean属性

    内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...

  6. Spring入门(二)— IOC注解、Spring测试、AOP入门

    一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...

  7. spring入门(二)【加载properties文件】

    在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...

  8. Spring入门(十二):Spring MVC使用讲解

    1. Spring MVC介绍 提到MVC,参与过Web应用程序开发的同学都很熟悉,它是展现层(也可以理解成直接展现给用户的那一层)开发的一种架构模式,M全称是Model,指的是数据模型,V全称是Vi ...

  9. Spring入门(二):SpringBoot之基础Web开发

    接上回 现在,我们已经能自行完成SpringBoot的初级项目搭建了,接下来看如何实现一些Web开发中的基础功能. 先看项目完整的目录结构: 1. 返回Json数据 创建model文件夹,并新建Per ...

随机推荐

  1. 使用redis集群中遇到的错误

    一. 上述错误的原因: 1.在redis服务器上关闭防火墙 2.可能是host写错了 上述错误的原因: 配置文件中jedisClient代表的是单机版的redis,但是在类中转化的时候转化的是集群版

  2. WPF学员管理系统

    下载

  3. 吴裕雄--天生自然JAVA数据库编程:PrepareStatement

    import java.sql.Connection ; import java.sql.DriverManager ; import java.sql.SQLException ; import j ...

  4. springmvc 后台实体类接受前端json字符串时,其中一个属性content 接受富文本内容时 标签<p>、<span> 这些标签丢失问题解决

    问题描述: 前端一个字段 <script id="editor" type="text/plain" name="content" s ...

  5. Python--unique()与nunique()函数

    参考:https://www.cnblogs.com/xxswkl/p/11009059.html 1 unique() 统计list中的不同值时,返回的是array.它有三个参数,可分别统计不同的量 ...

  6. Day6 - K - 陌上花开 HYSBZ - 3262

    有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),用三个整数表示. 现在要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量. 定义一朵花A比另一朵花B要美丽,当且仅Sa>= ...

  7. Ado.NET SQLHelper(2)

    测试发现前面发的那个功能太简单,不能调用getdate()等内部函数.  完善后重载了insert和update两个功能,将函数作为字符串传入SQL语句构造,需要的可以试用一下   using Sys ...

  8. postman 请求get post方法的 区别

    1.HTTP的五种请求方法:GET, POST ,HEAD,OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法. GET请求:请求指定的页面信息,并返回实体主体.(通常用 ...

  9. 数据结构第二版之(课后题)BF算法病毒感染检测

    //vs2013下编译通过.换别的编译器自行补充头文件和修改源代码#include<iostream> #include<fstream> #include <strin ...

  10. Django(十)模型:django模型类对数据库的:增/删/改/查、自关联、管理器、元选项(指定表名)

    一.插入.更新和删除 调用一个模型类对象的save方法的时候就可以实现对模型类对应数据表的插入和更新. 调用一个模型类对象的delete方法的时候就可以实现对模型类对应数据表数据的删除. 二.自关联 ...