简体   繁体   English

如何在SpringBoot中@Autowire服务

[英]How to @Autowire services in SpringBoot

Good day, guys. 大家好 I have a question about autowiring services into my classes when using Springboot. 我在使用Springboot时有一个关于将服务自动装配到类中的问题。 All of the examples I have seen on the Internet as well as in the Springboot specification do something of the like (taking an excerpt from the Springboot version 1.5.7 specification): 我在Internet以及Springboot规范中看到的所有示例都具有类似的功能(摘自Springboot版本1.5.7规范):

package com.example.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor;

@Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
}
// ...
}

This is a class that injects a property through its constructor, by means of @Autowiring the constructor. 这是一个通过@Autowiring构造函数通过其构造函数注入属性的类。 Another form is to @Autowire the property like this: 另一种形式是@Autowire这样的属性:

@Autowired
private final RiskAssessor riskAssessor

But, where I work, for these two methods to work, I have been told that I need to use this method: 但是,在我工作的地方,要使这两种方法都起作用,我被告知我需要使用此方法:

 applicationContext.getAutowireCapableBeanFactory().autowireBean(Object.class)

They have told me that I need this in order for the @Autowired annotation to work. 他们告诉我,我需要这样做才能使@Autowired注释起作用。

Now my question to you is: why is there no simple annotation that allows the @Autowire to function correctly? 现在我对您的问题是:为什么没有简单的注释可以使@Autowire正常运行? (Something like @AutowiredClass). (类似于@AutowiredClass)。 The above method is too verbose and hard to remember, so surely there must be a better way to make @Autowired work on classes in order to inject services, just like we do in Grails where we just say def someService and it is automatically injected. 上面的方法太冗长且难以记住,因此肯定有一种更好的方法可以使@Autowired在类上工作以注入服务,就像我们在Grails中所做的那样,我们只说def someService并自动注入。

If you want properly use @Autowired in your spring-boot application, you must do next steps: 如果要在Spring-boot应用程序中正确使用@Autowired ,则必须执行以下步骤:

  1. Add @SpringBootApplication to your main class @SpringBootApplication添加到您的主类
  2. Add @Service or @Component annotation to class you want inject 在要注入的类中添加@Service@Component批注
  3. Use one of two ways that you describe in question, to autowire 使用您描述的两种方式中的一种自动接线

If you don't have any wiered package structure and the main class package includes all other classes you want spring to instantiate (directly or in the subpackages) a simple annotation @ComponentScan on your main class will help you save all those boiler plate code. 如果您没有任何麻烦的包结构,并且主类包包括您要spring实例化的所有其他类(直接或在子包中),则在主类上使用简单的批注@ComponentScan可以帮助您保存所有这些样板代码。 Then spring will do the magic, it will go and scan the package(and subpackages) and look for classes annotated with @Service , @Component etc and instantiate it. 然后spring会做魔术,它将去扫描包(和子包)并查找带有@Service @Component@Service @Component等注释的@Component并将其实例化。

Even better, use @SpringBootApplication in your main class, this will cover @Configuration as well. 更好的是,在您的主类中使用@SpringBootApplication ,这也将涉及@Configuration If it is a green field project , I would encourage to start from start.spring.io - a template generation/scaffolding tool for spring 如果这是一个绿色项目,我鼓励从start.spring.io-一个用于spring的模板生成/脚手架工具开始

Now my question to you is: why is there no simple annotation that allows the @Autowire to function correctly? 现在我对您的问题是:为什么没有简单的注释可以使@Autowire正常运行?

There is: @SpringBootApplication 有: @SpringBootApplication

If you put this at the root of your application (file that contains the main class) and as long as your services are at the same package or a sub-package, Spring will auto-discover, instantiate, and inject the proper classes. 如果将其放在应用程序的根目录(包含主类的文件)中,并且只要您的服务位于同一包或子包中,Spring就会自动发现,实例化并注入适当的类。

There's an example in this walk-through: REST Service with Spring Boot 本演练中有一个示例: Spring Boot的REST服务

As described in that page: 如该页面中所述:

@SpringBootApplication is a convenience annotation that adds all of the following: @Configuration tags the class as a source of bean definitions for the application context. @SpringBootApplication是一个方便注释,它添加了以下所有内容:@Configuration将类标记为应用程序上下文的Bean定义的源。 @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. @EnableAutoConfiguration告诉Spring Boot根据类路径设置,其他bean和各种属性设置开始添加bean。 @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers. @ComponentScan告诉Spring在hello包中寻找其他组件,配置和服务,从而允许它找到控制器。

You need to annotate the implementation of RestService as a @Service or @Component so Spring would pick it up. 您需要将RestService的实现注释为@Service@Component以便Spring可以选择它。

@Service
public class MyRiskAssessorImpl implements RiskAssessor {
///
}

@Autowired almost works out of the box. @Autowired几乎可以立即使用。 Just do your component scanning of the class you want to autowire and you are done. 只需对要自动装配的类进行组件扫描,即可完成。 Just make sure your main class (or main configuration class) uses @ComponentScan("{com.example.app}") or @SpringBootApplication (main class). 只要确保您的主类(或主配置类)使用@ComponentScan("{com.example.app}")@SpringBootApplication (主类)即可。 The docs explain this stuff pretty good 文档解释了这些东西

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

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