spring bean注入方式

1.spring注入方式

1.1属性注入

即通过setter()方法注入bean的属性值或者依赖的对象,属性注入使用<property>,使name属性值对应bean

的属性名称,value属性或者<value>子节点指定属性值。

 <bean id="hello" class="it.com.cn.spring.HelloWord">
     <property name="name" value="张无忌"></property>
 </bean>

1.2构造器注入

即通过构造方法注入bean的属性值或者依赖的对象,可以保证bean实例化后可以使用,使用<constructor-arg></constructor-arg>标签中声明属性

 <!--通过构造方法注入-->
     <bean id="car" class="it.com.cn.spring.Car">
         <constructor-arg value="奥迪"></constructor-arg>
         <constructor-arg value="红色"></constructor-arg>
         <constructor-arg value="2000"></constructor-arg>
     </bean>
 <bean id="car" class="it.com.cn.spring.Car">
         <constructor-arg value="奥迪"></constructor-arg>
        <!-- <constructor-arg value="红色"></constructor-arg>-->
         <constructor-arg type="java.lang.String">
             <!--<![CDATA[]]>可以包含特殊符号-->
             <value><![CDATA[<红色>]]></value>
         </constructor-arg>
         <constructor-arg >
             <value>20000</value>
         </constructor-arg>
   </bean>

一个bean中引入其他bean

 <!--bean中引入另一bean-->
     <bean id="person" class="it.com.cn.spring.Person">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
         <property name="car" ref="car"></property>
     </bean>

内部bean

 <!--内部bean,不能被外部引用-->
     <bean id="person2" class="it.com.cn.spring.Person">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
         <property name="car">
             <bean class="it.com.cn.spring.Car">
                 <constructor-arg value="ford"></constructor-arg>
                 <constructor-arg value="蓝色"></constructor-arg>
                 <constructor-arg value="15200"></constructor-arg>
             </bean>
         </property>
     </bean>

null值标记

  <constructor-arg><null></constructor-arg>

为级联属性赋值

必须先将对象初始化,否则会抛异常

 <property name="person.age" value="18"></property>

集合

  <!--集合-->
     <bean id="person3" class="it.com.cn.spring.Person">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
         <property name="cars">
             <list>
                 <ref bean="car"></ref>
                 <ref bean="car2"></ref>
                 <ref bean="car3"></ref>
             </list>
         </property>
     </bean>

map

 <!--map-->
     <bean id="person4" class="it.com.cn.spring.Person">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
         <property name="cars">
             <list>
                 <ref bean="car"></ref>
                 <!--<ref bean="car2"></ref>
                 <ref bean="car3"></ref>-->
             </list>
         </property>
         <property name="map">
             <map>
                 <entry key="张无忌" value="赵敏"></entry>
                 <entry key="杨过" value="小龙女"></entry>
                 <entry key="周芷若" value="张无忌"></entry>
             </map>
         </property>
     </bean>

配置properties

  <!--配置properties-->
     <bean id="dataSource" class="it.com.cn.spring.DataSource">
         <property name="properties">
             <props>
                 <prop key="user">root</prop>
                 <prop key="password">root</prop>
                 <prop key="jdbcUrl">jdbc:mysql:///test</prop>
                 <prop key="driverClass">com.mysql.jdbc.Driver</prop>
             </props>
         </property>
     </bean>

配置单个集合bean,以供其他bean使用

需要引入约束util

 xmlns:util="http://www.springframework.org/schema/util"
  <!--配置单个集合bean,以供其他bean使用-->
     <util:list id="list">
         <ref bean="car"></ref>
     </util:list>
     <bean id="person5" class="it.com.cn.spring.Person">
         <property name="name" value="tom"></property>
         <property name="age" value="18"></property>
         <property name="cars" ref="list"></property>
     </bean>

p约束简化bean配置

 xmlns:p="http://www.springframework.org/schema/p"
  <bean id="person6" class="it.com.cn.spring.Person"
     p:name="张无忌" p:age="18" p:cars-ref="list">
  </bean>

spEl表达式 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值