Spring DIP 依赖倒置原则

springboot中文社区

一、java 基础

在Java中,定义方法返回值为接口类型,需要实现该接口的具体实现类,并在方法中返回该实现类的实例对象。

例如,定义一个方法返回值为List接口类型,可以通过以下方式实现:

public List<String> getList() {
    List<String> list = new ArrayList<>();
    // add elements to the list
    return list;
}

在这个例子中,实现了List接口的具体实现类ArrayList,并在getList方法中返回了ArrayList的实例对象。由于ArrayList实现了List接口,因此可以将其作为List类型的返回值。

当调用该方法时,可以使用List类型来接收返回值:

List<String> result = getList();

这样做的好处是,如果后续需要修改返回值的类型,只需要修改返回的实现类即可,而无需修改调用该方法的代码。同时,使用接口作为返回类型也符合面向对象编程的原则,提高了代码的可扩展性和可维护性。

二、Spring 中如何注入Bean

在Spring中,可以使用@Autowired或@Resource注解来注入返回值类型是接口的Bean。

例如,定义一个接口UserService:

    public interface UserService {
        void addUser(User user);
        User getUserById(int id);
    }

然后有一个实现类UserServiceImpl:

    @Service
    public class UserServiceImpl implements UserService {
        // ...
    }

使用@Autowired注解来注入UserService:

@Service
public class SomeService {
    @Autowired
    private UserService userService;
    // ...
}

使用@Resource注解来注入UserService:

@Service
public class SomeService {
    @Resource
    private UserService userService;
    // ...
}

需要注意的是,如果有多个实现类实现了同一个接口,需要使用@Qualifier注解来指定注入的实现类。例如:

@Service
public class SomeService {
    @Autowired
    @Qualifier("userServiceImpl2")
    private UserService userService;
    // ...
}

三、针对接口编程

针对接口编程而不是具体实现类,是一种编程思想,也是面向对象编程的核心原则之一——依赖倒置原则(DIP)。这种编程方式可以使代码更加灵活、可扩展、易于维护。

在 Java 中,可以使用接口作为方法参数、返回值或成员变量的类型,从而实现针对接口编程而不是具体实现类。下面是一个简单的例子:

假设有一个接口 Animal 和两个实现类 Dog 和 Cat:

public interface Animal {
    public void makeSound();
}

public class Dog implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Wang Wang!");
    }
}

public class Cat implements Animal {
    @Override
    public void makeSound() {
        System.out.println("Miao Miao!");
    }
}

现在我们需要一个方法来处理动物的声音,但是我们不确定传入的具体是什么动物,这时候我们可以使用 Animal 接口来作为参数类型:

public class AnimalSound {
    public void makeSound(Animal animal) {
        animal.makeSound();
    }
}

这样,无论传入的是 Dog 还是 Cat,都可以正确输出声音。

使用接口编程还有一个好处是,可以方便地实现依赖注入。比如,我们有一个使用 Animal 接口作为参数类型的类:

public class AnimalService {
    private Animal animal;

    public AnimalService(Animal animal) {
        this.animal = animal;
    }

    public void makeSound() {
        animal.makeSound();
    }
}

使用 Spring 容器进行依赖注入时,只需要将具体的实现类配置为 Bean,然后在 AnimalService 中使用 @Autowired 注解注入即可:

@Configuration
public class AppConfig {
    @Bean
    public Animal dog() {
        return new Dog();
    }

    @Bean
    public AnimalService animalService(Animal animal) {
        return new AnimalService(animal);
    }
}

这样,无论是注入 Dog 还是 Cat,都可以正确输出声音。同时,如果需要新增一种动物实现,只需要实现 Animal 接口,并将其配置为 Bean 即可,不需要修改 AnimalService 类的代码。这就体现了针对接口编程而不是具体实现类的优势。

四、具体实例

有这样一个类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
public class MySecurity {
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }
}

这段代码是一个Spring的配置类,使用@Configuration注解标记,表示这是一个Java配置文件。

其中@Bean注解用于声明一个方法,这个方法返回一个PasswordEncoder类型的对象,并且这个对象会交给Spring容器管理,可以被其他组件注入使用。

具体来说,这段代码中声明了一个名为passwordEncoder的Bean,这个Bean的类型是PasswordEncoder,返回的是一个BCryptPasswordEncoder对象。BCryptPasswordEncoder是Spring Security提供的一种密码加密工具,用于对用户密码进行加密处理。

这个Bean的作用是在Spring Security中使用加密功能时提供PasswordEncoder实例,可以通过在其他组件中使用@Autowired注解来引用这个Bean,从而使用BCryptPasswordEncoder对密码进行加密处理。

假设我们需要在一个服务类中使用 PasswordEncoder 接口,可以使用 Spring 的依赖注入来获取该接口的实例。假设服务类的名称为 MyService,注入过程如下:

import org.springframework.security.crypto.password.PasswordEncoder;

@Service
public class MyService {
    @Autowired
    private PasswordEncoder passwordEncoder;

    // 其他代码
}

已经在Spring容器中定义了一个实现PasswordEncoder接口的Bean。在代码中使用@Autowired注解进行自动注入时,Spring会自动查找容器中是否有符合类型的Bean,然后注入到对应的字段中。因此,如果MySecurity配置类中定义了一个返回值类型为PasswordEncoder的Bean,并且该类被正确扫描到并加载到Spring容器中,那么在其他需要使用该Bean的地方,可以使用@Autowired进行自动注入。

虽然PasswordEncoder是一个接口,但是在MySecurity配置类中,使用@Bean注解定义了一个passwordEncoder()方法并返回了一个实现了PasswordEncoder接口的BCryptPasswordEncoder对象。这样在Spring容器启动时会创建该对象并将其注册到容器中,因此在其他需要使用PasswordEncoder的地方,可以通过@Autowired注解将其注入进去,实际上注入的是BCryptPasswordEncoder对象。这也是Spring中面向接口编程的一个重要原则:针对接口编程而不是具体实现类。