spring:FactoryBean与BeanFactory的区别

 参考文章:https://www.cnblogs.com/redcool/p/6413461.html 作者:低调的小黑

BeanFactory:
  以Factory结尾,表示他是一个工厂类接口,用于管理Bean的一个工厂,在spring中BeanFactory是ioc容器的核心接口,
  它的职责包括:实例化,定位,配置应用程序中的对象及建立这些对象间的依赖。
FactoryBean:
  以Bean结尾,表示它是一个Bean。不用于其他Bean的是:它实现了FactoryBean接口的Bean,根据该Bean的ID从BeanFactory
  中获取的实际上是FactoryBean的getObject()返回的对象,而不是FactoryBean本身,如果需要获取FactoryBean对象,请
  在ID前面加一个&符号来获取。

 获取Bean与FactoryBean:

 1.Bean类:HelloService

public class HelloService {
    public String hello(){
        return "helloService:hello.hello";
    }
}

2.FactoryBean类:HelloFactoryBean

import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;

public class HelloFactoryBean implements FactoryBean {
    private HelloService helloService;
    public String pring(){
        return "我是HelloFactoryBean.";
    }
    @Nullable
    @Override
    public Object getObject() throws Exception {
        return helloService;
    }

    @Nullable
    @Override
    public Class<?> getObjectType() {
        return helloService.getClass();
    }

    public HelloService getHelloService() {
        return helloService;
    }

    public void setHelloService(HelloService helloService) {
        this.helloService = helloService;
    }
}

3.bean.xml(由BeanFactory管理注入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 http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="helloService" class="com.spring.HelloService" />
    <bean id="helloFactoryBean" class="com.spring.HelloFactoryBean">
        <property name="helloService" ref="helloService"></property>
    </bean>
</beans>

4.测试类(通过id获取对应的对象)

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class TestFactoryBean {
    public static void main(String[] args) {
        ClassPathResource resource = new ClassPathResource("bean.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        HelloService helloService = (HelloService) factory.getBean("helloService");
        HelloService helloFactoryBean = (HelloService) factory.getBean("helloFactoryBean");
        HelloFactoryBean helloFactoryBean1 = (HelloFactoryBean) factory.getBean("&helloFactoryBean");

        System.out.println("id:helloService:" + helloService.hello());
        System.out.println("id:helloFactoryBean:" + helloFactoryBean.hello());
        System.out.println("id:&helloFactoryBean:" + helloFactoryBean1.pring());
    }
}
打印信息:
11:30:54.291 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 2 bean definitions from class path resource [bean.xml]
11:30:54.296 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory - Creating shared instance of singleton bean 'helloService'
11:30:54.311 [main] DEBUG org.springframework.beans.factory.xml.XmlBeanFactory - Creating shared instance of singleton bean 'helloFactoryBean'
id:helloService:helloService:hello.hello
id:helloFactoryBean:helloService:hello.hello
id:&helloFactoryBean:我是HelloFactoryBean.

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值