java 如何在 SpringBoot 中@Autowire 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46287538/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to @Autowire services in SpringBoot
提问by Greg
Good day, guys. I have a question about autowiring services into my classes when using 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):
美好的一天,伙计们。我有一个关于在使用 Springboot 时将服务自动装配到我的类中的问题。我在 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. Another form is to @Autowire the property like this:
这是一个通过其构造函数注入属性的类,通过@Autowiring 构造函数。另一种形式是@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? (Something like @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.
现在我要问你的问题是:为什么没有简单的注释可以让 @Autowire 正常运行?(类似于@AutowiredClass)。上面的方法太冗长而且难以记住,所以肯定有更好的方法让@Autowired 在类上工作以注入服务,就像我们在 Grails 中所做的那样def someService
,它会被自动注入。
回答by Pavlo Pastushok
If you want properly use @Autowired
in your spring-boot application, you must do next steps:
如果您想@Autowired
在您的 spring-boot 应用程序中正确使用,您必须执行以下步骤:
- Add
@SpringBootApplication
to your main class - Add
@Service
or@Component
annotation to class you want inject - Use one of two ways that you describe in question, to autowire
- 添加
@SpringBootApplication
到您的主类 - 添加
@Service
或@Component
注释到要注入的类 - 使用您所描述的两种方式之一进行自动装配
回答by Joanna
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. 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.
如果您没有任何 wiered 包结构并且主类包包含您希望 spring 实例化的所有其他类(直接或在子包中)@ComponentScan
,则主类上的简单注释将帮助您保存所有这些样板代码。那么Spring会做的魔力,它会去扫描包(子包和),然后寻找有注解的类@Service
,@Component
等,并对其进行实例化。
Even better, use @SpringBootApplication
in your main class, this will cover @Configuration
as well. If it is a green field project , I would encourage to start from start.spring.io- a template generation/scaffolding tool for spring
更好的是,@SpringBootApplication
在您的主类中使用,这也将涵盖@Configuration
。如果它是一个绿地项目,我会鼓励从start.spring.io开始- spring的模板生成/脚手架工具
回答by Cuga
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
本演练中有一个示例:REST Service with Spring Boot
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. @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. @ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.
@SpringBootApplication 是一个方便的注解,它添加了以下所有内容: @Configuration 将该类标记为应用程序上下文的 bean 定义的来源。@EnableAutoConfiguration 告诉 Spring Boot 根据类路径设置、其他 bean 和各种属性设置开始添加 bean。@ComponentScan 告诉 Spring 在 hello 包中查找其他组件、配置和服务,允许它找到控制器。
回答by Urosh T.
@Autowired
almost works out of the box. Just do your component scanning of the class you want to autowireand you are done. Just make sure your main class (or main configuration class) uses @ComponentScan("{com.example.app}")
or @SpringBootApplication
(main class). The docsexplain this stuff pretty good
@Autowired
几乎开箱即用。只需对要自动装配的类进行组件扫描即可。只需确保您的主类(或主配置类)使用@ComponentScan("{com.example.app}")
或@SpringBootApplication
(主类)。该文档解释这个东西不错
回答by Zilvinas
You need to annotate the implementation of RestService
as a @Service
or @Component
so Springwould pick it up.
你需要注释的实施RestService
作为@Service
或者@Component
所以春天将它捡起来。
@Service
public class MyRiskAssessorImpl implements RiskAssessor {
///
}