目录

理论

代码及演示


理论

注入Bean:可以通过构造方法注入Bean,通过Set方法注入Bean;

集合类型有:List、Set、Map、Properties;

以及特殊的null值的注入;

通过构造方法注入Bean

<bean id="bean" class="com...Bean"><constructor-arg index="0" name="anotherBean" type="com...AnotherBean" ref="anotherBean /><constructor-arg index="1" name="stringValue" type="java.lang.String" value="stringValue1" />
</bean>

通过set方法注入Bean

<bean id="bean" class="com...Bean"><property name="anotherBean" ref="anotherBean" /><property name="string" value="setValue1"/>
</bean>

集合类型Bean注入

<bean id="bean" class="com...Bean"><property name="litOrSet"><list>     <!-- <set> --><value>aaaa</value><value>bbbb</value></list>    <!-- </set> --></property>
</bean>
<bean id="bean" class="com...Bean"><property name="mapOrProp"><map>    <!-- <props> --><entry key="aaa" value="111" /><prop key="aaa">111</prop></map>    <!-- </props> --></property>
</bean>

注入null值

<bean id="bean" class="com..Bean"><property name="nullValue"><null/></property>
</bean>

代码及演示

程序运行截图如下:

下面给出txt文本:

bean = Bean{anotherBean=injectbean.demo.AnotherBean@5c84624f, string='通过构造方法注入Bean', anotherBean1=injectbean.demo.AnotherBean@5c84624f, string1='通过set方法注入Bean', stringList=[第一个List值, 第二个List值, 第三个List值], anotherBeanList=[injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f], stringMap={key1=value1, key2=value2}, anotherBeanMap={injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f}, stringSet=[第一个Set值, 第二个Set值, 第三个Set值], anotherBeanSet=[injectbean.demo.AnotherBean@5c84624f], properties={heheda=dadada}}
---------华丽的分割线---------
bean.getStringList() = [第一个List值, 第二个List值, 第三个List值]
bean.getStringSet() = [第一个Set值, 第二个Set值, 第三个Set值]
bean.getStringMap() = {key1=value1, key2=value2}
bean.getAnotherBeanList() = [injectbean.demo.AnotherBean@5c84624f, injectbean.demo.AnotherBean@5c84624f]
bean.getAnotherBeanSet() = [injectbean.demo.AnotherBean@5c84624f]
bean.getAnotherBeanMap() = {injectbean.demo.AnotherBean@5c84624f=injectbean.demo.AnotherBean@5c84624f}
bean.getProperties() = {heheda=dadada}
bean.getAnotherBean2() = null

程序结构如下:

源码如下:

AnotherBean.java

package injectbean.demo;public class AnotherBean {}

Bean.java

