《Spring Boot 3实战案例锦集》PDF电子书已更新至100篇!
🎉🎉《Spring Boot实战案例合集》目前已更新120个案例,我们将持续不断的更新。文末有电子书目录。
💪💪永久更新承诺
我们郑重承诺,所有订阅合集的粉丝都将享受永久免费的后续更新服务。
💌💌如何获取
订阅我们的合集《点我订阅》,并通过私信联系我们,我们将第一时间将电子书发送给您。
环境:SpringBoot3.4.2
本篇文章将介绍Spring2个强大的类,但不知道有什么应用场景。
MethodLocatingFactoryBean(方法查找)
MethodInvokingFactoryBean(方法调用)
1. 环境准备
首先,准备一个Service Bean,该类会在接下来的代码中都会使用到。
@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) ;}}
该类非常的简单就是为了接下来的演示足够简单清晰。
2. 方法定位
public class MethodLocatingFactoryBean implementsFactoryBean<Method>, BeanFactoryAware {private String targetBeanName;private String methodName;private Method method;public Method getObject()throws Exception {returnthis.method;}public Class<Method> getObjectType(){return Method.class;}// ...}
该类是个FactoryBean,具体返回的是Method对象;该类主要的作用就是通过beanName查找到对应的Class,然后通过设置的methodName查找该Class对应的Method对象。
定义Bean对象
public class AppConfig {MethodLocatingFactoryBean methodLocatingFactoryBean() {MethodLocatingFactoryBean fb = new MethodLocatingFactoryBean() ;// 设置要查找的方法签名fb.setMethodName("save") ;// 设置beanNamefb.setTargetBeanName("ps") ;return fb ;}}
知道了MethodLocatingFactoryBean返回的是Method,那接下来就可以在其它的Bean中直接注入该Method对象。
public class BeanMethod {// 注入Methodprivate Method method ;private PersonService target ;public Object invoke(Object ... params) throws Exception {return method.invoke(target, params) ;}}
测试&输出结果
private BeanMethod bm ;public void testSave() {bm.invoke() ;}// 输出PersonService save method invoke...
配置带参数的方法
MethodLocatingFactoryBean fb = new MethodLocatingFactoryBean() ;fb.setMethodName("query(java.lang.Integer)") ;
方法的参数类型需要完整的限定名(包名+类名),如果方法参数是基本数据类型,那么直接写基本类型即可(query(int))。
3. 方法调用
public class MethodInvokingFactoryBean extends MethodInvokingBeanimplements FactoryBean<Object>// 返回的实际对象private Object singletonObjectpublic 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;}}}
该类在初始化方法调用时,会调用目标类的方法,方法返回值作为这里getObject方法的返回对象。
定义一个工厂类(无所谓是否是工厂类)
public class PersonFactory {public PersonService getInstance() {return new PersonService() ;}public static PersonService getPs() {return new PersonService() ;}}
该类不用注册为Bean对象。接下来配置bean对象
public class AppConfig {MethodInvokingFactoryBean methodInvokingFactoryBean() {MethodInvokingFactoryBean fb = new MethodInvokingFactoryBean() ;// 静态方法fb.setTargetClass(PersonFactory.class) ;fb.setTargetMethod("getPs") ;return fb ;}}
测试&输出结果
private PersonService ps ;publicvoidtestSave(){ps.query(10) ;}// 输出PersonService query method id: 10
配置为非静态方法
MethodInvokingFactoryBean fb = new MethodInvokingFactoryBean() ;// 非静态方法fb.setTargetObject(new PersonFactory()) ;fb.setTargetMethod("getInstance") ;
测试&结果,与上面相同。
想不到有什么场景使用这2个类,你们知道吗?
以上是本篇文章的全部内容,如对你有帮助帮忙点赞+转发+收藏。
推荐文章
请一定记住!Spring Boot 执行初始化操作的 7 种王炸手段
Spring Boot 又一强大的内置功能,自动记录API请求/响应数据
玩转Redis!非常强大的Redisson分布式集合,少写60%代码
高级开发!一个注解动态控制Controller接口,支持实时更新
Spring Boot + Groovy 实现灵活的动态规则引擎
非常实用!玩转 Spring Boot 接口参数类型转换,支持任意场景
强大的异步任务处理类CompletableFuture使用详解


