Spring官网阅读(二)(依赖注入及方法注入)(2)

简介: Spring官网阅读(二)(依赖注入及方法注入)(2)

方法注入:


我们不完全按照官网顺序进行学习,先看这一小节,对应官网上的位置如下图

微信图片_20221112150416.jpg


为什么需要方法注入:


首先我们思考一个问题,在有了依赖注入的情况下,为什么还需要方法注入这种方式呢?换而言之,方法注入解决了什么问题?

我们来看下面这种场景:

@Component
public class MyService {
  @Autowired
  private LuBanService luBanService;
  public void test(int a){
    luBanService.addAndPrint(a);
  }
}
@Component
// 原型对象
@Scope("prototype")
public class LuBanService {
  int i;
  LuBanService() {
    System.out.println("luBan create ");
  }
  // 每次将当前对象的属性i+a然后打印
  public void addAndPrint(int a) {
    i+=a;
    System.out.println(i);
  }
}
public class Main02 {
  public static void main(String[] args) {
    AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(Config.class);
    MyService service = (MyService) ac.getBean("myService");
    service.test(1);
    service.test(2);
    service.test(3);
  }
}

在上面的代码中,我们有两个Bean,MyService为单例的Bean,LuBanService为原型的Bean。我们的本意可能是希望每次都能获取到不同的LuBanService,预期的结果应该打印出:


1,2,3


实际输出:


1

3

6


这个结果说明我们每次调用到的LuBanService是同一个对象。当然,这也很好理解,因为在依赖注入阶段我们就完成了LuBanService的注入,之后我们在调用测试方法时,不会再去进行注入,所以我们一直使用的是同一个对象。


我们可以这么说,原型对象在这种情况下,失去了原型的意义,因为每次都使用的是同一个对象。那么如何解决这个问题呢?只要我每次在使用这个Bean的时候都去重新获取就可以了,那么这个时候我们可以通过方法注入来解决。


通过注入上下文(applicationContext对象)


又分为以下两种方式:


实现org.springframework.context.ApplicationContextAware接口

@Component
public class MyService implements ApplicationContextAware {
  private ApplicationContext applicationContext;
  public void test(int a) {
    LuBanService luBanService = ((LuBanService) applicationContext.getBean("luBanService"));
    luBanService.addAndPrint(a);
  }
  @Override
  public void setApplicationContext(@Nullable ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }
}

直接注入上下文

@Component
public class MyService{
  @Autowired
  private ApplicationContext applicationContext;
  public void test(int a) {
    LuBanService luBanService = ((LuBanService) applicationContext.getBean("luBanService"));
    luBanService.addAndPrint(a);
  }
}

通过@LookUp的方式(也分为注解跟XML两种方式,这里只演示注解的)

@Component
public class MyService{
  public void test(int a) {
    LuBanService luBanService = lookUp();
    luBanService.addAndPrint(a);
  }
  // 
  @Lookup
  public LuBanService lookUp(){
    return null;
  }
}

方法注入 之 replace-method


方法注入还有一种方式,即通过replace-method这种形式,没有找到对应的注解,所以这里我们也就用XML的方式测试一下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="myService" class="com.dmz.official.service.MyService">
    <replaced-method replacer="replacer" name="test"/>
  </bean>
  <bean id="replacer" class="com.dmz.official.service.MyReplacer"/>
</beans>
public class MyReplacer implements MethodReplacer {
    @Override
   public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
        System.out.println("替代"+obj+"中的方法,方法名称:"+method.getName());
        System.out.println("执行新方法中的逻辑");
        return null;
    }
}
public class MyService{
    public void test(int a) {
        System.out.println(a);
    }
}
public class Main {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext cc =
            new ClassPathXmlApplicationContext("application.xml");
        MyService myService = ((MyService) cc.getBean("myService"));
        myService.test(1);
    }
}

执行结果:

替代com.dmz.official.service.MyService$$EnhancerBySpringCGLIB$$61c14242@63e31ee中的方法,方法名称:test
执行新方法中的逻辑

这里需要注意一点:

我在测试replace-method这种方法注入的方式时,受动态代理的影响,一直想将执行我们被替代的方法。用代码体现如下:

public class MyReplacer implements MethodReplacer {
  @Override
  public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
//    System.out.println("替代"+obj+"中的方法,方法名称:"+method.getName());
//    System.out.println("执行新方法中的逻辑");
    method.invoke(obj,args);
    return null;
  }
}

