Spring04之基于XML方式的依赖注入

一级目录

二级目录

三级目录

1.Spring IOC基于xml文件的配置格式

此时这个XML配置文件就是IOC容器。
我们需要在其中配置所有我们需要的bean。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean id="hello" class="com.xinbao.pojo.HelloWord">
        <!-- collaborators and configuration for this bean go here -->
        <property name="name" value="lijiaxin"></property>
    </bean>

    <bean id="person" class="com.xinbao.pojo.Person">
        <property name="PersonDao" ref="hello"></property>
    </bean>
    
    <bean id = "student" class="com.xinbao.pojo.Student">
        <constructor-arg index="0" value="dongshi"></constructor-arg>
        <constructor-arg index="1" value="90"></constructor-arg>
    </bean>

</beans>

文件中一些属性的简单介绍

1.id属性:标识不同的bean,名字自己取,一般和pojo类名一样,开头字母小写。
2.class属性:该bean所属的类的全路径(原理是反射)
3.property属性:设置该bean中的属性。name是属性名,value是属性值。
4.ref属性:引用已经注册过得bean,值为引用的bean的id值。这种情况就是,一个类作为一个属性出现在另一个类中时。我们set该属性的值时,可以在spring的xml中通过ref属性来设置。
5.constructor-arg属性:选择带参的指定的的构造器。这里不再使用Set方式注入属性。
参数给属性注入值有三种方式:
1.下标赋值 index ;
2.类型赋值 type ;
3.参数名赋值 name;(掌握这个即可)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值