一、实例化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>

最新文章

  1. 【走过巨坑】android studio对于jni调用及运行闪退无法加载库的问题解决方案
  2. SCNU 2015ACM新生赛初赛【1006. 3D打印】解题报告
  3. pandas 透视表 pivot_table
  4. Tomcat &amp; Nginx
  5. Cygwin环境编译/usr/include/sys/_types.h:72:20: 致命错误:stddef.h:can not found
  6. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
  7. php 二维数组按照某value值求出最大值最小值
  8. CachedRowSet的用法
  9. linux中tar 打包指定路径文件
  10. Access-简易进销存管理系统
  11. How To Configure VMware fencing using fence_vmware_soap in RHEL High Availability Add On(RHEL Pacemaker中配置STONITH)
  12. Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/SpringStruts]]
  13. Necklace(树状数组+离线操作)
  14. python实现三级菜单
  15. [APIO2012]
  16. 20175221 MyCP(课下作业,必做)
  17. javascript小实例,拖拽应用(一)
  18. ESXI6时间源快速同步
  19. System.exit(int status)
  20. (16)Python练习题

热门文章

  1. Linux centos7 shell特殊符号、cut命令、sort_wc_uniq命令、tee_tr_split命令、shell特殊符号
  2. GoJS 友情链接
  3. Http服务和JSP
  4. matlab练习程序(概率路线图PRM)
  5. Lesson 45 Of men and galaxies
  6. greenplum 存储过程 输出信息
  7. centos7下yourcompleteme安装
  8. XtraReport1添加参数
  9. 在命令提示符中运行install adb 包名.apk文件 遇到的问题
  10. css滚动