spring_05装配bean
一.前言
<bean id="user1" scope="singleton" init-method="myInit" destroy-method="myDestory" class="com.ahd.domain.User">
1.id作用设置对象名,scope为作用域,class是相关bean的完整路径.
2.使用原型bean会对性能产生影响,尽量不要设置成prototype,除非有必要,尽量使用scope=”singleton”
3.init-method和destroy-method为定制方法,前者为定制初始化bean的方法,后者为定制销毁bean及其他需要关闭对象的方法
4.定制方法也可以通过注解的方式配置
5.不推荐使用接口,使用接口后bean和springAPI绑定了
6.如果要把bean的属性设置成空,可以设置成<null/>
二.bean的作用域

三.如何给集合对象注入值(map,list,set,数组)?
1.list和数组类似:
<property name="namelist">
<list>
<value>北京</value>
<value>上海</value>
<value>天津</value>
</list>
</property>
<!--value元素改成ref即可引用其他bean对象 -->
<property name="userlist">
<list>
<ref bean="user1"/>
<ref bean="user2"/>
</list>
</property>
java测试遍历代码:
//遍历list<String>
for(String s:cu.getNamelist()){
System.out.println(s);
}
//遍历list<User>
System.out.println();
for(User user:cu.getUserlist()){
System.out.println(user.getId()+user.getName());
System.out.println(user);
}
2.配置set,set无序不重复,配置代码beans:
<property name="userset">
<set>
<ref bean="user1"/>
<ref bean="user2"/>
</set>
</property>
java测试遍历代码:
//遍历set
for(User user:cu.getUserset()){
System.out.println("set"+user.getId()+user.getName());
}
3.配置map,
<property name="usermaps">
<map>
<entry key="111" value-ref="user1"/>
<entry key="222" value-ref="user2"/>
</map>
</property>
java测试遍历代码:
//遍历map,方式一
Map<String,User> map=cu.getUsermaps();
for(String key:map.keySet()){
System.out.println(key+map.get(key)+"user:"+map.get(key).getId());
}
//遍历map,方式二(使用迭代器)
Iterator<Map.Entry<String,User>> it=map.entrySet().iterator();
while(it.hasNext()){
Map.Entry<String, User> entry=it.next();
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
} //遍历map,方式三,适合数据量特别大的时候
for(Map.Entry<String, User> entry:map.entrySet()){
System.out.println(entry.getKey()+entry.getValue());
} //遍历map,方式四,只能遍历value,不能遍历key
for(User user:map.values()){
System.out.println(user);
}
4.给属性集合配值:
<property name="userpro">
<props>
<prop key="user">password</prop>
<prop key="user1">password1</prop>
</props>
</property>
测试代码
Properties prop=cu.getUserpro();
Enumeration<Object> keys = prop.keys();
while(keys.hasMoreElements()){
String key=keys.nextElement().toString();
System.out.println(key+" "+prop.getProperty(key));
}
四.内部bean,
内部bean不能被其他对象所引用,只能供父bean使用
<bean id="user1" class="com.ahd.domain.User">
<property name="name">
<bean id="user3" class="com.ahd.domain.Use">
<property name="id" value="2" />
<property name="name" value="李四" />
</bean>
</property>
</bean>
五.继承
<bean id="check3" parent="validateUser" class="com.ahd.serviceImpl.CheckUser3">
<property name="phone">
<value>13548965896</value>
</property>
</bean>
CheckUser3 类.
package com.ahd.serviceImpl;
import com.ahd.service.ValidateUser;
public class CheckUser3 extends CheckUser1{
private String phone;
public CheckUser3(){
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
spring_05装配bean的更多相关文章
- [spring]03_装配Bean
3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...
- Spring学习系列(三) 通过Java代码装配Bean
上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...
- 装配bean
spring有三种装配bean的方式:隐式装配.java代码装配.xml装配 隐式装配最为省事方便,也称为自动化装配 这三种装配方式可以混搭着来用 在这里通过一个例子来讲述配置 CD的两个实现,一个是 ...
- IOC装配Bean(注解方式)
Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- Spring常用注解,自动扫描装配Bean
1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...
- Spring学习笔记—装配Bean
在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...
- 自动装配Bean
Spring提供了几种技巧,可以减少XML的配置数量: 自动装配(autowiring):可以减少<property>(setter注入)和<constructor-arg>( ...
随机推荐
- bash基础特性3(shell编程)
Linux上文本处理三剑客: grep:文本过滤工具 sed:stream editor,文本编辑工具 awk:文本报告生成器 grep -v:显示不能够被pattern匹配到的行 -i:忽略字符大小 ...
- 12Js_原型对象
对象描述: 1. 每个对象中都有一个_proto_属性. JS世界中没有类(模具)的概念,对象是从另一个对象(原型)衍生出来的,所以每个对象中会有一个_proto_属性指向它的原型对象.(参考左上角的 ...
- Openvswitch手册(4): Mirror
这一节我们来分析Mirror Mirror就是配置一个bridge,将某些包发给指定的mirrored ports 对于包的选择: select_all,所有的包 select_dst_port se ...
- java初级笔记
1:java核心优势:跨平台,一次编译,四处运行,只要安装了对应的jvm虚拟机: 2:JVM其实就是一种规范,就是一个虚拟的用于执行bytecode字节码的计算机: 3:数据类型分为四类八种,整数型( ...
- Web前端JQuery入门实战案例
前端jquery入门到实战 为什么要学习Jquery?因为生活. 案例: <!DOCTYPE html> <html lang="zh-CN"> <h ...
- JVM和GC垃圾回收机制和内存分配
JVM运行期间 线程共享 线程私有 线程共享: 方法区 堆方法区:存放可以共享数据,静态常量,类的共有方法属性字段等,可以共享的存在方法区. 堆:存放class对象 . 线程私有:本地方法栈 虚拟机栈 ...
- Kubernetes 笔记 02 demo 初体验
本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. 从前面的文章我 ...
- LeetCode:1_Two_Sum | 两个元素相加等于目标元素 | Medium
题目: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- mongo 字段重命名
执行语句 db.getCollection("A表").updateMany( {}, { $rename: { "A": "A1"} } ...
- 项目总结一:情感分类项目(emojify)
一.Emojifier-V1 模型 1. 模型 (1)前向传播过程: (2)损失函数:计算the cross-entropy cost (3)反向传播过程:计算dW,db dz = a - Y_oh[ ...