spring-依赖注入和Autowired

在Spring容器中为一个bean配置依赖注入有三种方式:

1· 使用属性的setter方法注入  这是最常用的方式;

2· 使用构造器注入;

3· 使用Filed注入(用于注解方式).

 

对于使用字段(Filed)注入(用注解方式)

在Spring中,注入依赖对象可以采用手工装配或自动装配,在实际应用开发中建议使用手工装配,因为自动装配会产生许多未知情况,开发人员无法预见最终的装配结果。

手工装配依赖对象又分为两种方式:

一种是在XML文件中,通过在bean节点下配置;如上面讲到的使用属性的setter方法注入依赖对象和使用构造器方法注入依赖对象都是这种方式。

另一种就是在java代码中使用注解的方式进行装配,在代码中加入@Resource或者@Autowired、

 

  • Autowired注入是先byType再byName,populateBean方法里,通过后置处理器来处理,先bytype找,数量大于一再byName
  • Resource先byName,后byType
  • Qualifier和Autowired配合使用,指定bean的名称

   这里byName byType仅仅是两种装配技术

  

 

 

Spring 中,`@Autowired` 是基于注解的依赖注入的核心注解,可让容器知道为当前类注入哪些依赖,其按照类型匹配进行依赖注入,作用范围较广,能修饰属性、构造方法、普通方法参数 [^2]。以下是几种不同场景下使用 `@Autowired` 进行依赖注入的举例: #### 1. 属性注入 ```java package com.imooc.beanannotation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; // 使用 @Service 注解将该类注册为 Spring 服务组件 @Service public class InjectionServiceImpl implements InjectionService { // 使用 @Autowired 注解进行属性注入 @Autowired private InjectionDAO injectionDAO; public void save(String arg) { System.out.println("接受数据" + arg); arg = arg + ":" + this.hashCode(); injectionDAO.save(arg); } } ``` 在上述代码中,`InjectionServiceImpl` 类使用 `@Service` 注解将自身注册为 Spring 服务组件,通过 `@Autowired` 注解对 `injectionDAO` 属性进行注入,这样 `InjectionServiceImpl` 类就可以使用 `InjectionDAO` 实例的方法 [^4]。 #### 2. 构造器注入 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { private final UserRepository userRepository; // 使用 @Autowired 注解进行构造器注入 @Autowired public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public void saveUser(User user) { userRepository.save(user); } } ``` 这里 `UserService` 类使用构造器注入的方式,通过 `@Autowired` 注解将 `UserRepository` 实例注入到构造器中,从而在类中可以使用 `UserRepository` 的功能。 #### 3. Setter 方法注入 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ProductService { private ProductRepository productRepository; // 使用 @Autowired 注解进行 Setter 方法注入 @Autowired public void setProductRepository(ProductRepository productRepository) { this.productRepository = productRepository; } public void saveProduct(Product product) { productRepository.save(product); } } ``` `ProductService` 类使用 `@Autowired` 注解在 `setProductRepository` 方法上,实现了 `ProductRepository` 实例的注入。 #### 4. 方法参数注入 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class OrderService { public void processOrder(Order order, @Autowired PaymentService paymentService) { paymentService.processPayment(order.getAmount()); // 处理订单的其他逻辑 } } ``` 在 `OrderService` 的 `processOrder` 方法中,通过 `@Autowired` 注解注入 `PaymentService` 实例,在方法内部可以使用该实例进行支付处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值