@ How to use Qualifier

Asked 2 years ago, Updated 2 years ago, 89 views

Using the Java Spring Framework,
Creating web applications

However, I want to register more than one Bean, but it doesn't work.

·Objective
Want to handle more than one Bean

·Error message
Consumer marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

·What I did to resolve the error
@Grant Qualifier

User.java

package com.example.demo.entity;

import java.io.Serializable;
import org.springframework.stereotype.Component;

@ Component ("User")
public class User implements Serializable {
    
    private static final long serialVersionUID = 1L;
    
    
    private int userId;
    private int pass;
    
    public int getUserId(){
        return userId;
    }
    public void setUserId(int userId){
        this.userId = userId;
    }
    
    public int getPass(){
        return pass;
    }
    public void setPass(int pass){
        This.pass = pass;
    }

}

Info.java

package com.example.demo.entity;

import java.io.Serializable;

import org.springframework.beans.factory.announcement.Qualifier;
import org.springframework.stereotype.Component;

@ Component ("Info")
public class Information Serializable {
    
    private static final long serialVersionUID = 1L;
    
    private intexamId;
    private String exampleName;
    private String exampleDate;
    
    public int getExamId(){
        return exampleId;
    }


    public void setExamId(intexamId){
        This.examId =examId;
    }


    public void setExamName(StringexamName){
        This.examName=examName;
    }

    public void setExamDate(StringexamDate){
        This.examDate=examDate;
    }

    public String getExamName(){
        return exampleName;
    }

    public String getExamDate(){
        return exampleDate;
    }

}

ServiceUserImpl.java

package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.announcement.Autowired;
import org.springframework.beans.factory.announcement.Qualifier;
import org.springframework.stereotype.Service;

import com.example.demo.dao.UserDao;
import com.example.demo.entity.Info;
import com.example.demo.entity.User;

@Service
public classServiceUserImplementationsTestService{
    
    @Autowired
    UserDao;
    
    @Autowired
    @ Qualifier ("User")
    User user;
    

    @ Override
    publicList selectWhere(Objectt){
        
        User = (User)t;
        
        List<User>list=dao.selectWhere(u);
        
        return list;
        
    
    }
}

ServiceExamInfoImpl.java

package com.example.demo.service;

import java.util.List;

import org.springframework.beans.factory.announcement.Autowired;
import org.springframework.beans.factory.announcement.Qualifier;
import org.springframework.stereotype.Service;

import com.example.demo.dao.ExamInfoDao;
import com.example.demo.entity.Info;

@Service
public classServiceExamInfoImplementationsTestService{
    
    @Autowired
    ExamInfoDao;
    
    @Autowired
    @ Qualifier ("Info")
    Info info;
    
    

    @ Override
    publicList selectWhere(Objectt){
        
        Info info=(Info)t;
        
        List<Info>list=dao.selectWhere(info);
        
        return list;
        
    }

java spring spring-boot

2022-09-30 11:24

1 Answers

It is important to know which bean the error message in the question statement appears for.Just before the error message appears in the question text, you will also see what the problem is.

If the code in the question is possible, you may have tried to auto-wire the TestService, but in this case, for example, @Qualifier should be given as follows:

@RestController
public class MyController {

    @Autowired
    @ Qualifier ("serviceUserImpl")
    private TestService testService;
...

Supplement:

The default qualifier value is bean name, and the default bean name is lowercase at the beginning of the class name.
In other words, if you do not specify anything, the ServiceUserImpl qualifier value is serviceUserImpl.

1.9.4. Fine-tuning Annotation-based Autowiring with Qualifiers

For a fallback match, the bean name is considered a default qualifier value.

1.10.6. Naming Autodetected Components

If such an announcement contains no name value or for any other detected component (such as these discovered by custom filters), the default bean name generator returns the uncapacitated non-qualified class name.


2022-09-30 11:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.