概念

http://developer.51cto.com/art/200610/33311.htm

http://kb.cnblogs.com/page/45266/

https://www.chkui.com/article/spring/spring_core_stereotype_component_and_bean_scan

https://www.chkui.com/article/spring/spring_core_design_pattern_and_ioc

构造器注入

Value:参数值,通常是基本类型和 type或者index配合使用。

Name:构造函数中参数的名字

Ref:引用其他Spring管理的对象

Type:参数的类型,值为基本类型或者完全限定的类名。

   <constructor-arg index="..." value="..." name="..." ref="..." type="..."></constructor-arg>

使用index,可以不按顺序

采用type进行限定,使用正确的构造器型

使用name 有一些限制,限制在于必须以debug的形式编译类,因为只有这样,才可以保留构造函数中参数的变量名。

或者使用在构造函数中使用注解 @ConstructorProperties

@ConstructorProperties({"years", "ultimateAnswer"})

public ExampleBean(int years, String ultimateAnswer) {

this.years = years;

this.ultimateAnswer = ultimateAnswer;

}

静态工厂方法注入

实例工厂方法注入

设值注入

   property 标签有三个属性 name  ref  和 value
其中 name是将要被注入的属性的名称。
ref 是引用被spring管理的对象

propertyEditor 转换value的值也可以是 spring表达式 spel

类型为java.util.properties可以采用类似下面的配置:

<!-- typed as a java.util.Properties -->
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>

集合

<!-- 集合的注入,包括 set list map properties -->
<bean id="fruitSet" class="com.test.wdid.FruitCollection">
<property name="fruits">
<!-- 集合 set标签,可以定义内部bean,引用其他bean -->
<set>
<bean class="com.test.wdi.Fruit">
<property name="apple" ref="a">
</property>
<property name="banana" ref="b"></property>
</bean>
<ref bean="f1" />
<ref bean="f2" />
<ref bean="f3" />
<ref bean="f4" />
<ref bean="f5a" />
<ref bean="f5b" />
<ref bean="f5c" />
</set>
</property> <property name="fruitList">
<!-- list标签,可以定义内部bean,引用其他bean -->
<list>
<bean class="com.test.wdi.Fruit">
<property name="apple" ref="a">
</property>
<property name="banana" ref="b"></property>
</bean>
<ref bean="f1" />
<ref bean="f2" />
<ref bean="f3" />
<ref bean="f4" />
<ref bean="f5a" />
<ref bean="f5b" />
<ref bean="f5c" />
</list>
</property> <property name="fruitMap">
<!-- map标签 定义enrty 子标签,属性有 key value key-ref value-ref ,意思很明显-->
<map>
<entry key="f1" value-ref="f1" ></entry>
<entry key="f2" value-ref="f2"></entry>
<entry key="f3" value-ref="f3"></entry>
<entry key="f4" value-ref="f4"></entry>
</map>
</property> <property name="ppp"> <!-- props标签 定义prop 子标签,本质是字符串类型的键值对--> <props>
<prop key="1">t1</prop>
<prop key="2">t2</prop>
<prop key="3">t3</prop>
<prop key="4">t4</prop>
</props>
</property>
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>

Null和空串

<!-- 空串和null -->
<bean id="exam5Null" class="com.test.wdi.ExampleBean">
<property name="years" value="1111"></property>
<property name="ultimateAnswer">
<null/>
</property>
<property name="test" value="true"></property>
</bean> <bean id="exam5Empty" class="com.test.wdi.ExampleBean">
<property name="years" value="1111"></property>
<property name="ultimateAnswer" value="">
</property>
<property name="test" value="true"></property>
</bean>

https://blog.csdn.net/windsunmoon/article/details/44019579

spring bean 注入的更多相关文章

  1. [spring]Bean注入——在XML中配置

    Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...

  2. Spring bean注入方式

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html  Spring bean提供了3中注入方式:属 ...

  3. spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)

    这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...

  4. Spring Bean 注入 2 注解篇

    1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...

  5. Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配

    1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...

  6. [spring]Bean注入——使用注解代替xml配置

    使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...

  7. Spring bean注入

    1.构造方法注入 1.bean类 public class User { private String name; private Integer age; private Cat cat; publ ...

  8. Spring JMX之一:使用JMX管理Spring Bean

    spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...

  9. 在非spring组件中注入spring bean

    1.在spring中配置如下<context:spring-configured/>     <context:load-time-weaver aspectj-weaving=&q ...

随机推荐

  1. Django框架的探索

    django框架的路由 django2 路由支持正则匹配,如: re_path(r'^category/(?P<category_id>\d+)/$',CourseCategoryView ...

  2. Selenium+TestNG+Maven+Jenkins+SVN(转载)

    转载自:https://blog.csdn.net/u014202301/article/details/72354069 一. 创建Maven项目,下载Selenium和TestNG的依赖(依赖可以 ...

  3. vim字符查找和替换

    一.替换 1. r+<待替换的字母>    将光标处的字母替换为指定的字母 2.R       连续替换直到按下ESC 3.cc      整行替换,并进入插入模式 4.cw     替换 ...

  4. esxi虚拟机克隆后的主机网卡设置

    windows 在虚拟机配置页面右击属性,编辑虚拟机设置,删除掉原来的网卡,重新添加新的网卡 进入系统以后,进入 windows/system32 找到sysprep,运行等待电脑重启 重启过程会稍微 ...

  5. Codeforces Round #553 (Div. 2) C题

    题目网址:http://codeforces.com/contest/1151/problem/C 题目大意:给定奇数集和偶数集,现构造一个数组,先取奇数集中一个元素1,再取偶数集二个元素2,4,再取 ...

  6. odoo KeyError

    one2many字段对应的表名不存在造成

  7. Java多线程02(线程安全、线程同步、等待唤醒机制)

    Java多线程2(线程安全.线程同步.等待唤醒机制.单例设计模式) 1.线程安全 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运行结果和单线程运行的结果是一样的,而且其他的变量 ...

  8. session cookie简介

    会话机制:Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端记录信息确定用户身 ...

  9. vmware 挂起后不能恢复

    报错:未能锁定主内存文件,还原虚拟机状态时出错 虚拟机目录下有一个文件夹,xxx.vmem.lck,里面的lck文件是很久以前的,把它删掉重新恢复就可以了.

  10. 探索未知种族之osg类生物---渲染遍历之Renderer::draw()简介

    我们今天进入上一节的遗留问题Renderer::draw()的探究. 1.从_drawQueue中取出其中一个sceneView对象.SceneView是对scene和view类的封装,通过他可以方便 ...