Spring基础——方法注入(Method Injection)

本文介绍了Spring框架中查找方法注入(LookupMethod)和任意方法替换(ArbitraryMethodReplacement)的原理和使用,以及如何通过ApplicationContextAware实现prototype模式的Bean实例化。同时,讨论了这些技术的优缺点和官方推荐的替代方案。
摘要由CSDN通过智能技术生成

文章所用项目源码参考:java_spring_learn_repo

查找方法注入(Lookup Method)

  • 通常我们项目开发中大多都使用单例模式的Bean,因此Bean之间的依赖大多都是单例与单例之间的,而如果我们想将prototype模式的Bean被注入到单例模式的Bean中,因为单例Bean只会在创建时注入Bean的引用,所以prototype模式的Bean会一直是最开始创建的实例(换句话说就是prototype间接失效了,已经是单例模式了)。如果想要每次调用prototype模式Bean都获取一个新的实例,可以通过方法注入来实现。
  • 在Spring官方给的文档中提供了一种能在每次调用都获取新实例对象的方案,可以通过实现ApplicationContextAware接口,给Bean注入容器,然后再让Bean需要调用新实例时让容器创建一个新的实例。

ApplicationContextAware是Spring框架中的一个接口,用于让Bean实现类获取对Spring应用上下文(ApplicationContext)的引用。通过实现ApplicationContextAware接口,Bean可以在被实例化后,由Spring容器注入应用上下文的引用,从而获取容器中的其他Bean或执行一些与容器相关的操作。

  • 首先创建一个prototype的Bean
public class Command {
    private String state;

    public void setState(String state) {
        this.state = state;
    }

    public String execute() {
        return this + "对象被执行";
    }
}
  • XML配置Scope为prototype
<bean id="command" class="com.nobugnolife.method_injection.Command" scope="prototype"/>
  • 这里通过实现ApplicationContextAware来获取ApplicationContext,然后在对象每次调用process方法时都会让容器获取一个新的Command实例
public class MyApplicationContextAwareBean implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    // 通过applicationContext创建新的Commmad实例化Bean
    protected Command createCommand() {
        return this.applicationContext.getBean("command", Command.class);
    }

    // 每次调用process时都从容器中获取新的bean实例
    public String process(String state) {
        Command command = createCommand();
        command.setState(state);
        return command.execute();
    }
}
  • 测试process调用是否为新实例
@Test
public void testMethodInjection(){
    ApplicationContext ctx = new ClassPathXmlApplicationContext("methodInjection.xml");
    MyApplicationContextAwareBean mcb = ctx.getBean(MyApplicationContextAwareBean.class);
    System.out.println(mcb.process("发送信息"));
    System.out.println(mcb.process("激活"));
    System.out.println(mcb.process("添加"));
}

test_applicationcontextaware

  • 这种方法会将Spring的代码框架耦合进项目工程中,因此官方并不推荐通过这种方法解决非单例模式Bean的调用问题,Spring也给出了通过方法注入的解决方案(有其他很多解决方案,本篇文章重点是讲方法注入)。

查找方法注入

  • 查找方法是通过在单例Bean中声明一个抽象方法,该抽象方法会在Spring容器运行时动态生成(通过CGLIB库直接生成字节码)一个子类来实现这个抽象方法,以提供prototypeBean实例。
  • 因为Spring在运行的时候是自动创建子类实现,因此要被子类实现的抽象类不能有final
  • 如果是通过工厂模式实例化Bean的话,查找方法注入是不起作用的,特别是通过@Bean注解的方式,因为此时容器不负责创建实例,所以不能动态创建运行时子类。

基于XML的方法注入

  • 首先需要定义一个查找方法的抽象类,并提供实例化Bean的抽象方法
public abstract class CommandManager {
    public String process(String commandState) {
        // 通过调用createCommand来获取新的实例Bean
        Command command = createCommand();
        command.setState(commandState);
        return command.execute();
    }

    protected abstract Command createCommand();
}
  • 在bean配置中设置lookup-method标签
<!-- 查找方法注入 -->
<bean id="commandManager" class="com.nobugnolife.method_injection.CommandManager">
    <lookup-method name="createCommand" bean="command"/>
</bean>

基于注解的方法注入

  • 注解通过Lookup注解对抽象方法进行标记即可
@Component
public abstract class CommandManager {
    public String process(String commandState) {
        // 通过调用createCommand来获取新的实例Bean
        Command command = createCommand();
        command.setState(commandState);
        return command.execute();
    }

    @Lookup
    protected abstract Command createCommand();
}

Arbitrary Method Replacement(任意方法替换)

  • 任意方法是指Spring框架中一种高级的AOP(面向切面编程)功能。通过这种方式,你可以在运行时替换类中的方法,并提供自定义的实现逻辑。
  • 要想实现任意方法替换,首先要实现org.springframework.beans.factory.support.MethodReplacer提供的方法
public class ReplacementCompute implements MethodReplacer {
    public Object reimplement(Object o, Method method, Object[] objects) throws Throwable {
        // 获取原方法传入参数
        String input = (String) objects[0];
        // 执行新的指令方法
        return "Compute方法重构:" + input;
    }
}
  • 之后部署初始类,然后指定对应实现方法覆盖的Bean
<!-- 定义原方法Bean-->
<bean id="compute" class="com.nobugnolife.method_injection.Compute">
    <!-- 使用replaced-method关联需要覆盖的方法 name为方法名 replacer为指定覆盖方法的bean -->
    <replaced-method name="ComputeValue" replacer="replacementCompute">
        <!-- 重载方法参数的签名,只有当方法重载时有多个变体时才是必要的 -->
        <arg-type>String</arg-type>
    </replaced-method>
</bean>

<bean id="replacementCompute" class="com.nobugnolife.method_injection.ReplacementCompute"/>
  • 任意方法替换的功能很灵活而且十分强大,但因为过于接近底层,而且会重构代码逻辑,容易造成非常危险的后果,因此尽量不要在开发中运用。
  • 17
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值