spring bean 注入
概念
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 注入的更多相关文章
- [spring]Bean注入——在XML中配置
Bean注入的方式有两种: 一.在XML中配置 属性注入 构造函数注入 工厂方法注入 二.使用注解的方式注入@Autowired,@Resource,@Required 本文首先讲解在XML中配置的注 ...
- Spring bean注入方式
版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5619525.html Spring bean提供了3中注入方式:属 ...
- spring+cxf 开发webService(主要是记录遇到spring bean注入不进来的解决方法)
这里不介绍原理,只是记录自己spring+cxf的开发过程和遇到的问题 场景:第三方公司需要调用我们的业务系统,以xml报文的形式传递数据,之后我们解析报文存储到我们数据库生成业务单据: WebSer ...
- Spring Bean 注入 2 注解篇
1. 自动装配注解 配置applicationContext.xml开启注解 <?xml version="1.0" encoding="UTF-8"?& ...
- Spring Bean 注入 1 - 构造方法注入,属性注入,自动装配
1.代码结构图 xxx 2.bean代码 package com.xxx.bean; /** * Created with IntelliJ IDEA. * User: zhenwei.liu * D ...
- [spring]Bean注入——使用注解代替xml配置
使用注解编程,主要是为了替代xml文件,使开发更加快速. 一.使用注解前提: <?xml version="1.0" encoding="UTF-8"?& ...
- Spring bean注入
1.构造方法注入 1.bean类 public class User { private String name; private Integer age; private Cat cat; publ ...
- Spring JMX之一:使用JMX管理Spring Bean
spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...
- 在非spring组件中注入spring bean
1.在spring中配置如下<context:spring-configured/> <context:load-time-weaver aspectj-weaving=&q ...
随机推荐
- hadoop更改保存路径后,批量重启不能启动datanode(或者format以后不能启动datanode)
这是因为更改文件后,所有的子节点的core-site.xml并没有一致,所以使用start-dfs.sh的时候导致机器起不起来. 修改slave(datanode)节点的core-site.xml就可 ...
- webpack打包优化
https://www.cnblogs.com/vvjiang/p/9327903.html
- Django继承AbstractUser新建UserInfor Model时出现fields.E304错误
错误详情: SystemCheckError: System check identified some issues: ERRORS:app01.UserInfo.groups: (fields.E ...
- FM(Factorization Machines)模型详解
优点 FM模型可以在非常稀疏的数据中进行合理的参数估计,而SVM做不到这点 在FM模型的复杂度是线性的,优化效果很好,而且不需要像SVM一样依赖于支持向量. FM是一个通用模型,它可以用于任何特征为实 ...
- python学习中遇到的错误及解决办法
1. nodename nor servname provided 原因:Python程序中有段程序调用 socket.gethostbyname(socket.gethostname()) sock ...
- java进阶系列之装饰器模式
1.介绍 装饰器模式顾名思义就是装饰某个对象的,是一种结构型模式.装饰器模式允许向一个现有对象添加新的功能,同时不改变其结构,用户可以随意的扩展原有的对象.它是作为现有的类的一个包装.装饰器模式一方面 ...
- 1、背景介绍及移动云MAS平台 --短信平台
目的: 刚开发完成一套短信平台以及一个Web端短信发送系统,短信平台耗时两个周.短信发送系统耗时两个多月,开发使用的技术没什么高科技含量,在此主要是记录下很多情况的处理方案,希望能让大家提出改善方案和 ...
- 涂色(CQOI2007)
--BZOJ1260_区间dp Description 假设你有一条长度为5的木版,初始时没有涂过任何颜色.你希望把它的5个单位长度分别涂上红.绿.蓝.绿.红色,用一个长度为5的字符串表示这个目标:R ...
- 解决Kettle与Kerberos集成问题
本文目的:记录Kerberos环境下,通过Kettle将MySQL数据清洗到HDFS过程解决的2个问题,希望对大家有所帮助. Kettle版本:pdi-ce-7.1.0.0-12 1.在Kerbero ...
- 惠普笔记本fn键
fn+shift+f10 看到fn上的小灯亮了就可以了