Spring 常用的类

Spring bean的生命周期

Spring 常用的类_第1张图片

BeanPostProcessor

如果BeanFactory装配了BeanPostProcessor后置处理器。可以在bean初始化完成后或者初始化之前对bean进行重新处理。

public Object postProcessAfterInitialization(Object obj, String beanName) throws BeansException {
                if (beanName.equals("myAnnoService")) {
                    Annotation[]  annos = obj.getClass().getDeclaredAnnotations();
                    for (Annotation anno:annos) {
                        System.out.println("hello world " + anno.annotationType().getName());
                    }
                }
                return obj;
            }

BeanFactoryAware

如果bean实现了BeanFactoryAware接口,则将调用setBeanFactory方法,将BeanFactory容器实例设置到bean中。

@Component
public class BeanNameAwareTest implements BeanFactoryAware {

    private BeanFactory factory;
    public void setBeanFactory(BeanFactory arg0) throws BeansException {
        // TODO Auto-generated method stub
        factory = arg0;
        MyAnnoService service = factory.getBean(MyAnnoService.class);
        System.out.println("factory inint ok");
        service.test();
    }

}

你可能感兴趣的:(spring)