Spring Boot 依赖注入如何使用不同类型的注解
How SpringBoot dependency injection works with different type of annotations
我最近开始探索 Spring Boot
。我看到有两种方法可以在 Spring Boot
中定义 Beans
。
- 定义
@Bean
在 class 注释 @SprinBootApplication
- 在用
@Configuration
注释的 class 中定义 @Bean
我也很困惑 stereo-type annotation
@Repository
@Service
@Controller
等等
有人可以解释依赖注入如何与这些注释一起工作吗?
Spring Boot auto-configuration 尝试根据您添加的 jar 依赖项自动配置您的 Spring 应用程序。
您需要通过添加 @EnableAutoConfiguration 或 @SpringBootApplication[=21 来 opt-in 到 auto-configuration =] 对您的 @Configuration 类.
之一的注释
您可以自由使用任何标准 Spring 框架技术来定义您的 bean 及其注入的依赖项。为简单起见,我们经常发现使用 @ComponentScan(查找 bean)和使用 @Autowired(进行构造函数注入)效果很好。
One way is to define @Bean in the class annotated with
@SprinBootApplication
如果你看到 @SprinBootApplication
它是许多注释的组合,其中之一是 @Configuration
。所以当你在 Main class 中定义 @Bean
时,这意味着它在 @Configuration
class 中。
Indicates that a class declares one or more @Bean methods and may be
processed by the Spring container to generate bean definitions and
service requests for those beans at runtime.
class annotated with @Configuration
当你定义@Bean
是一个用@Configuration
注释class的class时,这意味着它是spring配置的一部分,所有Beans
在其中定义所有可用的 Dependency-Injection
。
I have also seen some code where neither of the 2 above approaches
have been used and yet dependency injection works fine. I have tried
to research a lot on this but could not find any concrete answer to
this. Is this possible?
根据文档,我假设您在谈论 Sterio-type annotation. Every sterio type annotation has @Component:
Indicates that an annotated class is a "component". Such classes are
considered as candidates for auto-detection when using
annotation-based configuration and classpath scanning.
是的,这是可能的。
您可以在任何 @Configuration 或 @SpringBootApplication class 中使用 @Bean,或者使用 @Service、@Component @Repository 等注释显式标记 bean classes
@Service 或@Component
当您使用@Service 或@Compoenent 标记class 并且如果spring 的注释扫描范围允许它到达包时,spring 将注册实例那些 class 是 spring beans
。
您可以在 @ComponentScan
扫描期间提供要 included/excluded 的包
@Bean
@Beans
标记在可以创建特定 class 实例的工厂方法上。
@Bean
public Account getAccount(){
return new DailyAccount();
}
现在在您的应用程序中,您可以简单地使用@Autowire Account,spring 将在内部调用其工厂方法 getAccount
,后者又 returns DailyAccount
的一个实例。
使用@Bean 与@Service 或@Compoenent 有一个简单的区别。
第一个使您的 bean 彼此松散耦合。
- 在@Bean 中,您可以灵活地更改帐户实现,甚至无需更改任何帐户 classes。
- 考虑一下如果您的 classes 实例化是一个 multi-step 操作,例如读取属性值等,那么您可以在 @Bean 方法中轻松地完成它。
- 如果您没有源代码访问您尝试实例化的 class,@Bean 也会提供帮助。
我最近开始探索 Spring Boot
。我看到有两种方法可以在 Spring Boot
中定义 Beans
。
- 定义
@Bean
在 class 注释@SprinBootApplication
- 在用
@Configuration
注释的 class 中定义@Bean
我也很困惑 stereo-type annotation
@Repository
@Service
@Controller
等等
有人可以解释依赖注入如何与这些注释一起工作吗?
Spring Boot auto-configuration 尝试根据您添加的 jar 依赖项自动配置您的 Spring 应用程序。
您需要通过添加 @EnableAutoConfiguration 或 @SpringBootApplication[=21 来 opt-in 到 auto-configuration =] 对您的 @Configuration 类.
之一的注释您可以自由使用任何标准 Spring 框架技术来定义您的 bean 及其注入的依赖项。为简单起见,我们经常发现使用 @ComponentScan(查找 bean)和使用 @Autowired(进行构造函数注入)效果很好。
One way is to define @Bean in the class annotated with @SprinBootApplication
如果你看到 @SprinBootApplication
它是许多注释的组合,其中之一是 @Configuration
。所以当你在 Main class 中定义 @Bean
时,这意味着它在 @Configuration
class 中。
Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.
class annotated with @Configuration
当你定义@Bean
是一个用@Configuration
注释class的class时,这意味着它是spring配置的一部分,所有Beans
在其中定义所有可用的 Dependency-Injection
。
I have also seen some code where neither of the 2 above approaches have been used and yet dependency injection works fine. I have tried to research a lot on this but could not find any concrete answer to this. Is this possible?
根据文档,我假设您在谈论 Sterio-type annotation. Every sterio type annotation has @Component:
Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.
是的,这是可能的。 您可以在任何 @Configuration 或 @SpringBootApplication class 中使用 @Bean,或者使用 @Service、@Component @Repository 等注释显式标记 bean classes
@Service 或@Component
当您使用@Service 或@Compoenent 标记class 并且如果spring 的注释扫描范围允许它到达包时,spring 将注册实例那些 class 是 spring beans
。
您可以在 @ComponentScan
@Bean
@Beans
标记在可以创建特定 class 实例的工厂方法上。
@Bean
public Account getAccount(){
return new DailyAccount();
}
现在在您的应用程序中,您可以简单地使用@Autowire Account,spring 将在内部调用其工厂方法 getAccount
,后者又 returns DailyAccount
的一个实例。
使用@Bean 与@Service 或@Compoenent 有一个简单的区别。 第一个使您的 bean 彼此松散耦合。
- 在@Bean 中,您可以灵活地更改帐户实现,甚至无需更改任何帐户 classes。
- 考虑一下如果您的 classes 实例化是一个 multi-step 操作,例如读取属性值等,那么您可以在 @Bean 方法中轻松地完成它。
- 如果您没有源代码访问您尝试实例化的 class,@Bean 也会提供帮助。