Spring入门之二-------SpringIoC之实例化Bean以及注入Bean
一、实例化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的更多相关文章
- Spring入门学习(二)三种实例化bean的方法
前面的哪一种就是通过构造函数来实例化对象 下面我们可能用到工厂方法来视力话对象,这样我们的配置文件又该怎么配置呢 <bean name="service2" class=&q ...
- Spring学习总结二——SpringIOC容器二
一:指定bean的依赖关系 例如examplebean对象依赖examplebean1对象,那么在创建examplebean对象之前就 需要先创建examplebean1对象. 1:创建Example ...
- Spring入门(二)
Spring IOC&DI 控制反转(inversion of control):控制什么?什么反转? 我们都知道,传统的程序中,如果A类需要使用B类对象,会在程序中直接创建B类对象实例,此时 ...
- Spring入门(二)——DI
1. DI Dependency Injection,依赖注入.当对象里有属性或对象的时候,就需要为这些属性或对象赋值 2. 流程 这里介绍两种方式 set方法 注解方式 2.1 set方法 Bean ...
- spring实战一:装配bean之注入Bean属性
内容参考自spring in action一书. 创建应用对象之间协作关系的行为通常称为装配,这也是依赖注入的本质. 1. 创建spring配置 spring是一个基于容器的框架.如果没有配置spri ...
- Spring入门(二)— IOC注解、Spring测试、AOP入门
一.Spring整合Servlet背后的细节 1. 为什么要在web.xml中配置listener <listener> <listener-class>org.springf ...
- spring入门(二)【加载properties文件】
在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...
- Spring入门(十二):Spring MVC使用讲解
1. Spring MVC介绍 提到MVC,参与过Web应用程序开发的同学都很熟悉,它是展现层(也可以理解成直接展现给用户的那一层)开发的一种架构模式,M全称是Model,指的是数据模型,V全称是Vi ...
- Spring入门(二):SpringBoot之基础Web开发
接上回 现在,我们已经能自行完成SpringBoot的初级项目搭建了,接下来看如何实现一些Web开发中的基础功能. 先看项目完整的目录结构: 1. 返回Json数据 创建model文件夹,并新建Per ...
随机推荐
- Spring--@configuration 和 @Bean
参考:http://wiki.jikexueyuan.com/project/spring/java-based-configuration.html @Configuration 和 @Bean 注 ...
- 如何使用IDEA将maven项目打成war包
1.点击IDEA界面右边的maven project 2.双击package
- 初学微信小程序——配置问题(1)
一.注册: 微信小程序账号注册:登录https://mp.weixin.qq.com 点击“立即注册”->”小程序” 注册完成后,下载微信小程序开发者工具: 依次点击:“首页”->“文档 ...
- 图片索引 lire
1:定义 LIRE( Lucene Image Retrieval)相似图像索引和搜索机制 2:资料来源 LIRE官网:http://www.semanticmetadata.net/lir ...
- 题目:给定一数组 例如:a = [1,2,3,5,2,1] 现用户提供一个数字 请返回用户所提供的数字的所有下标
def test(ary): ds = {} for i in range(len(ary)): if ds.get(ary[i]): ds[ary[i]].append(i) else: ds[ar ...
- R 《回归分析与线性统计模型》page140,5.1
rm(list = ls()) library(car) library(MASS) library(openxlsx) A = read.xlsx("data140.xlsx") ...
- 云时代架构阅读笔记十一——数据库SQL优化
网上关于SQL优化的教程很多,但是比较杂乱.近日有空整理了一下,写出来跟大家分享一下,其中有错误和不足的地方,还请大家纠正补充. 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 ...
- WEB-INF
WEB-INF下的内容是没有办法通过浏览器去请求的.可以把东西放在WEB-INF下面,避免用户直接通过浏览器请求.那些资源只允许通过url请求过来通过其他途径转发给用户. 比如WEB-INF/jsp/ ...
- 使用Spring Data JPA的Spring Boot
本文教你开始使用Spring Data JPA.来自优锐课JAVA架构专业讲师精心整理. 欢迎使用带有Spring Data JPA的Spring Boot教程!在本教程中,我们将看到Spring D ...
- Django(七)模型:字段属性、字段选项(参数)
一.模型类属性命名限制 参考:https://docs.djangoproject.com/zh-hans/3.0/topics/db/models/ 1)不能是python的保留关键字. 2)不允许 ...