2013-05-09 70 views
0

我有一個工廠類,我不知道是否有可能將AnimalMapper工廠類注入到其他需要它的bean中?向其他豆注入工廠類

AnimalMapper工廠類

public static Mapper create(final String type) { 
    if (type.equalsIgnoreCase("dog")) { 
     return new DogMapper(); 
    } else if (type.equalsIgnoreCase("cat")) { 
     return new CatMapper(); 
    } ... 
} 

目前我使用AnimalMapper.create(...)

+3

我對這裏的問題完全不清楚。如果你想注入映射器,那就這樣做。什麼阻止你? – 2013-05-09 14:54:28

+2

僅供參考,更好的設計可能是使用'enum'作爲'type' – 2013-05-09 14:55:33

+0

另外我不明白爲什麼要使用公共靜態工廠方法注入某些東西。 – 2013-05-09 15:00:21

回答

0

下面是示例代碼

​​

我不知道,如果你正在尋找這一點,但如果你能澄清問題更多,然後可以添加更多的細節。

0

你想實現什麼?事情是這樣的與CDI:

public class TestClass{ 

    @Inject @Any 
    Instance<Mapper> mapper; 

    public void myMethod(){ 
     if(isCat()){ 
      mapper.select(new AnnotationLiteral<Cat>(){}).get(); 
     } 

     if(isDog()){ 
      mapper.select(new AnnotationLiteral<Dog>(){}).get(); 
     } 
    } 

} 

@Dog 
public class DogMapper implements Mapper... 

@Cat 
public class CatMapper implements Mapper...