将@Autowired元素作为函数参数传递 - Spring

时间:2015-10-14 10:56:14

标签: java spring enums autowired

如果我的班级有.controller("ChapterListCtrl", ['$scope', 'VideoPlayerModel', function($scope, VideoPlayerModel) { $scope.chapters = VideoPlayerModel.chapters; $scope.$watch(function() { return VideoPlayerModel.chapters; }, function(newValue, oldValue){ $scope.chapters = newValue; console.log("A Change"); // Only runs at initialisation. }); }]) Dao:

@Autowired

我想在这样的枚举中使用这个Dao:

@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); } } //... } @Autowired myDao(但我可能会得到enum)?我认为这有点奇怪 - 将NullPointerException元素作为函数参数传递,但我可能错了。

1 个答案:

答案 0 :(得分:0)

只需在MyStatus中添加一些映射函数,即为每个AnotherStatus返回适当的MyStatus

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