Spring指定注入哪个子类

不及物动词 其他 23

回复

共3条回复 我来回复
  • fiy的头像
    fiy
    Worktile&PingCode市场小伙伴
    评论

    在Spring中,可以使用@Qualifier注解来指定要注入的子类。@Qualifier注解可以与@Autowired或@Inject注解一起使用。

    假设有一个接口定义如下:

    public interface Animal {
        void sound();
    }
    

    接下来有两个子类实现了该接口:

    @Component
    @Qualifier("cat")
    public class Cat implements Animal {
        @Override
        public void sound() {
            System.out.println("Meow!");
        }
    }
    
    @Component
    @Qualifier("dog")
    public class Dog implements Animal {
        @Override
        public void sound() {
            System.out.println("Woof!");
        }
    }
    

    现在我们想要在另一个类中注入其中一个子类。可以使用@Autowired或@Inject注解,结合@Qualifier注解来指定具体要注入的子类。

    @Component
    public class AnimalService {
        private final Animal animal;
    
        @Autowired
        public AnimalService(@Qualifier("cat") Animal animal) {
            this.animal = animal;
        }
    
        public void makeSound() {
            animal.sound();
        }
    }
    

    在上面的例子中,AnimalService类中有一个Animal类型的成员变量,通过@Autowired注解和@Qualifier("cat")注解来指定要注入的是Cat类。

    这样,当使用AnimalService类的实例时,Spring会自动将Cat类的实例注入给animal成员变量,从而可以调用Cat类的sound()方法。

    如果想要注入Dog类的实例,只需将@Qualifier注解的值改为"dog"即可。

    总结:通过@Qualifier注解可以在Spring中指定注入哪个子类,可以配合@Autowired或@Inject注解使用。

    1年前 0条评论
  • 不及物动词的头像
    不及物动词
    这个人很懒,什么都没有留下~
    评论

    在Spring中,可以通过使用注解来指定注入哪个子类。以下是几种常见的方式:

    1. @Primary注解:
      @Primary注解可以用于在多个实现类中指定优先注入哪个子类。当存在多个实现类时,Spring会根据@Primary注解来确定优先注入哪个实现类。在使用@Autowired或@Inject注解时,如果没有指定具体的实现类,Spring会自动选择被@Primary注解标记的实现类进行注入。
    public interface Animal {
        String getName();
    }
    
    @Service
    @Primary
    public class Cat implements Animal {
        @Override
        public String getName() {
            return "Cat";
        }
    }
    
    @Service
    public class Dog implements Animal {
        @Override
        public String getName() {
            return "Dog";
        }
    }
    
    @Service
    public class AnimalService {
        private final Animal animal;
    
        @Autowired
        public AnimalService(Animal animal) {
            this.animal = animal;
        }
    
        public void printAnimalName() {
            System.out.println(animal.getName());
        }
    }
    

    在上述示例中,如果没有指定具体的实现类,Spring会默认注入Cat实现类。当然,也可以通过指定具体的实现类来进行注入。

    1. @Qualifier注解:
      @Qualifier注解可以结合@Autowired或@Inject注解来指定注入的具体实现类。通过在@Qualifier注解中指定实现类的名称,可以确保具体注入哪个子类。
    public interface Animal {
        String getName();
    }
    
    @Service
    public class Cat implements Animal {
        @Override
        public String getName() {
            return "Cat";
        }
    }
    
    @Service
    @Qualifier("dog")
    public class Dog implements Animal {
        @Override
        public String getName() {
            return "Dog";
        }
    }
    
    @Service
    public class AnimalService {
        private final Animal animal;
    
        @Autowired
        public AnimalService(@Qualifier("dog") Animal animal) {
            this.animal = animal;
        }
    
        public void printAnimalName() {
            System.out.println(animal.getName());
        }
    }
    

    在上述示例中,AnimalService类中的构造函数中使用了@Qualifier注解来指定注入Dog实现类。

    1. 使用Java配置:
      在Java配置中,可以通过编写@Configuration和@Bean的方式来指定注入哪个子类。
    public interface Animal {
        String getName();
    }
    
    @Service
    public class Cat implements Animal {
        @Override
        public String getName() {
            return "Cat";
        }
    }
    
    @Service
    public class Dog implements Animal {
        @Override
        public String getName() {
            return "Dog";
        }
    }
    
    @Configuration
    public class AppConfig {
        @Bean
        public Animal animal() {
            return new Dog();
        }
    }
    
    @Service
    public class AnimalService {
        private final Animal animal;
    
        @Autowired
        public AnimalService(Animal animal) {
            this.animal = animal;
        }
    
        public void printAnimalName() {
            System.out.println(animal.getName());
        }
    }
    

    在上述示例中,通过在AppConfig类中的@Bean注解中返回具体的实现类来指定注入哪个子类。

    1. 使用XML配置:
      除了使用Java配置,还可以使用XML配置的方式来指定注入哪个子类。
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="cat" class="com.example.Cat"></bean>
        <bean id="dog" class="com.example.Dog"></bean>
    
        <bean id="animalService" class="com.example.AnimalService">
            <constructor-arg>
                <ref bean="cat"/>
            </constructor-arg>
        </bean>
    </beans>
    

    在上述示例中,通过在animalService的constructor-arg标签中指定ref为cat来指定注入Cat实现类。

    总结起来,Spring提供了多种方式来指定注入哪个子类,包括使用@Primary注解、@Qualifier注解、Java配置和XML配置。根据具体场景和需求,可以选择适合的方式来实现子类的注入。

    1年前 0条评论
  • worktile的头像
    worktile
    Worktile官方账号
    评论

    在Spring中,当存在一个接口或抽象类有多个的实现类时,可以使用@Qualifier注解来指定注入哪个子类。

    在使用@Qualifier注解时,首先需要给每个子类定义一个唯一的限定符(Qualifier),通过在子类中添加@Qualifier注解并指定限定符的值。例如:

    @Component
    @Qualifier("subClassA")
    public class SubClassA implements MyInterface {
        //...
    }
    
    @Component
    @Qualifier("subClassB")
    public class SubClassB implements MyInterface {
        //...
    }
    

    然后,在注入的地方使用@Qualifier注解并指定相应的限定符,告诉Spring需要注入哪个子类。例如:

    @Autowired
    @Qualifier("subClassA")
    private MyInterface myInstance;
    

    除了@Qualifier注解,还可以使用@Resource注解来指定注入哪个子类。与@Qualifier注解类似,首先需要给每个子类定义一个唯一的名称,然后在注入的地方使用@Resource注解并指定相应的名称。例如:

    @Component("subClassA")
    public class SubClassA implements MyInterface {
        //...
    }
    
    @Autowired
    @Resource(name = "subClassA")
    private MyInterface myInstance;
    

    另外,如果子类的实现类名称与接口名称的首字母大小写不同,可以使用@Qualifier注解或@Resource注解指定注入的实现类。例如:

    public interface MyInterface {
        //...
    }
    
    @Component
    @Qualifier("subClassA")
    public class SubClassA implements MyInterface {
        //...
    }
    
    @Autowired
    @Qualifier("subClassA")
    private MyInterface myInstance;
    

    或者

    public interface MyInterface {
        //...
    }
    
    @Component("subClassA")
    public class SubClassA implements MyInterface {
        //...
    }
    
    @Autowired
    @Resource(name = "subClassA")
    private MyInterface myInstance;
    

    通过以上方法,可以在Spring中指定注入哪个子类。

    1年前 0条评论
注册PingCode 在线客服
站长微信
站长微信
电话联系

400-800-1024

工作日9:30-21:00在线

分享本页
返回顶部