spring的依赖注入

spring自动注入的方式

xml方式

手动:

  • set方法注入
<bean name ="orderService" class ="com.zzy.service.OrderService">          
</bean>

<bean name ="userService" class ="com.zzy.service.UserService">
    <property name="orderService" ref= "orderService"></property>     
</bean>
  • 构造器注入
<bean name ="orderService" class ="com.zzy.service.OrderService">          
</bean>

<bean name ="userService" class ="com.zzy.service.UserService">
    <constructor-arg index="0" ref= "orderService"/>     
</bean>
  • p标签注入
不常用略

自动注入:

<bean name ="orderService" class ="com.zzy.service.OrderService">          
</bean>
<!--autowire  默认五种方式(ByType,ByName,default,no,constructor),
byType:根据类型去容器寻找
ByName:找set方法,去除Set后的名称去容器中寻找
no:默认
-->
<bean name ="userService" class ="com.zzy.service.UserService" autowire = "byType"> 
</bean>

spring注解方式(常用)

  • 属性注入
  • 方法注入
  • 构造器注入

直接看代码

package com.zzy.zzyNotes.spring.DI依赖注入;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @author: zzy
 * @createDate: 2022-01-07 15:28
 * @description:
 * spring依赖注入的方式
 * 1.属性注入
 * 2.方法注入
 * 3.构造器注入
 *  @Autowired  先根据Type,在根据名字注入,可以放在属性.普通方法,构造方法上
 *
 *  @Resource  根据名字注入,可以放在属性.普通方法上
 *
 *  @Qualifier  指定注入Bean的名字,可以放在属性.普通方法(单参数),普通方法形参前,构造方法形参前
 *
 *  @Primary  相同类型的Bean的优先级最高
 */
@Service
public class UserService {

    //一:属性注入 -------------------------------------------------------------------------------------------------------
    //接口
    @Autowired                         //@Autowired先根据Type,在根据名字
    @Qualifier("ticketsOrderService")  //指定寻找的名字
    private OrderService orderService;
    //类
    @Autowired                         //@Autowired先根据Type,在根据名字
    private GoodsOrderService goodsOrderService;


    //二:方法注入不一定是set方法) -----------------------------------------------------------------------------------------
    //使用方法注入
    //这个我们想指定注入水果Bean
    //方式有三种
    //1.@Primary FruitsOrderService上加@Primary
    //2.@Autowired 配合 @Qualifier("fruitsOrderService") @Qualifier可以加在参数前面
    //2.@Resource(name = "fruitsOrderService") 根据name注入
    private OrderService fruitsOrderService;

    private OrderService goodsOrderService2;

    @Autowired
    //@Resource(name = "fruitsOrderService")
    public void fruits(OrderService orderService,GoodsOrderService goodsOrderService) {
        this.fruitsOrderService = orderService;
        this.goodsOrderService2 = goodsOrderService;
    }


    //三:构造方法注入            -----------------------------------------------------------------------------------------
    //构造方法注入meatOrderService,啥也不加会注入fruitsOrderService,因为@Primary.需要用@Qualifier("meatOrderService")指定
    private OrderService meatOrderService;

    @Autowired
    public UserService( OrderService orderService) {
        this.meatOrderService = orderService;
    }

    public UserService() {

    }
    /**
     * 查询注入的Bean
     */
    public void getBean() {
        System.out.println(orderService);
        System.out.println("属性注入 --------");
        System.out.println(goodsOrderService);
        System.out.println("属性注入 --------");
        System.out.println(fruitsOrderService);
        System.out.println("方法注入 --------");
        System.out.println(goodsOrderService2);
        System.out.println("方法注入 --------");
        System.out.println(meatOrderService);
        System.out.println("构造器注入 --------");
    }

}

spring @Autowired自动注入的流程

spring自动注入的流程(简)
添加了@Autowired的会被标记为注入点,注入点两种
1.属性
2.方法
遍历所有的注入点
先通过Type,在根据Name寻找容器中的Bean,找不到就创建,找到后通过反射给Bean的属性赋值.静态属性不会赋值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值