Day4 基于XML的Spring应用

总结

  • java依赖注入的方式

  • set方法注入

  • List、map和properties的注入

  • 通过构造方法注入

  • ref是reference的缩写,需要引用其他bean的id,value用于注入普通属性值。

  • 自定义标签和其他标签的引用

  • 自定义标签

  • beans

  • bean

  • import

  • alias

  • 其他标签用于引用其他命名空间

1 bean的依赖注入方式

  1. 通过set方法注入

  1. 通过构造方法注入。

其中,ref是reference的缩写,需要引用其他bean的id,value用于注入普通属性值。

依赖注入的数据类型有如下三种

  • 普通数据类型,例如: String、int、boolean等,通过value属性指定引用数据类型,

  • 例如: UserDaolmpl、DataSource等,通过ref属性指定集合数据类型,

  • 例如: List、Map、Properties

1.1针对List集合

前提有属性和set方法。首先要确定是哪一个对象的list因此需要有bean,然后配置property,然后通过name定义属性,最后用list标签表示类型,使用value标签传入普通属性值 或者通过ref/bean标签传入引用属性值。

如果配置set 进把list标签改成set标签即可。

bean id="userService" class="cn.msf.service.impl.UserServiceImpl">
      <property name="stringList">
         <list>
            <value>张三</value>
            <value>李四</value>
            <value>王五</value>
         </list>
      </property>
      <property name="userDaoList">
         <list>
            <ref bean="userDao"></ref>
            <ref bean="userDao1"></ref>
            <ref bean="userDao2"></ref>
         </list>
      </property>
   </bean>
   <bean id="userDao" class="cn.msf.dao.impl.UserDaoImpl"></bean>
   <bean id="userDao1" class="cn.msf.dao.impl.UserDaoImpl"></bean>
   <bean id="userDao2" class="cn.msf.dao.impl.UserDaoImpl"></bean>

1.2针对Map集合

前提有属性和set方法。首先要确定是哪一个对象的map因此需要有bean,然后配置property,然后通过name定义属性,最后用map标签表示类型,然后使用entry标签,利用key、value、key-ref或者value-ref进行配置。

<property name="userDaoMap">
         <map>
            <entry key="a" value-ref="userDao"></entry>
            <entry key="b" value-ref="userDao1"></entry>
            <entry key="c" value-ref="userDao2"></entry>
         </map>
      </property>

1.3针对Properties集合

前提有属性和set方法。首先要确定是哪一个对象的properties因此需要有bean,然后配置property,然后通过name定义属性,最后用props标签表示类型,然后使用prpp标签,利用key进行配置。

<property name="properties">
         <props>
            <prop key="p1">ppp1</prop>
            <prop key="p2">ppp2</prop>
            <prop key="p3">ppp3</prop>
         </props>
      </property>

2 Spring 的其他配置标签

Spring的xml标签大体上分为两类,一种是默认标签,一种是自定义标签

默认标签:就是不用额外导入其他命名空间约束的标签,例如 <bean>标签

自定义标签:就是需要额外引入其他命名空间约束,并通过前缀引用的标签,例如<context:property-placeholder/> 标签

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
">

2.1 beans 标签

需要进行激活

//指定环境
System. setProperty("spring.profiles.active","dev");

2.2 import 标签

<import>标签,用于导入其他配置文件,项目变大后,就会导致一个配置文件内容过多,可以将一个配置文件根据业务某块进行拆分,拆分后,最终通过<import>标签导入到一个主配置文件中,项目加载主配置文件就连同<import>导入的文件一并加载了。

2.3 Alias 标签

用于起别名。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兜兜转转m

一毛钱助力博主实现愿望

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值