package injectbean.demo;import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;public class Bean {@Overridepublic String toString() {return "Bean{" +"anotherBean=" + anotherBean +", string='" + string + '\'' +", anotherBean1=" + anotherBean1 +", string1='" + string1 + '\'' +", stringList=" + stringList +", anotherBeanList=" + anotherBeanList +", stringMap=" + stringMap +", anotherBeanMap=" + anotherBeanMap +", stringSet=" + stringSet +", anotherBeanSet=" + anotherBeanSet +", properties=" + properties +'}';}//通过构造方法注入Beanprivate AnotherBean anotherBean;private String string;public Bean(AnotherBean anotherBean, String string) {this.anotherBean = anotherBean;this.string = string;}public AnotherBean getAnotherBean() {return anotherBean;}public void setAnotherBean(AnotherBean anotherBean) {this.anotherBean = anotherBean;}public String getString() {return string;}public void setString(String string) {this.string = string;}//通过set方法注入Beanprivate AnotherBean anotherBean1;private String string1;public AnotherBean getAnotherBean1() {return anotherBean1;}public void setAnotherBean1(AnotherBean anotherBean1) {this.anotherBean1 = anotherBean1;}public String getString1() {return string1;}public void setString1(String string1) {this.string1 = string1;}//集合类型Bean的注入private List<String> stringList;private List<AnotherBean> anotherBeanList;private Map<String, String> stringMap;private Map<AnotherBean, AnotherBean> anotherBeanMap;private Set<String> stringSet;private Set<AnotherBean> anotherBeanSet;private Properties properties;public List<String> getStringList() {return stringList;}public void setStringList(List<String> stringList) {this.stringList = stringList;}public List<AnotherBean> getAnotherBeanList() {return anotherBeanList;}public void setAnotherBeanList(List<AnotherBean> anotherBeanList) {this.anotherBeanList = anotherBeanList;}public Map<String, String> getStringMap() {return stringMap;}public void setStringMap(Map<String, String> stringMap) {this.stringMap = stringMap;}public Map<AnotherBean, AnotherBean> getAnotherBeanMap() {return anotherBeanMap;}public void setAnotherBeanMap(Map<AnotherBean, AnotherBean> anotherBeanMap) {this.anotherBeanMap = anotherBeanMap;}public Set<String> getStringSet() {return stringSet;}public void setStringSet(Set<String> stringSet) {this.stringSet = stringSet;}public Set<AnotherBean> getAnotherBeanSet() {return anotherBeanSet;}public void setAnotherBeanSet(Set<AnotherBean> anotherBeanSet) {this.anotherBeanSet = anotherBeanSet;}public Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}//null值注入private AnotherBean anotherBean2;public AnotherBean getAnotherBean2() {return anotherBean2;}public void setAnotherBean2(AnotherBean anotherBean2) {this.anotherBean2 = anotherBean2;}
}

spring.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd"><bean class="injectbean.demo.AnotherBean" id="anotherBean" /><bean class="injectbean.demo.Bean" id="bean"><constructor-arg index="0" type="injectbean.demo.AnotherBean" ref="anotherBean"/><constructor-arg index="1" type="java.lang.String" value="通过构造方法注入Bean" /><property name="anotherBean1" ref="anotherBean" /><property name="string1" value="通过set方法注入Bean" /><property name="stringList"><list><value>第一个List值</value><value>第二个List值</value><value>第三个List值</value></list></property><property name="anotherBeanList"><list><ref bean="anotherBean" /><ref bean="anotherBean" /></list></property><property name="stringSet"><set><value>第一个Set值</value><value>第二个Set值</value><value>第三个Set值</value></set></property><property name="anotherBeanSet"><set><ref bean="anotherBean" /><ref bean="anotherBean" /></set></property><property name="stringMap"><map><entry key="key1" value="value1" /><entry key="key2" value="value2" /></map></property><property name="anotherBeanMap"><map><entry key-ref="anotherBean" value-ref="anotherBean" /></map></property><property name="properties"><props><prop key="heheda">dadada</prop></props></property><property name="anotherBean2"><null/></property></bean>
</beans>

DemoApplicationTests.java

