spring中beanfactory的控制反转和依赖注入小结。

1.创建接口

2.创建接口实现类

3.在beanfactory的xml文件中配置创建的接口实现类。

4.通过工厂得到类的实例化,下面是例子

package com.service.test;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;

public class bftest {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();
        XmlBeanDefinitionReader reader=new XmlBeanDefinitionReader(beanFactory);
        reader.loadBeanDefinitions("beans.xml");
        Object userService=beanFactory.getBean("userServiceimpl");
        //System.out.println(userService);
        Object userDao=beanFactory.getBean("userDaoimpl");
        //System.out.println(userDao);
    }
}

5.关于依赖注入即设置xml中bean的property元素来实现依赖注入,下面是例子

<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userServiceimpl" class="com.service.impl.UserServiceimpl">
        <property name="userDao" ref="userDaoimpl" ></property>
    </bean>
    <bean id="userDaoimpl" class="com.service.Dao"></bean>
</beans>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值