但是,这段代码是无法执行的,会报栈内存溢出。因为obj是我们的代理对象,method.invoke(obj,args)执行时会进入方法调用的死循环。最终我也没有找到一种合适的方式来执行被替代的方法。目前看来这可能也是Spring的设计,所以我们使用replace-method的场景应该是想完全替代某种方法的执行逻辑,而不是像AOP那样更多的用于在方法的执行前后等时机完成某些逻辑。


依赖注入跟方法注入的总结:


  • 我们首先要明确一点,什么是依赖(Dependencies)?来看官网中的一段话:

微信图片_20221112151143.jpg

可以说,一个对象的依赖就是它自身的属性,Spring中的依赖注入就是属性注入。


  • 我们知道一个对象由两部分组成:属性+行为(方法),可以说Spring通过属性注入+方法注入的方式掌控的整个bean。
  • 属性注入跟方法注入都是Spring提供给我们用来处理Bean之间协作关系的手段
  • 属性注入有两种方式:构造函数,Setter方法。
  • 方法注入(LookUp Method跟Replace Method)需要依赖动态代理完成
  • 方法注入对属性注入进行了一定程度上的补充,因为属性注入的情况下,原型对象可能会失去原型的意义,见:为什么需要方法注入

画图如下:

微信图片_20221112151228.jpg


相关文章
|
Java Spring
在使用Spring的`@Value`注解注入属性值时,有一些特殊字符需要注意
【10月更文挑战第9天】在使用Spring的`@Value`注解注入属性值时,需注意一些特殊字符的正确处理方法,包括空格、引号、反斜杠、新行、制表符、逗号、大括号、$、百分号及其他特殊字符。通过适当包裹或转义,确保这些字符能被正确解析和注入。
748 3
|
Java 测试技术 程序员
为什么Spring不推荐@Autowired用于字段注入?
作为Java程序员,Spring框架在日常开发中使用频繁,其依赖注入机制带来了极大的便利。然而,尽管@Autowired注解简化了依赖注入,Spring官方却不推荐在字段上使用它。本文将探讨字段注入的现状及其存在的问题,如难以进行单元测试、违反单一职责原则及易引发NPE等,并介绍为何Spring推荐构造器注入,包括增强代码可读性和维护性、方便单元测试以及避免NPE等问题。通过示例代码展示如何将字段注入重构为构造器注入,提高代码质量。
395 1
|
11月前
|
Java Spring
一键注入 Spring 成员变量,顺序编程
介绍了一款针对Spring框架开发的插件,旨在解决开发中频繁滚动查找成员变量注入位置的问题。通过一键操作(如Ctrl+1),该插件可自动在类顶部添加`@Autowired`注解及其成员变量声明,同时保持光标位置不变,有效提升开发效率和代码编写流畅度。适用于IntelliJ IDEA 2023及以上版本。
162 2
一键注入 Spring 成员变量,顺序编程
|
缓存 Java Spring
源码解读:Spring如何解决构造器注入的循环依赖?
本文详细探讨了Spring框架中的循环依赖问题,包括构造器注入和字段注入两种情况,并重点分析了构造器注入循环依赖的解决方案。文章通过具体示例展示了循环依赖的错误信息及常见场景,提出了三种解决方法:重构代码、使用字段依赖注入以及使用`@Lazy`注解。其中,`@Lazy`注解通过延迟初始化和动态代理机制有效解决了循环依赖问题。作者建议优先使用`@Lazy`注解,并提供了详细的源码解析和调试截图,帮助读者深入理解其实现机制。
763 1
|
Java Apache Spring
Spring官网改版后下载
Spring官网改版后下载 Spring官网改版后找了好久都没有找到直接下载Jar包的链接,下面汇总些网上提供的方法,亲测可用. 1.直接输入地址,改相应版本即可:http://repo.springsource.org/libs-release-local/org/springframework/spring/3.2.4.RELEASE/spring-fra
1294 0
|
Java Spring
Spring官网改版后下载
<p style="color:rgb(54,46,43); font-family:Arial; font-size:13.63636302947998px; line-height:26px"> Spring官网改版后找了好久都没有找到直接下载Jar包的链接,下面汇总些网上提供的方法,亲测可用.</p> <p style="color:rgb(54,46,43); font-fam
1485 0
|
4月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
878 0
|
5月前
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
618 0
|
1月前
|
JavaScript Java Maven
【SpringBoot(二)】带你认识Yaml配置文件类型、SpringMVC的资源访问路径 和 静态资源配置的原理!
SpringBoot专栏第二章,从本章开始正式进入SpringBoot的WEB阶段开发,本章先带你认识yaml配置文件和资源的路径配置原理,以方便在后面的文章中打下基础
271 3
|
1月前
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
814 2

热门文章

最新文章

下一篇
oss云网关配置