package injectbean.demo;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class DemoApplicationTests {@Testpublic void contextLoads() {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");Bean bean = context.getBean("bean", Bean.class);System.out.println("bean = " + bean);System.out.println("---------华丽的分割线---------");System.out.println("bean.getStringList() = " + bean.getStringList());System.out.println("bean.getStringSet() = " + bean.getStringSet());System.out.println("bean.getStringMap() = " + bean.getStringMap());System.out.println("bean.getAnotherBeanList() = " + bean.getAnotherBeanList());System.out.println("bean.getAnotherBeanSet() = " + bean.getAnotherBeanSet());System.out.println("bean.getAnotherBeanMap() = " + bean.getAnotherBeanMap());System.out.println("bean.getProperties() = " + bean.getProperties());System.out.println("bean.getAnotherBean2() = " + bean.getAnotherBean2());}}

Spring学习笔记-构造和Set方法注入Bean及集合和null值的注入相关推荐

  1. Spring学习记录(九)---通过工厂方法配置bean

    1. 使用静态工厂方法创建Bean,用到一个工厂类 例子:一个Car类,有brand和price属性. 1 package com.guigu.spring.factory; 2 3 public c ...

  2. Spring学习笔记4,注解方式管理Bean

    Spring框架的Bean管理注解方式的快速入门 1. 步骤一:下载Spring框架的开发包 解压后的目录结构如下 * docs – API和开发规范 * libs – jar包和源码      Sp ...

  3. spring学习(52):工厂方法创建bean对象

    pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  4. spring学习笔记03-spring-DI-依赖注入详解(通过xml配置文件来配置依赖注入)

    spring学习笔记03-spring-DI-依赖注入详解 1.概念 2.构造函数注入 3.set方法注入 4.集合的注入 需要被注入的实体对象 package com.itheima.service ...

  5. spring学习笔记四(注入Bean属性)

      注入Bean属性 初始化bean的时候我们可能要对生成的Bean对象进行一些初始属性的设置,那么在spring中是如何设置的呢? 我们以下面图中实例来为Bean注入属性 一.通过构造方法来注入属性 ...

  6. JavaEE——Spring学习笔记01【Ioc开发的模式】

    JavaEE--Spring学习笔记01[Ioc开发的模式] JavaEE--Spring学习笔记02[Spring和Mybatis的整合] JavaEE--Spring学习笔记03[AOP开发] J ...

  7. CHY的Spring学习笔记---师从动力节点王鹤老师(B站白嫖)

    Spring学习笔记 核心技术:ioc和aop ioc:使用di(依赖注入)实现控制反转,底层使用的是反射机制 spring可以创建自己写的类的对象,也可以创建非自定义对象,只要知道类的全限定名即可. ...

  8. 【Spring学习笔记 九】Spring声明式事务管理实现机制

    什么是事务?事务就是把一系列的动作当成一个独立的工作单元,这些动作要么全部完成,要么全部不起作用,关乎数据准确性的地方我们一定要用到事务,防止业务逻辑出错. 什么是事务管理,事务管理对于企业应用而言至 ...

  9. Spring 学习笔记----->AOP

    Spring 学习笔记----->AOP 代理模式 为什么学代理模式? 因为这就是Spring Aop的底层 代理模式的分类: 静态代理 动态代理 静态代理 生活用的例子: 房东 public ...

最新文章

  1. CV圈太卷了!继谷歌提出MLP-Mixer之后,清华、牛津等学者又发表三篇MLP相关论文...
  2. 诺奖得主克鲁格曼:比特币是庞氏骗局,但不一定很快走向崩溃
  3. 学用MVC4做网站五:5.1添加文章
  4. 大学php老师,php高校教师总结计划系统
  5. Spring Session, Redis 实现微服务 Session 共享
  6. javascript判断一个元素是另外一个元素的子元素
  7. 移动端 html5领奖页面,HTML5移动端交互
  8. 鸿蒙2秒开机官方,两款荣耀智慧屏发布:鸿蒙OS首秀2秒开机,安卓应用迁移不难...
  9. 构建REST风格的Web Service (转)
  10. 奥迪坚受邀参加银联数据2016年度客服云平台专题研讨会
  11. 在虚拟机vmware中安装windows7保姆级教程
  12. 高分辨率扫描出来的图片有摩尔纹_文档扫描仪选购指南:扫描仪哪个牌子比较好?...
  13. burst什么意思_burst是什么意思_burst在线翻译_英语_读音_用法_例句_海词词典
  14. hadoop如何解除safemode-安全模式
  15. 读书百客:《嘲鲁儒》赏析
  16. 《水墨青花》----徐志摩
  17. 计算机方面的顶级会议
  18. java 位置定位_地图实时定位我的位置
  19. Rsa前后端加密交互 带demo
  20. python基础知识点大全

热门文章

  1. jQuery 1.6 中更新的几个功能
  2. 5.1.2 云计算的定义
  3. 金笛邮件中使用wap邮箱
  4. weblogic部署,常见错误解决——Unmarshaller failed
  5. 分享codeigniter 路由(URL)终极优化
  6. 【转载】此时此刻的飞秋爱好者
  7. 那时我大约5岁的飞鸽传书
  8. 飞鸽传书 bbs以及个人主页服务好不热闹
  9. 用线程实现动态改变图标
  10. HTML5 API详解(6):getUserMedia实现拍照功能