简体   繁体   English

JAVA - 我可以直接删除@Autowire吗?

[英]JAVA - Can I delete @Autowire directly?

after my teammates merged her changes (implements @EnableFeignClients for our SpringBootApplication and created some feign clients, properties and configs from the main branch), when boot the application it will pop up the The dependencies of some of the beans in the application context from a cycle: xxx在我的队友合并她的更改后(为我们的 SpringBootApplication 实现 @EnableFeignClients 并从主分支创建一些假客户端、属性和配置),当启动应用程序时,它将弹出The dependencies of some of the beans in the application context from a cycle: xxx

To resolve this issue I deleted some @Autowired from some involved controller/service classes and make them private and then the application can be boot successfully:为了解决这个问题,我从一些涉及的控制器/服务类中删除了一些 @Autowired 并将它们设为私有,然后应用程序可以成功启动:

before my change:在我改变之前:

@Autowired
MyUtil myUtil; //@Component

@Autowired
MyConfig myConfig; //@Component

@Autowired
MyApi myApi; //Interface class

public void myFunction(){
   String id = myUtil.getId();
   String name = myConfig.getNameById(id);
   myApi.sendInfo(id, name); 
}

after my change:在我改变之后:

private MyUtil myUtil; //@Component

private MyConfig myConfig; //@Component

private MyApi myApi; //@Component

public void myFunction(){
   String id = myUtil.getId();
   String name = myConfig.getNameById(id);
   myApi.sendInfo(id, name); 
}

Spring version: Spring版本:

<groupID>org.springframework.boot</groupID>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>

and here are my concerns:这是我的担忧:

  1. Will there be any impact after my changes (functional, performance, etc.)?我的改变(功能、性能等)后会有什么影响吗?

  2. In my case (or in most scenarios in the future development), should i use @Autowired?就我而言(或者在未来开发的大多数场景中),我应该使用@Autowired吗? When need to?什么时候需要? When doesn't need to?什么时候不需要?

@Autowired help the spring to find where it should inject classes. @Autowired 帮助 spring 找到应该注入类的位置。

The impact is that spring will not create objects of the classes where you removed the annotation.影响是 spring 不会创建您删除注释的类的对象。

Additional way to create objects in spring are with setter or constructor injection.在 spring 中创建对象的其他方法是使用 setter 或构造函数注入。

Example of constructor injection will be something like:构造函数注入的示例将类似于:

public YourClassName(MyUtil myUtil, MyConfig myConfig, MyApi myApi){
      this.myUtil = myUtil;   
      this.myConfig = myConfig;   
      this.myApi = myApi;   
}

I think also good idea as well to check why @Autowired not working.我认为检查@Autowired 为何不起作用也是个好主意。 Can you provide full error?你能提供完整的错误吗? Does your class annotated with @Service or something您的 class 是否用 @Service 或其他注释

TL DR. TL博士。 You cannot delete @Autowired directly.您不能直接删除@Autowired。

@Autowired is used by Spring in order to do dependency injection ( I know this is a fancy word) @Autowired 被 Spring 用来进行依赖注入(我知道这是一个花哨的词)

@Autowired
ChickenService chickenService

@Autowired is almost equivalent to ChickenService chickenService = new ChickenService(); @Autowired几乎等同于 ChickenService chickenService = new ChickenService();

After your change, private MyConfig myConfig; //@Component更改后, private MyConfig myConfig; //@Component private MyConfig myConfig; //@Component myConfig will be null and therefore you introduced a bug. private MyConfig myConfig; //@Component myConfig将为 null,因此您引入了一个错误。

Suppose that you have an EggService假设你有一个 EggService

@Service
public class ChickenService {
  
   @Autowired 
   private EggService eggService;

}

The chicken holds a reference to the egg鸡持有对鸡蛋的引用

@Service
public class EggService {
  
   @Autowired 
   private ChickenService; 

}

Now you have a chicken and egg problem.现在你遇到了先有鸡还是先有蛋的问题。 https://www.baeldung.com/circular-dependencies-in-spring https://www.baeldung.com/circular-dependencies-in-spring

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM