参考文章: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.