Java @Bean 和 @Autowired 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34172888/
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
Difference between @Bean and @Autowired
提问by zhuochen shen
Why can't I use @Autowired
in this case?
为什么我不能@Autowired
在这种情况下使用?
@SpringBootApplication
public class Application {
@Autowired
BookingService bookingService;
public static void main(String[] args) {
bookingService.book("Alice", "Bob", "Carol");
}
}
but can use @Bean
但可以使用 @Bean
@SpringBootApplication
public class Application {
@Bean
BookingService bookingService() {
return new BookingService();
}
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
BookingService bookingService = ctx.getBean(BookingService.class);
bookingService.book("Alice", "Bob", "Carol");
}
}
Aren't the two ways to generate BookingService
the same thing?
这两种方法不是产生BookingService
相同的东西吗?
采纳答案by DaveyDaveDave
@Bean
and @Autowired
do two very different things. The other answers here explain in a little more detail, but at a simpler level:
@Bean
并@Autowired
做两件非常不同的事情。这里的其他答案更详细地解释了,但在更简单的层面上:
@Bean
tells Spring 'here is an instance of this class, please keep hold of it and give it back to me when I ask'.@Autowired
says 'please give me an instance of this class, for example, one that I created with an@Bean
annotation earlier'.
@Bean
告诉 Spring '这是这个类的一个实例,请保留它并在我要求时还给我'。@Autowired
说'请给我一个这个类的实例,例如,一个我@Bean
之前用注释创建的实例'。
Does that make sense? In your first example, you're asking Spring to give you an instance of BookingService
, but you're never creating one, so Spring has nothing to give you. In your second example, you're creating a new instance of BookingService
, telling Spring about it, and then, in the main()
method, asking for it back.
那有意义吗?在您的第一个示例中,您要求 Spring 为您提供一个 实例BookingService
,但您从未创建过一个实例,因此 Spring 没有任何东西可以提供给您。在您的第二个示例中,您正在创建 的一个新实例BookingService
,将其告知 Spring,然后在该main()
方法中要求返回它。
If you wanted, you could remove the two additional lines from the second main()
method, and combine your two examples as below:
如果需要,您可以从第二种main()
方法中删除另外两行,并将您的两个示例组合如下:
@SpringBootApplication
public class Application {
@Autowired
BookingService bookingService;
@Bean
BookingService bookingService() {
return new BookingService();
}
public static void main(String[] args) {
bookingService.book("Alice", "Bob", "Carol");
}
}
In this case, the @Bean
annotation gives Spring the BookingService
, and the @Autowired
makes use of it.
在这种情况下,@Bean
注释为 Spring 提供了BookingService
,并且@Autowired
使用了它。
This would be a slightly pointless example, as you're using it all in the same class, but it becomes useful if you have the @Bean
defined in one class, and the @Autowired
in a different one.
这将是一个有点无意义的例子,因为你在同一个类中使用它,但是如果你@Bean
在一个类中定义了它,并且在另一个类中定义它会变得有用@Autowired
。
回答by pmverma
@Bean
BookingService bookingService() {
return new BookingService();
}
Annotating @Bean
only registers the service as a bean(kind of an Object) in spring application context. In simple words, it is just registration and nothing else.
注释@Bean
仅将服务注册为 spring 应用程序上下文中的 bean(一种对象)。简单来说,就是注册而已。
@Autowired
BookingService bookingService;
Annotating a variable with @Autowired
injects a BookingService
bean(i.e Object) from Spring Application Context.
使用Spring Application Context@Autowired
注入BookingService
bean(即对象)来注释变量。
(i.e) The registered bean with @Bean
annotation will be injected to the variable annotated with @Autowired
.
(ie) 带有@Bean
注解的注册 bean将被注入到带有注解的变量中@Autowired
。
Hope this clears your doubt!
希望这能消除你的疑惑!
回答by Kris Swat
great answer by @DaveyDaveDave In the example instead of
@DaveyDaveDave 在示例中而不是
@Bean
BookingService bookingService() {
return new BookingService();
}
You can use @Service annotation on BookingService class
您可以在 BookingService 类上使用 @Service 注释
回答by n0rtan
Here's good article about @Autowired annotation: http://www.baeldung.com/spring-autowire
这是关于@Autowired 注释的好文章:http: //www.baeldung.com/spring-autowire
The @Autowired annotation can instantiate your injectables by defining @ComponentScan("namespace.with.your.components.for.inject") on config class
@Autowired 注释可以通过在配置类上定义 @ComponentScan("namespace.with.your.components.for.inject") 来实例化您的可注入对象
@Configuration
@ComponentScan("com.baeldung.autowire.sample")
public class AppConfig {}
All components must be marked by @Component annotation. It replaces the @Bean annotation.
所有组件都必须用@Component 注解进行标记。它取代了@Bean 注释。
回答by SARIKA
@Bean is just for the metadata definition to create the bean(equivalent to tag). @Autowired is to inject the dependancy into a bean(equivalent to ref XML tag/attribute).
@Bean 只是为了元数据定义来创建bean(相当于标签)。@Autowired 是将依赖注入到一个 bean 中(相当于 ref XML 标签/属性)。