草庐IT

Autowired

全部标签

java - Spring @Autowired @Lazy

我正在使用Spring注解,我想使用惰性初始化。我遇到了一个问题,当我想从另一个类导入一个bean时,我被迫使用@Autowired,它似乎没有使用惰性初始化。无论如何强制这种懒惰的初始化行为?在这个例子中,我不想看到“加载父bean”被打印出来,因为我只加载childBean,它不依赖于lazyParent。@ConfigurationpublicclassConfigParent{@Bean@LazypublicLonglazyParent(){System.out.println("Loadingparentbean");return123L;}}@Configuration@I

java - 无法自动连线字段: private org. springframework.security.core.userdetails.UserDetailsS​​ervice

我是Spring的新手,所以我一直在考虑安全方面。每当我运行我的应用程序时,我都会得到:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'securityConfig':Injectionofautowireddependenciesfailed;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:privateorg.springfra

java - 类新实例上的 Spring @Autowired

我对Spring不是很熟悉,遇到以下情况:存储库类:@RepositorypublicclassMyRepository{//...}使用存储库类的类:publicclassMyClassextendsAbstractClass{@AutowiredprivateMyRepositorymyRepository;//...}我知道如果我注释我的MyClass与@Component并将其与@Autowired一起使用,然后是@AutowiredMyRepository解决就好了。问题是我需要创建MyClass的新实例与反射。所以MyRepository永远不会解决,并且始终为null。有

java - Spring Autowiring 接口(interface)

我有一个接口(interface)IMenuItempublicinterfaceIMenuItem{StringgetIconClass();voidsetIconClass(StringiconClass);StringgetLink();voidsetLink(Stringlink);StringgetText();voidsetText(Stringtext);}然后我有这个接口(interface)的实现@Component@Scope("prototype")publicclassMenuItemimplementsIMenuItem{privateStringiconCl

java - Spring 注解@Autowired 内部方法

@Autowired可以与构造函数、setter和类变量一起使用。如何在方法或任何其他范围内使用@Autowired注释。?我尝试了以下,但它会产生编译错误。例如publicclasssTestSpring{publicvoidmethod(Stringparam){@AutowiredMyCustomObjctobj;obj.method(param);}}如果这是不可能的,有没有其他方法可以实现?(我使用的是Spring4。) 最佳答案 @Autowired注解本身是用注解的@Target({ElementType.CONSTR

java - 如何重构使用 spring Autowiring 的代码库

我继承了两个相当重要的代码库,它们使用spring来配置应用程序。现在我需要重新配置应用程序。但是很多配置是通过Autowiring提供的,所以几乎不可能找出实际配置是什么。项目大小适中,每个项目大约有20个maven模块,包括集成测试模块等。大多数模块为各种目的定义了一些应用程序上下文,其中包含一个或两个本地spring配置文件以及一个或两个它所依赖的核心模块。结果是无数的配置,并且我不能更改类或变量名称(或setter方法)而不冒破坏某些上游或下游模块中的依赖关系的风险,即使在项目的任何地方都看不到这种依赖关系。我如何在spring中有效地使用Autowiring的依赖项?任何人,

spring for hadoop 使用 FsShell autowired 报错

我在基于springboot的hadoop上使用spring唯一文件设置FsShell只做了需要的但正如这篇文章发生错误错误是:errorisCouldnotautowire.Nobeansof'FsShell'typefound.请帮帮我 最佳答案 我想念依赖compile('org.springframework.data:spring-data-hadoop-boot:2.4.0.BUILD-SNAPSHOT')正在运行但我正在使用compile('org.springframework.data:spring-data-ha

Spring Boot @Autowired注入为空的原因及解决方法

在使用SpringBoot时,@Autowired注解是常用的依赖注入方式。然而,有时候我们可能会遇到@Autowired注入为空的情况,本文将探讨可能的原因并提供解决方法。未正确配置@Component或@Bean注解在使用@Autowired注解进行依赖注入时,被注入的对象必须被SpringBoot识别为一个可被管理的组件。这可以通过在类上添加@Component或@Bean注解来实现。如果忘记添加这些注解,SpringBoot将无法正确识别和管理该对象,导致@Autowired注入失败。示例代码://错误示例publicclassMyService{@AutowiredprivateMy

解决Spring Boot单元测试中@Autowired依赖注入失效的问题

本文介绍了在SpringBoot单元测试中使用@Autowired注入的方法中,由于使用反射导致依赖注入失效的问题,以及如何使用AutowiredAnnotationBeanPostProcessor手动处理依赖注入来解决这个问题。在SpringBoot的单元测试中,我们经常使用@Autowired注解来自动注入需要测试的对象或依赖。然而,有时候我们可能会发现在被@Autowired注入的对象中,依赖的对象都是null,导致测试无法进行。这个问题通常是由于使用反射调用私有方法造成的。在通过反射调用私有方法时我们使用的其实不是spring容器中的对象原因如下: 在使用@SpringBootTes

【报错】BeanCreationException: Error creating bean with name ‘xxController‘: Injection of autowired ....

一、报错内容org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'authController':Injectionofautowireddependenciesfailed;nestedexceptionisjava.lang.IllegalArgumentException:Couldnotresolveplaceholder'auth.username'invalue"${auth.username}" atorg.springframework.beans.factory.an