一、实例化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. JS 函数创建、封装、调用

    一.简单函数创建.封装 第三种就是构造函数 function fun(a,b){ this.firstName=a this.lastName=b } var x=new myFun(Jhon,Dav ...

  2. 项目启动异常,java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext

    java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' befo ...

  3. 使用Spring Cloud Gateway保护反应式微服务(二)

    抽丝剥茧,细说架构那些事——[优锐课] 接着上篇文章:使用Spring Cloud Gateway保护反应式微服务(一) 我们继续~ 将Spring Cloud Gateway与反应式微服务一起使用 ...

  4. HTML设置目标页面在新窗口打开

    在使用<a></a>标签进行超链接跳转时,目标页面默认在当前页面中打开. 如果希望当前页面中所有超链接跳转页面的时候,都在新窗口中打开,那么只需要在head标签中设置 base ...

  5. 吴裕雄--天生自然JAVA数据库编程:处理大数据对象

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

  6. zabbix java gateway配置实战案例

    zabbix java gateway配置实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.部署tomcat服务 博主推荐阅读: CentOS: https://www. ...

  7. Eclipse打包Android项目时用到proguard.cfg后,出现的Warning:can't find referenced class问题的解决方案

    原文地址:http://blog.csdn.net/u_xtian/article/details/7495023 这个看似简单的问题困扰了我好久了,我已经google了很多相关的信息了,但是在我看来 ...

  8. Sqlserver自动备份bat

    1.bat文件 @echo off echo 删除30天前的备分文件和日志 forfiles /p /c "cmd /c del @path" \Tools\Binn echo 数 ...

  9. 吴裕雄--天生自然java开发常用类库学习笔记:Stack类

    import java.util.Stack ; public class StackDemo{ public static void main(String args[]){ Stack<St ...

  10. c# quartz

    quartz调度核心元素: Scheduler:任务调度器,是实际执行任务调度的控制器.在spring中通过SchedulerFactoryBean封装起来. Trigger:触发器,用于定义任务调度 ...