Featured image of post Spring IOC DI 学习

Spring IOC DI 学习

Springboot 学习时的理解

Spring IOC DI 学习

最近在海底捞上班搞运维,但是我也没有放下我的Java开发学习,今天下班早点,我就又开始学习Springboot

因为去年写的wh项目太拉胯啦,有好多东西都是一知半解,只是知道某一个方法应该怎么用,但是为什么要这么使用,其中的道理我还是不太明白,虽然之前我也是写过两篇Springboot开发中的知识,但是现在显而易见,我已经把他们给忘记啦。所以现在再拉出来鞭尸一遍巩固学习的知识。

IOC简介

ioc [Inversion of Control] 控制反转,具体的官话我也说不出来,反正我个人的理解就是:

不需要我这个小垃圾来管理Java bean啦,我不需要亲自的new对象了,这些事情都交给Springboot来做。总的来说就是我解放了。

IOC是什么用?

众所周知,Springboot是注解式开发,那么IOC肯定是配合着注解来使用的。

假如,现在我有一个项目,里面有一个pojo package,里面有一个unix类如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package top.jokeme.funny.pojo;

import org.springframework.stereotype.Component;

@Component
public class unix {
    private Integer year;
    private String name;
    private Boolean unix;
  
    @Override
    public String toString() {return "unix{" +"year=" + year +", name='" + name + '\'' +", unix=" + unix +'}';
    }

    public Integer getYear() {return year;}

    public void setYear(Integer year) {this.year = year;}

    public String getName() {return name;}

    public void setName(String name) {this.name = name;}

    public Boolean getUnix() {return unix;}

    public void setUnix(Boolean unix) {this.unix = unix;}
}

可以看到我加了一个注解:@Component

然后我的server package下面有:interfacehalopackageimpl 、impl下面有一个halo的实现类:haloimpl

halo内容如下:

1
2
3
4
5
package top.jokeme.funny.service;

public interface halo {
    public String sayhalo ();
}

haloimpl内容如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
package top.jokeme.funny.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.jokeme.funny.pojo.unix;
import top.jokeme.funny.service.halo;

@Service
public class haloimpl implements halo {

    @Autowired
    private unix unix;

    @Override
    public String sayhalo() {
        unix.setUnix(true);
        unix.setYear(1973);
        unix.setName("Unix_like Linux_Ubuntu_20.04 Build 2021 03 06 13:09:35");
        return unix.toString();
    }
}

可以看的出来,这里最重要的注解就是@Autowired ,我们没有new对象,但是在加了注解以后就可以直接调用方法了。

如果你愿意加上一个controller,那你就可以在网页上查看你写的数据!

controller如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
package top.jokeme.funny.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.jokeme.funny.service.halo;

@Controller
public class retUnix {
    @Autowired
    private halo haloimpl;

    @ResponseBody
    @RequestMapping("getunix")
    public String getunix (){
        return haloimpl.sayhalo();
    }
}

可以看到,万众瞩目的注解 @Autowired 依然在这里发光发热,我们只需要调用接口halo,就有一个实现类被实例化了。

如果此时,你再一不小心运行啦该代码,并且又一不小心的访问web页面,你就会发现,你写的东西已经被成功的返回啦!

我的疑惑

问题一

为什么同样在/下面,unix类需要加上注解 @Component 才能被Springboot接管,而haloimpl没有加 @Component 就可以被Springboot接管呢?或者通俗的说就是haloimpl可以通过 @Autowired获取unix类的原因我知道,是因为unix加了 @Component 注解,但是 haliimpl 没有加注解为什么没有加 @Component 也可以被retUnix 获取到呢?

我个人现在也觉得这个问题很无聊,因为haloimpl 有一个 @Service 注解。

再来给自己解释一下注解:

@Component:加上该注解就表示当前类需要被Spring的容器管理; @Service:加上该注解就表示当前类需要在业务逻辑类当中使用。 @Repositorty: 加上该注解就表示当前类在数据访问层使用 @Controller:加上该注解就表示当前类在展现层(MVC)使用

问题二

为什么加了 @Autowired 参数以后就可以直接调用方法了?

其实吧加注解这一步就是叫 DI 也就是依赖注入,@Component@Autowired 是成对出现的,除非你让Spring管理你的类,你自己再手动new。我想正常开发没个十年脑血栓不会这么干吧!

文章部分内容参考:所念皆山海