IOC 容器中Bean 注入

来源:互联网 发布:淘宝店怎么刷流量群 编辑:程序博客网 时间:2024/05/24 00:53

spring 容器包含(Bean定义注册表,Bean缓存池)
spring读取Bean 配置信息-根据Bean定义注册表生成Bean实现类-将实现类放入Bean缓存池-应用程序使用Bean。

支持包括:xml配置,注解,Java类和Groovy

Bean 需要一个id,和实现类。id唯一,name 不唯一取值为后面那一个。一个bean可以有多个id或者name.逗号分隔。什么属性都没有的用全限定名字
建议使用Id.

Bean 依赖注入:

<!--需要有默认构造函数,和属性的setter方法--><property name = "price"><value>2000</value></property><!--需要带参的构造函数,可能有多个,参数顺序不能辨别是那个,所以使用type 标明--><constructor-arg type ="java.lang.String"><value>红旗</value></constructor-arg><!--type 不行的时候可以使用index,二者可以联合使用,否则可能随机选择一个--><constructor-arg index = "0"><value>红旗</value></constructor-arg><!--参数是一个引用类型的对象时,注意互相引用导致死锁的问题--><ref bean = "car"><!-- 非静态工厂方法,car3,是通过createHongqicar方法产生的--><bean id = "carFactory" class="com.smart.carFactory"><bean id = "car3" factory-bean="carFactory" factory-method ="createHongqicar"/><!--静态工厂--><bean id = "car4" class ="com.smart.carFactory" factory-method = "createCar"/>

注入参数详解:

<!--字面值--><value><!--引用其他Bean,同一容器或者父容器--><ref bean ="car"></ref><!--表明是父容器,当父子容器有同id的bean使用这种方法区别--><ref parent ="car"></ref><!--内部bean--><property name = "car">    <bean class ="com.smart.car">        <property name ="price" value ="23"/>    </bean></property><!-- null 值--><property name ="price" ><null/></property><!--级联属性,Car不能为空,必须new -->class Boss{    Car car = new Car();    public setCar(){    this.car = car;}}<bean id = "boss" class = "com.smart.Boos"><property name = "car.brand" value = "吉利汽车"/></bean><!--List--><property name = "favorites">    <list>        <value>看书</value>        <value>赛车</value>    </list></property><!--Set--><property name = "favorites">    <set>        <value>看书</value>        <value>看报</value>    </set></property><!--Map--><property>    <map>        <entry>            <key><value>liuyang</valu></key>            <value>会见客户</value>        </entry>        <entry>        </entry>    <map></property><!-- properties--><property>    <props>        <prop key = "job"> this is <prop>        <prop key = "life"> test </prop>    </props></property><!--集合合并 merge = true ,最终set中有5个元素--><bean id = "childBoss" parent = "parentBoss"><property>    <set merge = "true">        <value>爬山</value>        <value>上课</value>    </set></property>

两种特殊的注入

<!-- lookup 方法注入,使用了CGLib的动态代理功能,想要通过一个singleton Bean 获取一个prototype Bean 的时候使用--><bean id = "magicBoss" class = "com.smart.MagicBoss">    <lookup-method name = "getCar" bean = "car"></bean><!-- 方法替换,实现了MethodReplacer接口,reimplement()方法,Boss1中的方法被替换为reimplement 方法--><bean id = "boss1" class = "com.smart.Boss1">    <replaced-method name = "getCar" replacer = "boss2"/></bean>

Bean之间的关系 -继承
Bean 就相当于一个对象的实例。xml 配置只是告诉spring如何实例化,当需要使用的时候就实例化。当两个实例的属性值重复的时候,就可以使用Bean的继承.子Bean重复配置的,就会覆盖父Bean的配置。

<bean id = ""abstractCar class = "com.smart.Car">    p:brand = "红旗" p:price ="999" p:color="黑色" abstract = "true"</bena><bean id = "car3" p:color = "白色" parent = "abstractCar"/>

依赖
某一个Bean需要另外一个Bean比自己先实例化。
depends-on

<bean id = "manager" class = "com.smart.CacheManager" depends-on = "beanid"></bean>

引用:使用一个Bean 的id名字对另外一个Bean的属性进行复制。

<bean id = "boss" class = "com.smart.Boss"><property>    <idref bean = "car"/></property></bean>
原创粉丝点击