Unlock Spring Boot 3: Master MethodLocatingFactoryBean & MethodInvokingFactoryBean

This article introduces two powerful Spring Boot 3 classes—MethodLocatingFactoryBean and MethodInvokingFactoryBean—explains their purpose, shows step‑by‑step configuration and usage with code examples, and demonstrates how to invoke methods dynamically in a Spring application.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Unlock Spring Boot 3: Master MethodLocatingFactoryBean & MethodInvokingFactoryBean

Spring Boot 3 practical case collection has been updated to over 120 examples and is available as a free PDF ebook. Readers can subscribe to receive the ebook and future updates.

The tutorial focuses on two powerful Spring classes: MethodLocatingFactoryBean and MethodInvokingFactoryBean . Both are FactoryBean implementations that allow dynamic method lookup and invocation.

1. Environment

Spring Boot version 3.4.2 is used.

2. MethodLocatingFactoryBean (method lookup)

This class returns a java.lang.reflect.Method object based on a bean name and method name.

@Component("ps")
public class PersonService {
    public void save() {
        System.out.println("PersonService save method invoke...");
    }
    public void query(Integer id) {
        System.out.printf("PersonService query method id: %d%n", id);
    }
}
public class MethodLocatingFactoryBean implements FactoryBean<Method>, BeanFactoryAware {
    private String targetBeanName;
    private String methodName;
    private Method method;
    public Method getObject() throws Exception {
        return this.method;
    }
    public Class<Method> getObjectType() {
        return Method.class;
    }
    // ... other required methods
}
@Configuration
public class AppConfig {
    @Bean
    MethodLocatingFactoryBean methodLocatingFactoryBean() {
        MethodLocatingFactoryBean fb = new MethodLocatingFactoryBean();
        fb.setMethodName("save");
        fb.setTargetBeanName("ps");
        return fb;
    }
}

Inject the Method into another bean and invoke it:

@Component
public class BeanMethod {
    @Resource
    private Method method;
    @Resource
    private PersonService target;
    public Object invoke(Object... params) throws Exception {
        return method.invoke(target, params);
    }
}
@Resource
private BeanMethod bm;
@Test
public void testSave() {
    bm.invoke();
}
// Output: PersonService save method invoke...

3. Configuring Method Parameters

When the target method has parameters, the full qualified name must be used, e.g., fb.setMethodName("query(java.lang.Integer)"). Primitive types can be specified directly, such as query(int).

4. MethodInvokingFactoryBean (method invocation)

public class MethodInvokingFactoryBean extends MethodInvokingBean implements FactoryBean<Object> {
    private Object singletonObject;
    public void afterPropertiesSet() throws Exception {
        prepare();
        if (this.singleton) {
            this.initialized = true;
            this.singletonObject = invokeWithTargetException();
        }
    }
    public Object getObject() throws Exception {
        if (this.singleton) {
            return this.singletonObject;
        }
        return null;
    }
}

Static method example:

public class PersonFactory {
    public static PersonService getPs() {
        return new PersonService();
    }
    public PersonService getInstance() {
        return new PersonService();
    }
}
@Configuration
public class AppConfig {
    @Bean
    MethodInvokingFactoryBean methodInvokingFactoryBean() {
        MethodInvokingFactoryBean fb = new MethodInvokingFactoryBean();
        fb.setTargetClass(PersonFactory.class);
        fb.setTargetMethod("getPs");
        return fb;
    }
}
@Resource
private PersonService ps;
@Test
public void testSave() {
    ps.query(10);
}
// Output: PersonService query method id: 10

Non‑static method configuration:

MethodInvokingFactoryBean fb = new MethodInvokingFactoryBean();
fb.setTargetObject(new PersonFactory());
fb.setTargetMethod("getInstance");

The article ends by asking readers if they can think of scenarios for using these two classes and encourages likes, shares, and follows.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentspring-bootmethodinvokingfactorybeanmethodlocatingfactorybean
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.