2015-10-14 23 views
0

如果我有@Autowired道類:通@Autowired元素作爲函數的參數 - 春

@Service 
public class MyClass implements MyInterface 
{ 
    @Autowired 
    private MyDao myDao; 

//... 
private Status findStatus(final MyStatus status) 
    { 
     return status.findStatus(myDao); 
    } 
} 

而且我想在枚舉使用此道是這樣的:

public enum MyStatus implements Serializable 
{ 
    NEW("") 
      { 
       public Status findStatus(final myDao myDao) 
       { 
        return myDao.findByType(AnotherStatus.STATUS_NEW); 
       } 
      } 
//... 
} 

這是個好練習這樣做?或者我應該嘗試@AutowiredmyDaoenum再次(但很可能我會得到一個NullPointerException)?我認爲這有點奇怪 - 通過@Autowired元素作爲函數參數,但我可能是錯的。

+0

我不明白你爲什麼要在枚舉裏面找到findStatus方法。枚舉類型是一種特殊的數據類型,它使一個變量成爲一組預定義的常量 –

回答

0

只需在MyStatus中添加一些映射函數王,即可爲每個MyStatus返回適當的AnotherStatus

private Status findStatus(final MyStatus status) { 
    return myDao.findStatus(status.asAnotherStatus()); 
} 
相關問題