2017-01-15 59 views
7

我得到的錯誤,當我運行的主類。考慮您的配置定義類型「服務」的豆[春季啓動]

錯誤:

Action: 
Consider defining a bean of type 'seconds47.service.TopicService' in your configuration. 

Description: 
Field topicService in seconds47.restAPI.topics required a bean of type 'seconds47.service.TopicService' that could not be found 

TopicService接口:

public interface TopicService { 

    TopicBean findById(long id); 

    TopicBean findByName(String name); 

    void saveTopic(TopicBean topicBean); 

    void updateTopic(TopicBean topicBean); 

    void deleteTopicById(long id); 

    List<TopicBean> findAllTopics(); 

    void deleteAllTopics(); 

    public boolean isTopicExist(TopicBean topicBean); 
} 

控制器:

@RestController 
public class topics { 

    @Autowired 
    private TopicService topicService; 

    @RequestMapping(path = "/new_topic2", method = RequestMethod.GET) 
    public void new_topic() throws Exception { 
     System.out.println("new topic JAVA2"); 
    } 
} 

實現類:

public class TopicServiceImplementation implements TopicService { 

    @Autowired 
    private TopicService topicService; 

    @Autowired 
    private TopicRepository topicRepository; 

    @Override 
    public TopicBean findById(long id) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public TopicBean findByName(String name) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public void saveTopic(TopicBean topicBean) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public void updateTopic(TopicBean topicBean) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public void deleteTopicById(long id) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public List<TopicBean> findAllTopics() { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public void deleteAllTopics() { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 

    @Override 
    public boolean isTopicExist(TopicBean topicBean) { 
     throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 
    } 
} 

其餘的類也被定義。儘管在主類中宣佈了componentScan,但我不知道爲什麼投擲。

主要類:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class }) 
@ComponentScan(basePackages = {"seconds47"}) 
@EnableJpaRepositories("seconds47.repository") 
public class Application { 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

我有我的包是這樣的:

seconds47 
seconds47.beans 
seconds47.config 
seconds47.repository 
seconds47.restAPI 
seconds47.service 
+1

請問類'TopicServiceImplementation'有註釋'@ Component'?如果沒有,你必須將它添加到類中,以便Spring可以將它識別爲一個bean。 – dunni

+1

@dunni春天開機,'@不需要Component'註釋自動受到springboot用SpringMVC不同 – kittu

+0

如果沒有在類中添加任何註釋掃描,它不被掃描,因爲你還沒有將其標記爲一個豆。如果你不相信我,請閱讀文檔。 – dunni

回答

10

一個類必須具有@Component註釋或派生(例如@Service,@Repository等)才能被組件掃描識別爲Spring bean。所以如果你將@Component添加到課程中,它應該可以解決你的問題。

+0

即使我用@Component註釋了該類, –

1

您試圖在自身注入一個bean。這顯然不起作用。

TopicServiceImplementation實現TopicService。該類嘗試自動裝入(通過字段!)一個`TopicService。所以你基本上要求上下文注入自己。

它看起來像你編輯的錯誤消息的內容:Field topicService in seconds47.restAPI.topics是不是一類。如果您需要隱藏敏感信息,請小心,因爲這會讓別人更難以幫助您。

回到實際問題,它看起來像注射TopicService本身就在你身邊一個小故障。

+0

當你說'我正在注入一個bean本身'時,我不明白一件事情嗎?我正在注入'TopicService',這是一個控制器類'Topics'的接口。 – kittu

+0

我沒有編輯錯誤信息。我剛剛編輯了我發佈的問題標題 – kittu

+0

仍存在問題。我刪除了@ @ Autowired 私有TopicService topicService;'從實現類 – kittu

3

由於TopicServiceService類,因此您應該使用@Service對其進行註釋,以便Spring爲您自動裝配此Bean。像這樣:

@Service 
public class TopicServiceImplementation implements TopicService { 
    ... 
} 

這將解決您的問題。

-1

必須更新您的

scanBasePackages = { "com.exm.java" } 

的路徑添加到您的服務(與@Service註解之後)