Comment From: snicoll

courtesy of Google translate:

When using an XML configuration file, if only one Bean is currently configured, the two methods of implementing BeanPostProcessor and BeanFactoryPostProcessor BeanPostProcessor will not be executed, but if another Service method is added, it will be executed.

That sounds like a question that should be better suited to StackOverflow but I can't say for sure. Can you please attach a small sample that demonstrates what you've described?

Comment From: ZiYangZhou

傻逼请用英语

Comment From: Deycoesr

courtesy of Google translate:

When using an XML configuration file, if only one Bean is currently configured, the two methods of implementing BeanPostProcessor and BeanFactoryPostProcessor BeanPostProcessor will not be executed, but if another Service method is added, it will be executed.

That sounds like a question that should be better suited to StackOverflow but I can't say for sure. Can you please attach a small sample that demonstrates what you've described?

I think he means. When only single Bean is registered, BeanPostProcessor doesn't work.

Like this:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class TestBean implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TestBean.postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("TestBean.postProcessAfterInitialization");
        return bean;
    }

    public static void main(String[] args) {
        new AnnotationConfigApplicationContext(TestBean.class);
    }
}

Console output:

10:51:44.741 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@574caa3f
10:51:44.749 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
10:51:44.761 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerProcessor'
10:51:44.761 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.event.internalEventListenerFactory'
10:51:44.762 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
10:51:44.763 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
10:51:44.765 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'testBean'

Not output TestBean.postProcessBeforeInitialization and TestBean.postProcessAfterInitialization.

Comment From: 10zh

like this:

Start by defining two services:

package org.springframework.core.ioc.service.intf;

/**
 * @author pzh
 * createTime:2022/03/11/11:10
 */
public interface IOCService {
    String testIoc();
}


package org.springframework.core.ioc.service.intf;

/**
 * @author pzh
 * createTime:2022/03/11/16:23
 */
public interface IOCServiceTwo {
    String testIocTwo();
}

The implementation class:

package org.springframework.core.ioc.service.impl;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.ioc.service.intf.IOCService;

/**
 * @author pzh
 * createTime:2022/03/11/11:11
 */
public class IOCServiceImpl implements IOCService, BeanFactoryPostProcessor,
      ApplicationContextAware, BeanPostProcessor, InitializingBean {

    public IOCServiceImpl(){
        System.out.println("one con");
    }

   @Override
   public String testIoc() {
      return "One Test";
   }

   @Override
   public Object postProcessBeforeInitialization(Object bean, String beanName)
         throws BeansException {
      System.out.println("One postProcessBeforeInitialization");
      return bean;
   }

   @Override
   public Object postProcessAfterInitialization(Object bean, String beanName)
         throws BeansException {
      System.out.println("One postProcessAfterInitialization");
      return bean;
   }

   @Override
   public void postProcessBeanFactory(
         ConfigurableListableBeanFactory beanFactory) throws BeansException {
      //可以执行初始化前的一些逻辑
      System.out.println("one postProcessBeanFactory");
   }

   @Override
   public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
      System.out.println("one setApplicationContext:" + applicationContext.getApplicationName());
   }


   @Override
   public void afterPropertiesSet() {
      System.out.println("one afterPropertiesSet");
   }
}



package org.springframework.core.ioc.service.impl;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.core.ioc.service.intf.IOCServiceTwo;

/**
 * @author pzh
 * createTime:2022/03/11/16:23
 */
public class IOCServiceTwoImpl implements IOCServiceTwo, BeanPostProcessor,
        BeanFactoryPostProcessor {

    public IOCServiceTwoImpl(){
        System.out.println("two con");
    }

    @Override
    public String testIocTwo() {
        return "Two ioc";
    }

    @Override
    public void postProcessBeanFactory(
            ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("Two postProcessBeanFactory");
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Two postProcessBeforeInitialization");
        return BeanPostProcessor.super.postProcessBeforeInitialization(bean, beanName);
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("Two postProcessAfterInitialization");
        return BeanPostProcessor.super.postProcessAfterInitialization(bean, beanName);
    }
}

Test file:

@Test
public void testIocXml() {
   ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
         "classpath:application-ioc.xml");
   IOCService iocService = applicationContext.getBean(IOCService.class);
   IOCServiceTwo iocServiceTwo = applicationContext.getBean(IOCServiceTwo.class);
   System.out.println(iocService.testIoc());
   System.out.println(iocServiceTwo.testIocTwo());
}

The configuration file(application-ioc.xml):

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.springframework.org/schema/beans"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
      default-autowire="byName">
   <bean id="iocService" class="org.springframework.core.ioc.service.impl.IOCServiceImpl"/>
   <bean id="iocServiceTwo" class="org.springframework.core.ioc.service.impl.IOCServiceTwoImpl"/>
</beans>

output:

one con
one setApplicationContext:
one afterPropertiesSet
two con
one postProcessBeanFactory
Two postProcessBeanFactory
One Test
Two ioc

conclusion:

Implementing BeanFactoryPostProcessor will cause the Bean to be pre-initialized. When all beans implement BeanFactoryPostProcessor, the BeanPostProcessor will fail

Comment From: snicoll

@Deycoesr let's assume that's what the OP meant as this is pretty close to my understanding. The Javadoc of BeanPostProcessor states:

An ApplicationContext can autodetect BeanPostProcessor beans in its bean definitions and apply those post-processors to any beans subsequently created.

It's not a matter of one bean vs. several bean but rather that the only bean around is the BPP.