IOC DI

一.@Autowired

自动装配(可以用在字段、构造、方法、参数)

匹配方式:

先根据类型匹配再根据名称匹配,如果匹配不到就报错

未匹配解决方案:

1.设置@Autowired对应的字段名

2.通过@Qualifier(名称) 指定具体对象名

3.设置@Autowired(required=false),代表非必须字段,不匹配(一个都没有时)就为null

自动装配一定要使用@Autowried吗?

1.@Bean方法的参数会自动装配

2.构造方法的参数会自动装配

二.@Resource

Idea使用@Autowired时会出现警告

@Resource是由JDK提供的,更推荐使用

匹配方式:

先根据名称匹配,再根据类型匹配

三.@Value

设置字段值,用于字段、构造参数上(必须是bean,@Value才生效)

1.直接值

package com.gok.iocdi.student;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Student {
    @Value("张三")
    private String name;
    @Value("18")
    private Integer age;
}

2.读取配置文件值

package com.gok.iocdi.student;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Student {
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private Integer age;
    @Value("${student.score:100}") //使用:指定默认值
    private Integer score;
}

读取的配置值不存在会报错

application.properties文件默认使用ISO-8859-1编码,SpringBoot项目整体使用UTF-8,所以application.properties文件中使用中文会乱码,之后会使用.yml文件代替.properties文件

3.复杂类型(SPEL表达式)

package com.gok.iocdi.student;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class Student {
    @Value("#{1 + 1}") //使用#{}执行运算
    private Integer count;
    @Value("#{{'语文':'90', '数学':'100'}}") //map数据
    private Map<String, Integer> scores;
    @Value("#{'唱歌,跳舞'}") //List数据
    private List<String> hobbies;
    @Value("#{${student.age} * 2}") //#{}搭配${}
    private Integer age;
}

### Inversion of Control (IoC) Inversion of Control是一种设计思想,强调的是控制权的转移。传统上,在代码中直接操控对象的方式使得程序内部紧密耦合。而采用IoC之后,这种操作交给了外部容器来管理对象及其生命周期[^1]。 ```java // 未使用IoC前的例子 public class Service { private Repository repository; public Service() { this.repository = new DatabaseRepository(); // 对象A主动创建B } } ``` ### Dependency Injection (DI) 依赖注入是实现IoC的一种具体方式。通过这种方式,对象不再需要自己寻找或构建所依赖的服务,而是这些服务会在运行时自动提供给该对象,从而降低了对象间的耦合度。这可以通过构造函数、工厂方法或是Setter方法完成[^2]。 ```java // 使用DI后的例子 public class Service { private final Repository repository; @Autowired // 或者其他形式的标注表明这是一个待注入的属性 public Service(Repository repository) { this.repository = repository; // 对象A被动接收B } } ``` ### Aspect-Oriented Programming (AOP) 面向切面编程旨在解决横切关注点的问题——那些影响多个类的功能(如日志记录、事务处理),但又不适合嵌入到业务逻辑中的部分。借助于Spring框架的支持,可以将这类通用功能抽取出来作为独立模块,并在不改变原有代码结构的情况下应用到各个地方[^3]。 ```xml <!-- AOP配置片段 --> <aop:config> <aop:aspect ref="loggingAspect"> <aop:before pointcut="execution(* com.example.service.*.*(..))" method="logBefore"/> </aop:aspect> </aop:config> ``` ### 概念间的关系 - **DI** 则是在此基础之上进一步细化了如何获取所需资源的具体手段; - 而 **AOP** 可以看作是对上述两者能力的一个补充扩展,专注于分离跨领域问题并将其集中处理[^4]。 ### 区别 | 特征 | IoC | DI | AOP | |---------------| | 主要作用 | 控制反转 | 依赖注入 | 处理横向切割的关注点 | | 影响范围 | 整体架构层面 | 类级别 | 方法调用前后的行为 | | 实现机制 | 容器接管对象创建 | 构造器/设值等方式传递依赖 | 编译期织入或代理技术 |
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值