什么是控制反转?
也就是控制权反转,获取依赖对象的过程被反转了,当需要 某个对象时,传统开发模式中需要自己通过new创建对象,现在不需要在进行创建,而是把这个创建对象的任务交给了容器. 程序只需要依赖注入(DI)就可以. 这个容器就是IoC容器.
IoC是一种思想,在生活中也有体现:
比如:传统的驾驶方式,车辆的控制权需要由驾驶员来控制,而现在就可以把驾驶权交给自动化系统来控制。
IoC是一种思想,DI是一种实现方式!
传统开发模式
设计轮⼦(Tire),然后根据轮⼦的⼤⼩设计底盘(Bottom),接着根据底盘设计⻋⾝(Framework),最后根据⻋⾝设计好整个汽⻋(Car)。这⾥就出现了⼀个"依赖"关系:汽⻋依赖⻋⾝,⻋⾝依赖底盘,底盘依赖轮⼦.
public class Main {
public static void main(String[] args) {
Car car = new Car(17);
car.run();
}
}
static class FrameWork {
private Bottom bottom;
public FrameWork(int size) {
bottom = new Bottom(size);
}
}
static class Bottom {
private Tire tire;
public Bottom(int size) {
tire = new Tire(size);
System.out.println("Bottom...init..");
}
}
static class Tire {
public int size;
private String color;
public Tire(int size) {
this.size = size;
System.out.println("tire....init...size:"+size);
}
}
从以上代码可以看出,以上程序的问题是:当最底层代码改动之后,整个调⽤链上的所有代码都需要修改.程序的耦合度⾮常⾼(修改⼀处代码,影响其他处的代码修改)
loC开发模式
把调⽤汽⻋的程序⽰例改造⼀下,把创建⼦类的⽅式,改为注⼊传递的⽅式.
public class Main {
public static void main(String[] args) {
Tire tire= new Tire(17,"red");
Bottom bottom = new Bottom(tire);
Framework framework = new Framework(bottom);
Car car = new Car(framework);
car.run();
}
}
代码经过以上调整,⽆论底层类如何变化,整个调⽤链是不⽤做任何改变的,这样就完成了代码之间的解耦,从⽽实现了更加灵活、通⽤的程序设计了.
IoC的优势
- 资源集中管理,实现资源的可配置和易管理。
- 降低了使⽤资源双⽅的依赖程度,也就是我们说的耦合度(解耦)。
IoC 的使用
Bean的存储
共有两类注解类型可以实现:
-
- 类注解:@Controller、@Service、@Repository、@Component、@Configuration.
-
- ⽅法注解:@Bean.
@Controller //把这个对象交给Spring进行管理
public class UserController {
public void hello() {
System.out.println("Hello,Controller");
}
}
获取对象:
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(IocDemoApplication.class, args);
//从Spring上下⽂中获取对象
UserController userController = context.getBean(UserController.class);
userController.hello();
}
- @Controller:控制层,接收请求,对请求进⾏处理,并进⾏响应.
- @Servie:业务逻辑层,处理具体的业务逻辑.
- @Repository:数据访问层,也称为持久层.负责数据访问操作
- @Component :组件存储.
- @Configuration:配置层.处理项⽬中的⼀些配置信息
方法注解
类注解是添加到某个类上的,但是存在两个问题:
-
- 使⽤外部包⾥的类,没办法添加类注解
-
- ⼀个类,需要多个对象,⽐如多个数据源
这种场景,我们就需要使⽤⽅法注解@Bean.
@Configuration
public class BeanConfig {
@Bean("u1")
public User user1() {
User user = new User();
user.setName("zhangsan");
user.setAge(18);
return user;
}
}
@Bean 方法注解要搭配类注解一起使用,否则就是报错!! 可以对Bean对象进行重命名操作.
DI
依赖注⼊是⼀个过程,是指IoC容器在创建Bean时,去提供运⾏时所依赖的资源,⽽资源指的就是对象.
关于依赖注⼊,Spring也给我们提供了三种⽅式:
-
- 属性注⼊
-
- 构造⽅法注⼊
-
- Setter注⼊
属性注入
属性注⼊是使⽤@Autowired注解实现的. 比如:将Service 类注⼊到 Controller类中.
@Controller //把这个对象交给Spring进行管理
public class UserController {
@Autowired
private UserService userService;
public void hello() {
System.out.println("Hello,Controller");
userService.hello();
}
}
@Service
public class UserService {
public void hello() {
System.out.println("Hello,UserService...");
}
}
构造⽅法注⼊
@Controller
public class UserController2 {
//注⼊⽅法2: 构造⽅法
private UserService userService;
public UserController2() {
}
@Autowired
public UserController2(UserService userService) {
this.userService = userService;
}
public void hi(){
System.out.println("hi,UserController2...");
userService.hello();
}
}
去掉@Autowired,就会报异常,如下:
如果类只有⼀个构造⽅法,那么,@Autowired注解可以省略;如果类中有多个构造⽅法,那么需要添加上@Autowired来明确指定到底使⽤哪个构造⽅法。
最后
tps://img-blog.csdnimg.cn/direct/46ae363392964d5893c9c8d43829b02e.png)
如果类只有⼀个构造⽅法,那么,@Autowired注解可以省略;如果类中有多个构造⽅法,那么需要添加上@Autowired来明确指定到底使⽤哪个构造⽅法。
最后
[外链图片转存中…(img-91814tjB-1714525097510)]
[外链图片转存中…(img-j8gEzGNO-1714525097510)]