Spring

浅谈IOC编程思想

2020-10-21  本文已影响0人  AbstractCulture

IOC简介

IOC是Inversion of Control的缩写,也叫控制反转
为什么要有控制反转,这是因为软件设计的原则是"高内聚、低耦合",为了解耦,工业内使用了很多手段,如现在如火如荼的微服务架构,就是从软件架构去解耦合来达到性能提高和业务切分的目的。在每个SpringBoot的应用中,我们也会用到IOC来解决对象与对象之间的耦合问题。(需求每时每刻都有可能变化,我们设计的系统要去拥抱变化,所以要保持扩展性)

牵一发而动全身

如果没有IOC,实现一个删除用户的需求要怎么写

public class BeforeSpring {

    public static void main(String[] args) {
        MySQLConfig mySQLConfig = new MySQLConfig();
        mySQLConfig.url = "jdbc:mysql://127.0.0.1:3306/user";
        mySQLConfig.userName = "root";
        mySQLConfig.password = "root";
        UserMapper userMapper = new UserMapper(mySQLConfig);
        UserService userService = new UserService(userMapper);
        UserController userController = new UserController(userService);
        userController.deleteUserById("1");
    }

    public static class UserController{
        public UserService userService;

        public UserController(UserService userService){
            this.userService = userService;
        }

        public void deleteUserById(String id){
            userService.deleteUserById(id);
        }
    }

    public static class UserService{
        public UserMapper userMapper;

        public UserService(UserMapper userMapper){
            this.userMapper = userMapper;
        }

        public void deleteUserById(String userId){
            userMapper.deleteUser(userId);
        }
    }

    public static class UserMapper {
        private MySQLDataSource dataSource;

        public UserMapper(MySQLConfig mySQLConfig) {
            this.dataSource = new MySQLDataSource(mySQLConfig);
        }

        public void deleteUser(String userId) {
            Connection connection = dataSource.getConnection();
            try {
                Statement stmt = connection.createStatement();
                stmt.execute("DELETE FROM USER WHERE user_id = " + userId);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public static class MySQLConfig{
        private String url;
        private String userName;
        private String password;
    }

    public static class MySQLDataSource {
        private String url;
        private String userName;
        private String password;
        private Connection connection;

        public MySQLDataSource(MySQLConfig config) {
            this.url = config.url;
            this.userName = config.userName;
            this.password = config.password;
            try {
                //1.加载驱动程序
                Class.forName("com.mysql.jdbc.Driver");
                //2. 获得数据库连接
                connection = DriverManager.getConnection(config.url, config.userName, config.password);
            } catch (ClassNotFoundException | SQLException e) {
                System.out.println("异常");
            }
        }

        public Connection getConnection(){
            return this.connection;
        }
    }
}

这样写代码容易引发几个问题:

IOC,便是这个答案。

IOC的核心思想

上面的例子我们看到,大量存在依赖关系的对象通过new进行了实例化,调用者必须要主动去new并且理清楚所有的下层对象初始化的过程才能进行逻辑处理。
怎么做才合理呢?每个层级之间各司其职,将自己要处理的事情做好,然后交由第三方容器进行管理。容器根据这些对象的配置自动进行装配,其中需要用到依赖注入(Dependency Injection)。
我们在需要某个实例的时候,我们告诉IOC,我们需要什么对象即可,由IOC帮我们装配好对象然后通过"注入"的方式给到我们,这个方式可以是Setter、Constructor、Interface、Annotation。(例子:Spring工程中的@Autowired@Resource)
那么我们来解答控制反转是什么进行了反转?——我们原本需要自己创建对象,现在我们将这个工作交给IOC来做。由IOC来创建了对象。
一个简单的生活中的例子:

依赖倒转前:

before.png

依赖倒转后:

after.png

一个编码中的例子:
我需要使用apache的包进行开发,但是我觉得"下载"特别麻烦,所以我让Maven帮我去"下载"这个包,我只需要在pom文件中将仓库地址和想要的Jar版本告诉Maven即可。

上一篇 下一篇

猜你喜欢

热点阅读