一,配置合作者的Bean

Bean设置的属性值是容器中的另一个Bean实力,使用<ref.../>元素,可制定一个bean属性,该属性用于指定容器中其他Bean实例的id属性

 <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean>

也可以简写成

 <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean>

二,使用自动装配注入合作者的Bean

1,自动装配可以通<beans.../>的default-autowire指定,该元素对所有的<bean.../>起作用

2,也可以通过<bean>的autowire指定

default-autowire,autowire可接受如下属性值

no:默认此

byName

  <!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean>

setter方法名为setGunDog(),spring容器会找id为gunDog的Bean,若找到了,该Bean就会调用setGunDog方法的参数

     public void setGunDog(Dog dog){
this.dog=dog;
}

byType:根据参数类型,进行匹配,不多做例子,估计用不到

三,注入集合值

使用集合元素标签<list../><set../><map../><props../>

 <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- 资源国际化 -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<!-- 驱动spring调用messageSource Bean的setBasenames()的方法,改方法需要一个参数组 -->
<property name="basenames">
<list>
<value>message</value>
<!-- 如果有多个资源文件,全部列在这儿 -->
</list>
</property>
</bean>
<bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<property name="axe">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<ref bean="steelAxe"/>
</property>
</bean> <bean id="steelAxe" class="org.com.service.impl.SteelAxe"></bean>
<bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 指定使用个容器中的id为steelAxe的Bean作为调用setAxe()方法的参数 -->
<property name="axe" ref="steelAxe"/>
</bean> <!-- 指定使用byName,根据setter的方法名与Bean的id进行匹配 -->
<bean id="chinese" class="org.com.service.impl.Chinese" autowire="byName">
</bean>
<bean id="gunDog" class="org.com.service.impl.GunDog">
<property name="name" value="wanggang"></property>
</bean> <bean id="chinese" class="org.com.service.impl.Chinese">
<!-- 为调用setSchools()方法配置List集合作为参数值 和数组一样 -->
<property name="schools">
<list>
<!-- 每个value,ref,bean..都配置一个List元素 -->
<value>小学</value>
<value>中学</value>
<value>大学</value>
</list>
</property>
<property name="scores">
<!-- 为调用setScores()方法配置Map集合作为参数值 -->
<map>
<entry key="语文" value="80"/>
<entry key="数学" value="80"/>
<entry key="外语" value="80"/>
</map>
</property>
</bean>
</beans>

四,组合属性
除了最后一个属性可以为NULL,其他都不行

<bean id="" class="">

<property name="person.name" value="zhangliang"/>相当于执行getPerson().setName(String name);

</bean>

Spring容器中的Bean的更多相关文章

  1. 从Spring容器中获取Bean。ApplicationContextAware

    引言:我们从几个方面有逻辑的讲述如何从Spring容器中获取Bean.(新手勿喷) 1.我们的目的是什么? 2.方法是什么(可变的细节)? 3.方法的原理是什么(不变的本质)? 1.我们的目的是什么? ...

  2. 在listener或者工具中使用spring容器中的bean实例

    在项目中经常遇见需要在Listener中或者工具中使用Spring容器中的bean实例,由于bean不能在stataic的类中使用. 介绍一种方式: public class SpringTool { ...

  3. 7 -- Spring的基本用法 -- 5... Spring容器中的Bean;容器中Bean的作用域;配置依赖;

    7.5 Spring容器中的Bean 7.5.1 Bean的基本定义和Bean别名 <beans.../>元素是Spring配置文件的根元素,该元素可以指定如下属性: default-la ...

  4. 7 -- Spring的基本用法 -- 4... 使用 Spring 容器:Spring 容器BeanFactory、ApplicationContext;ApplicationContext 的国际化支持;ApplicationContext 的事件机制;让Bean获取Spring容器;Spring容器中的Bean

    7.4 使用 Spring 容器 Spring 有两个核心接口:BeanFactory 和 ApplicationContext,其中ApplicationContext 是 BeanFactory ...

  5. SpringBoot 之 普通类获取Spring容器中的bean

    [十]SpringBoot 之 普通类获取Spring容器中的bean   我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...

  6. 获取Spring容器中的Bean协助调试

    在使用Spring进行开发时,有时调bug真的是很伤脑筋的一件事,我们可以通过自定义一个监听器来获取Spring容器中的Bean实例来协助我们调试. 第一步:编写自定义监听器 /** * 监听serv ...

  7. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  8. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

  9. 获取Spring容器中的Bean

    摘要 SpringMVC框架开发中可能会在Filter或Servlet中用到spring容器中注册的java bean 对象,获得容器中的java bean对象有如下方法 Spring中的Applic ...

随机推荐

  1. mssql全文索引

    在使用全文索引的时候例如: SELECT [PRID] ,[PRCode] ,[PRDesc] FROM [test1].[dbo].[PerformanceIssue] where contains ...

  2. java调用Http请求 -HttpURLConnection学习

    最常用的Http请求无非是get和post,get请求可以获取静态页面,也可以把参数放在URL字串后面,传递给servlet,post与get的不同之处在于post的参数不是放在URL字串里面,而是放 ...

  3. 玩儿了一下django User authentication

    五一在家,VPN不能链接了,而项目在本地run的过程中,又需要链接公司的SSO server才能login.下雨,不想去公司,又不得不在家做task,只能想办法避开SSO login,以前知道djan ...

  4. How to Move Magento to a New Server or Domain

    Magento is one of the fastest growing eCommerce platforms on the internet and with its recent acquis ...

  5. jQuery动态对表格Table进行添加或删除行以及修改列值操作

    jQuery,不仅可以以少量的代码做很多操作,而且兼容性好(各种浏览器,各种版本). 下面用jQuery动态对表格Table进行添加或删除行以及修改列值操作 1.jQuery代码 <script ...

  6. Interview Check If n Is A Perfect Square

    Check if a given number is a perfect square with only addition or substraction operation. eg. 25 ret ...

  7. opencv hog+svm行人检测

    http://blog.csdn.net/masibuaa/article/details/16105073 http://blog.csdn.net/u011263315/article/detai ...

  8. C#资源释放

    转自:http://www.cnblogs.com/psunny/archive/2009/07/07/1518812.html 深刻理解C#中资源释放 今天我的一个朋友看到我写的那篇<C#中用 ...

  9. Linux下获取公网IP地址

    curl http://members.3322.org/dyndns/getipcurl http://ip.6655.com/ip.aspx

  10. 微信支付开发(2) 静态链接Native支付

    关键字:微信支付 微信支付v3 native支付 统一支付 Native支付 prepay_id 作者:方倍工作室原文: http://www.cnblogs.com/txw1958/p/wxpayv ...