Autowiring Dependencies

╰半橙微兮° 2024-02-29 04:51 83阅读 0赞

Spring bean autowiring 定义:

The Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.

Enabling @Autowired Annotations

@Autowired 的使用是有前置条件的 ☞ activate the dependency injection annotations

Spring项目 ☞ 可选方式1

To use Java-based configuration in our application, let’s enable annotation-driven injection to load our Spring configuration:

  1. @Configuration
  2. @ComponentScan("com.autowire.sample")
  3. public class AppConfig {
  4. }

Spring项目 ☞ 可选方式2

The <context:annotation-config> annotation is mainly used to activate the dependency injection annotations in Spring XML files.

SpringBoot项目

Spring Boot introduces the @SpringBootApplication annotation. This single annotation is equivalent to using @Configuration, @EnableAutoConfiguration, and @ComponentScan.

As a result, when we run this Spring Boot application, it will automatically scan the componentsin the current package and its sub-packages. Thus it will register them in Spring’s Application Context, and allow us to inject beans using @Autowired.

阅读更多
Spring <context:annotation-config> and <context:component-scan>
Spring Component Scanning
Spring @ComponentScan – Filter Types

在这里插入图片描述

There are four modes of autowiring a bean using an XML configuration

1、 no: the default value – this means no autowiring is used for the bean and we have to explicitly name the dependencies.
2、 byName: autowiring is done based on the name of the property, therefore Spring will look for a bean with the same name as the property that needs to be set.
3、 byType: similar to the byName autowiring, only based on the type of the property. This means Spring will look for a bean with the same type of the property to set. If there’s more than one bean of that type, the framework throws an exception.
3、constructor: autowiring is done based on constructor arguments, meaning Spring will look for beans with the same type as the constructor arguments.

by type

  1. @Bean(autowire = Autowire.BY_TYPE)
  2. public class Store {
  3. private Item item;
  4. public setItem(Item item){
  5. this.item = item;
  6. }
  7. }
  8. public class Store {
  9. @Autowired
  10. private Item item;
  11. }
  12. <bean id="store" class="org.store.Store" autowire="byType"> </bean>

By name

  1. # more than one bean of the same type,use the @Qualifier to reference a bean by name
  2. public class Store {
  3. @Autowired
  4. @Qualifier("item1")
  5. private Item item;
  6. }
  7. <bean id="item" class="org.store.ItemImpl1" />
  8. <bean id="store" class="org.store.Store" autowire="byName"> </bean>

We can also override the autowiring by defining dependencies explicitly through constructor arguments or setters.

参考:Intro to Inversion of Control and Dependency Injection with Spring

发表评论

表情:
评论列表 (有 0 条评论,83人围观)

还没有评论,来说两句吧...

相关阅读