2017-01-15 19 views
7

Tôi gặp lỗi khi chạy lớp chính.Xem xét xác định loại đậu 'dịch vụ' trong cấu hình của bạn [Khởi động mùa xuân]

Lỗi:

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 

giao diện 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); 
} 

điều khiển:

@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"); 
    } 
} 

lớp thực hiện:

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. 
    } 
} 

Phần còn lại của lớp học cũng được xác định. Tôi không biết tại sao lại ném nó mặc dù tuyên bố componentScan trong lớp chính.

lớp chính:

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

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

tôi có các gói của tôi như thế này:

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

Liệu các lớp 'TopicServiceImplementation' có chú thích '@ Component'? Nếu không, bạn phải thêm nó vào lớp, để Spring có thể nhận ra nó như là một bean. – dunni

+1

@dunni Trong khởi động vào mùa xuân, chú thích '@ Component' không bắt buộc phải được quét tự động bởi springboot không giống như springmvc – kittu

+0

Nếu bạn không thêm bất kỳ chú thích nào trên lớp, nó sẽ không được quét bởi vì bạn chưa đánh dấu nó là hạt đậu. Nếu bạn không tin tôi, hãy đọc tài liệu. – dunni

Trả lời

10

Một lớp phải có chú thích @Component hoặc một dẫn xuất của chú thích đó (như @Service, @Repository, v.v.) để được nhận dạng dưới dạng hạt Spring bằng cách quét thành phần. Vì vậy, nếu bạn thêm @Component vào lớp học, nó sẽ giải quyết vấn đề của bạn.

+0

Tôi nhận được lỗi đó ngay cả khi tôi đã chú thích lớp đó với @Component –

1

Bạn đang cố gắng tiêm một bean của riêng mình. Điều đó rõ ràng sẽ không hoạt động.

TopicServiceImplementation thực hiện TopicService. Lớp đó cố gắng để autowire (by field!) Một `TopicService. Vì vậy, bạn về cơ bản yêu cầu bối cảnh để tự tiêm.

Có vẻ như bạn đã chỉnh sửa nội dung của thông báo lỗi: Field topicService in seconds47.restAPI.topics không phải là một lớp học. Hãy cẩn thận nếu bạn cần giấu thông tin nhạy cảm vì nó giúp người khác giúp đỡ bạn nhiều hơn.

Quay lại vấn đề thực tế, có vẻ như việc tiêm TopicService chính nó là một trục trặc về phía bạn.

+0

Tôi đã không hiểu một điều khi bạn nói 'Tôi đang tiêm một hạt đậu trong chính nó' ?? Tôi đang tiêm 'TopicService' là giao diện vào một lớp điều khiển' Chủ đề ' – kittu

+0

Tôi không chỉnh sửa thông báo lỗi. Tôi vừa chỉnh sửa tiêu đề câu hỏi tôi đã đăng – kittu

+0

Vẫn còn vấn đề tồn tại. Tôi đã xóa '@Autowired chủ đề riêngChủ đề dịch vụService;' từ lớp triển khai – kittu

3

TopicService là lớp Service, bạn nên chú thích nó với @Service, để Spring tự động lấy hạt này cho bạn. Giống như vậy:

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

Điều này sẽ giải quyết được vấn đề của bạn.

-1

Bạn phải cập nhật của bạn

scanBasePackages = { "com.exm.java" } 

để thêm đường dẫn đến dịch vụ của bạn (sau khi chú thích nó với @service)

Các vấn đề liên quan