2012-01-27 213 views
0

我試圖創建一個簡單的網頁,這是顯示數據,如何初始化網絡服務的網絡服務接收

@Service 
@Transactional 
public class TopicService { 
    @Autowired 
    private TopicRepository topicRepository; 

    public int saveTopic(Topic topic){ 
     return topicRepository.save(topic).getId(); 

     } 

    public Iterable<Topic> findAllTopics(){ 
     return topicRepository.findAll(); 

    } 

    public Topic findTopicByID(Long id){ 
     return topicRepository.findOne(id); 
    } 

    public List<Topic> findTopicsByTag(Tag tag){ 
     return topicRepository.findAllByTopicTag(tag); 
    } 

} 

主題庫擴展CRUD存儲庫

@Repository 
public interface TopicRepository extends CrudRepository<Topic, Long> 
{ 
    List<Topic> findAllByTopicTag(Tag currentTag); 

} 

控制器以下述方式

@Controller 
public class HomeController { 

    private TopicService service; 

    private static final Logger logger = LoggerFactory.getLogger(HomeController.class); 

    @RequestMapping(value = "/", method = RequestMethod.GET) 
    public ModelAndView home(Locale locale, Model model) 
    { 
     Iterable<Topic> listTopic = service.findAllTopics(); 
     return new ModelAndView("home", "model", listTopic); 
} 
} 

此字符串

01調用一個服務

Iterable listTopic = service.findAllTopics();

引發空指針異常。我想,因爲服務沒有初始化。我怎樣才能執行正確的服務初始化?

回答

1

你需要自動裝配的TopicServiceHomeController

@Controller 
public class HomeController { 

    @Aurowired 
    private TopicService service; 
+0

謝謝,非常 – Eugene 2012-01-27 12:26:44