spring怎样使用命名空间p简化bean的配置

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring怎样使用命名空间p简化bean的配置

  2021-04-02 我要评论

这篇文章主要介绍了spring如何使用命名空间p简化bean的配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一般情况下,我们是这么配置bean的:

<bean id="car1" class="com.gong.spring.beans.Car">
    <property name="name" value="baoma"></property>
  </bean>
  <bean id="car2" class="com.gong.spring.beans.Car">
    <property name="name" value="benchi"></property>
  </bean>
  <bean id="car3" class="com.gong.spring.beans.Car">
    <property name="name" value="binli"></property>
  </bean>
  
  <bean id="student" class="com.gong.spring.beans.Student">
    <property name="name" value="tom"></property>
    <property name="age" value="12"></property>
    <property name="score" value="98.00"></property>
    <property name="cars" ref="cars">
    </property>
    
  </bean>
  
  <util:list id="cars">
    <ref bean="car1"/>
    <ref bean="car2"/>
    <ref bean="car3"/>
  </util:list>

说明:cars是公用的集合Bean,Student里有name、age、score以及类型为List<Car>的car属性。

在引入了命名空间之后,我们就可以这么进行配置了:

<bean id="car1" class="com.gong.spring.beans.Car" p:name="baoma"></bean>
  <bean id="car2" class="com.gong.spring.beans.Car" p:name="benchi"></bean>
  <bean id="car3" class="com.gong.spring.beans.Car" p:name="binli"></bean>
  
  <bean id="student" class="com.gong.spring.beans.Student"
  p:name="tom" p:age="12" p:score="98.00" p:cars-ref="cars"></bean>
  
  <util:list id="cars">
    <ref bean="car1"/>
    <ref bean="car2"/>
    <ref bean="car3"/>
  </util:list>

相较于原来的,代码简洁了很多。

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们