一.前言

  1.  <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/>

  1.  

二.bean的作用域

  

三.如何给集合对象注入值(map,list,set,数组)?

  1.list和数组类似:

  1. <property name="namelist">
  2. <list>
  3. <value>北京</value>
  4. <value>上海</value>
  5. <value>天津</value>
  6. </list>
  7. </property>
  8.      <!--value元素改成ref即可引用其他bean对象 -->
  9.    <property name="userlist">
  10. <list>
  11. <ref bean="user1"/>
  12. <ref bean="user2"/>
  13. </list>
  14.    </property>

  

  java测试遍历代码:

  1.     //遍历list<String>
  2. for(String s:cu.getNamelist()){
  3. System.out.println(s);
  4. }
  5. //遍历list<User>
  6. System.out.println();
  7. for(User user:cu.getUserlist()){
  8. System.out.println(user.getId()+user.getName());
  9. System.out.println(user);
  10. }

  2.配置set,set无序不重复,配置代码beans:

  1.      <property name="userset">
  2. <set>
  3. <ref bean="user1"/>
  4. <ref bean="user2"/>
  5. </set>
  6. </property>

  

  java测试遍历代码:

  1. //遍历set
  2. for(User user:cu.getUserset()){
  3. System.out.println("set"+user.getId()+user.getName());
  4. }

  3.配置map,

  1. <property name="usermaps">
  2. <map>
  3. <entry key="111" value-ref="user1"/>
  4. <entry key="222" value-ref="user2"/>
  5. </map>
  6. </property>

  

  java测试遍历代码:

  1. //遍历map,方式一
  2. Map<String,User> map=cu.getUsermaps();
  3. for(String key:map.keySet()){
  4. System.out.println(key+map.get(key)+"user:"+map.get(key).getId());
  5. }
  6. //遍历map,方式二(使用迭代器)
  7. Iterator<Map.Entry<String,User>> it=map.entrySet().iterator();
  8. while(it.hasNext()){
  9. Map.Entry<String, User> entry=it.next();
  10. System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
  11. }
  12.  
  13. //遍历map,方式三,适合数据量特别大的时候
  14. for(Map.Entry<String, User> entry:map.entrySet()){
  15. System.out.println(entry.getKey()+entry.getValue());
  16. }
  17.  
  18. //遍历map,方式四,只能遍历value,不能遍历key
  19. for(User user:map.values()){
  20. System.out.println(user);
  21. }

  4.给属性集合配值:

  1.      <property name="userpro">
  2. <props>
  3. <prop key="user">password</prop>
  4. <prop key="user1">password1</prop>
  5. </props>
  6. </property>

  

  测试代码

  1.      Properties prop=cu.getUserpro();
  2. Enumeration<Object> keys = prop.keys();
  3. while(keys.hasMoreElements()){
  4. String key=keys.nextElement().toString();
  5. System.out.println(key+" "+prop.getProperty(key));
  6. }

  

四.内部bean,

  内部bean不能被其他对象所引用,只能供父bean使用

  1.   <bean id="user1" class="com.ahd.domain.User">
  2. <property name="name">
  3. <bean id="user3" class="com.ahd.domain.Use">
  4. <property name="id" value="2" />
  5. <property name="name" value="李四" />
  6. </bean>
  7. </property>
  8. </bean>

五.继承 

  1. <bean id="check3" parent="validateUser" class="com.ahd.serviceImpl.CheckUser3">
  2. <property name="phone">
  3. <value>13548965896</value>
  4. </property>
  5. </bean>

  

  

  1.   CheckUser3 类.
  1. package com.ahd.serviceImpl;
  2.  
  3. import com.ahd.service.ValidateUser;
  4.  
  5. public class CheckUser3 extends CheckUser1{
  6. private String phone;
  7. public CheckUser3(){
  8.  
  9. }
  10. public String getPhone() {
  11. return phone;
  12. }
  13. public void setPhone(String phone) {
  14. this.phone = phone;
  15. }
  16.  
  17. }

spring_05装配bean的更多相关文章

  1. [spring]03_装配Bean

    3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...

  2. Spring学习系列(三) 通过Java代码装配Bean

    上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...

  3. 装配bean

    spring有三种装配bean的方式:隐式装配.java代码装配.xml装配 隐式装配最为省事方便,也称为自动化装配 这三种装配方式可以混搭着来用 在这里通过一个例子来讲述配置 CD的两个实现,一个是 ...

  4. IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注解 ...

  5. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  6. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  7. Spring常用注解,自动扫描装配Bean

    1 引入context命名空间(在Spring的配置文件中),配置文件如下: xmlns:context="http://www.springframework.org/schema/con ...

  8. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

  9. 自动装配Bean

    Spring提供了几种技巧,可以减少XML的配置数量: 自动装配(autowiring):可以减少<property>(setter注入)和<constructor-arg>( ...

随机推荐

  1. eclipse maven install 报错 jdk rather than jre?

    解决方案:window => Perference => java => install jre => 选中jre => edit => 选择文件夹为jdk的

  2. iproute2学习笔记

    一.替代arp, ifconfig, route等命令 显示网卡和IP地址 root@openstack:~# ip link list 1: lo: <LOOPBACK,UP,LOWER_UP ...

  3. Batch_Size 详解

    Batch_Size(批尺寸)是机器学习中一个重要参数,涉及诸多矛盾,下面逐一展开. 首先,为什么需要有 Batch_Size 这个参数? Batch 的选择,首先决定的是下降的方向.如果数据集比较小 ...

  4. Spring Boot 2 - 使用CommandLineRunner与ApplicationRunner

    本篇文章我们将探讨CommandLineRunner和ApplicationRunner的使用. 在阅读本篇文章之前,你可以新建一个工程,写一些关于本篇内容代码,这样会加深你对本文内容的理解,关于如何 ...

  5. 前端切图神器-cutterman

    之前我写过一篇关于前端切图的博客:https://www.cnblogs.com/tu-0718/p/9741058.html 但上面的方法在切图量大时依然很费时间,下面向大家推荐这款免费切图神器 c ...

  6. Mybatis框架五:动态SQL

    1.if   where 实现一个简单的需求: 根据性别和名字查询用户: 正常来写: <select id="selectUserBySexAndUsername" para ...

  7. 空手套白狼,硬阅java字节码class文件

    如下,是一些java字节码也就是原始的class文件,当应用部署到线上之后,我们能够看到的也就是这样的字样了.那么怎样解呢?就让我们一起,来解读解读字节码吧! Offset A B C D E F C ...

  8. pocketsphinx 移植问题解决备案

    一 编译问题: 1 _continuous: error while loading shared libraries: libpocketsphinx.so.3: cannot open share ...

  9. 知识扩展——Git和GitHub的区别

    一直以为Git和GitHub是一个东西,直到我看到这个解释.... 转载自:git与github区别与简介 一开始接触git或是github的程序员可能搞不太清楚这些名词到底指代的是什么,所以在这里稍 ...

  10. Docker简介以及操作

    Docker 简介 Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Linu ...