@@ -241,4 +241,44 @@ public class NdBeanPostProcessor implements BeanPostProcessor {
241241}
242242```
243243
244- 4. ** Bean 销毁阶段** ,用户可以自定义destroyMethod()方法,在Bean 被销毁时被调用。
244+ 4. ** Bean 销毁阶段** ,用户可以自定义destroyMethod()方法,在Bean 被销毁时被调用。
245+
246+ ### BeanFactory 和FactoryBean 有什么区别?
247+
248+ BeanFactory 是一个接口,定义了IOC 容器的最基本的规范,并提供了IOC 容器应遵守的的最基本的方法。在Spring 代码中,BeanFactory 只是个接口,并不是IOC 容器的具体实现,但是Spring 容器给出了很多种实现,如 DefaultListableBeanFactory 、XmlBeanFactory 、ApplicationContext 等,都是附加了某种功能的实现。
249+
250+ ```java
251+ package org. springframework. beans. factory;
252+ import org.springframework.beans. BeansException ;
253+ public interface BeanFactory {
254+ String FACTORY_BEAN_PREFIX = " &" ;
255+ Object getBean (String name ) throws BeansException ;
256+ <T > T getBean (String name , Class<T > requiredType ) throws BeansException ;
257+ <T > T getBean (Class<T > requiredType ) throws BeansException ;
258+ Object getBean (String name , Object ... args ) throws BeansException ;
259+ boolean containsBean (String name );
260+ boolean isSingleton (String name ) throws NoSuchBeanDefinitionException ;
261+ boolean isPrototype (String name ) throws NoSuchBeanDefinitionException ;
262+ boolean isTypeMatch (String name , Class<?> targetType ) throws NoSuchBeanDefinitionException ;
263+ Class<?> getType (String name ) throws NoSuchBeanDefinitionException ;
264+ String [] getAliases (String name );
265+ }
266+ ```
267+
268+ FactoryBean 是一个接口,有一个创建bean对象的方法getObject(),当一些bean对象不能由ioc容器简单得调用类的构造器方法来创建实例对象时使用,可以将Bean 类实现FactoryBean 接口,实现getObject()方法,供ioc容器调用来创建bean对象。
269+
270+ ```java
271+ public interface FactoryBean <T> {
272+ @Nullable
273+ T getObject () throws Exception ;
274+
275+ @Nullable
276+ Class<?> getObjectType ();
277+
278+ default boolean isSingleton () {
279+ return true ;
280+ }
281+ }
282+ ```
283+
284+ https: // www.cnblogs.com/aspirant/p/9082858.html
0 commit comments