简体   繁体   English

带有自定义类的 Autowire Map 并使用别名来获取正确的对象引用

[英]Autowire Map with custom class and use aliases to get the correct object reference

I have the following code:我有以下代码:

public interface PolicyDetailsService {
    public void display();
}

Following are the implementations以下是实现

@Service("PolicyDetailsServiceImpl")
public class PolicyDetailsServiceImpl implements PolicyDetailsService {

    @Override
    public void display() {
        System.out.println("PolicyDetailsServiceImpl displayed");
    }

}

public class PreneedPolicyDetailsServiceImpl implements PolicyDetailsService {

    @Override
    public void display() {
        System.out.println("PreneedPolicyDetailsServiceImpl displayed");
        
    }

}

public class PreneedTrustPolicyDetailsServiceImpl implements PolicyDetailsService {

    @Override
    public void display() {
        System.out.println("PreneedTrustPolicyDetailsServiceImpl displayed");
        
    }

}

I am creating the objects by creating beans like the following,我通过创建如下的 bean 来创建对象,

@Bean(name = { "AGENCY-PLI", "AGENCY-PMM" })
public PolicyDetailsServiceImpl getPolicyDetailsServiceImpl(
        @Autowired PolicyDetailsServiceImpl policyDetailsServiceImpl) {

    return policyDetailsServiceImpl;
}

@Bean(name = { "AFS-AFS", "AFS-PMM" })
public PreneedPolicyDetailsServiceImpl getPreneedPolicyDetailsServiceImpl() {
    return new PreneedPolicyDetailsServiceImpl();
}

@Bean({ "AFS-PMT" })
public PreneedTrustPolicyDetailsServiceImpl getPreneedTrustPolicyDetailsServiceImpl() {
    return new PreneedTrustPolicyDetailsServiceImpl();
}

The controller where i am using the services,我使用服务的控制器,

@Autowired
Map<String, PolicyDetailsService> policyDetails;

@GetMapping("/display/{displayName}")
public String display(@PathVariable String displayName) {
    
    try {
        policyDetails.get(displayName).display();
    }catch(Exception e) {
        e.printStackTrace();
    }
    
    return "Display Succesfull";
}

What i want to acheive: If I pass either "AGENCY-PLI" or "AGENCY-PMM" in the REST service call, the map should return PolicyDetailsServiceImpl object.我想要实现的目标:如果我在 REST 服务调用中传递“AGENCY-PLI”或“AGENCY-PMM”,则地图应返回 PolicyDetailsS​​erviceImpl 对象。 It is currently working for "AGENCY-PLI".它目前正在为“AGENCY-PLI”工作。 On passing "AGENCY-PMM" i am getting NullPointerException as the map is not returing any object.在传递“AGENCY-PMM”时,我收到 NullPointerException,因为地图没有返回任何对象。 My understanding is, the "AGENCY-PLI" is acting as a primary name and the other names are aliases.我的理解是,“AGENCY-PLI”充当主要名称,其他名称是别名。 So if i used it like the following,所以如果我像下面这样使用它,

@Autowired
@Qualifier("AGENCY-PMM")
private PolicyDetailsService policyDetailsService;

I would get the correct object reference.我会得到正确的对象引用。 So, my question: Is there a way i can get the object from the map on passing aliases.所以,我的问题是:有没有办法在传递别名时从地图中获取对象。

Actually I think the solution can be done without holding a map.其实我觉得不用拿地图就可以解决。 Here is a possible solution you can try.这是您可以尝试的可能解决方案。

@Autowired
ApplicationContext applicationContext;

@GetMapping("/display/{displayName}")
public String display(@PathVariable String displayName) {
    
    try {
        PolicyDetailsService service = applicationContext.getBean(displayName, PolicyDetailsService.class);
        service.display();
    }catch(Exception e) {
        e.printStackTrace();
    }
    
    return "Display Succesfull";
}

In addition, I am wondering why you create a PolicyDetailsServiceImpl bean using @Service and another using @Bean , is it a typo?另外,我想知道为什么你使用@Service创建一个PolicyDetailsServiceImpl bean 而另一个使用@Bean ,这是一个错字吗? In case you would like to create one Singleton, I think you better remove the @Service in PolicyDetailsServiceImpl and add the name in @Bean(name=xxx)如果您想创建一个单例,我认为您最好删除PolicyDetailsServiceImpl中的@Service并在@Bean(name=xxx)添加名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM