2016-11-06 22 views
2

考慮您的配置定義類型 'com.ensat.services.ProductService' 的豆

package com.example; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration; 
import org.springframework.boot.autoconfigure.domain.EntityScan; 
import org.springframework.boot.builder.SpringApplicationBuilder; 
import org.springframework.boot.web.support.SpringBootServletInitializer; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 
import org.springframework.transaction.annotation.EnableTransactionManagement; 

@SpringBootApplication 
@ComponentScan(basePackages = {"hello","com.ensat.controllers"}) 
@EntityScan("com.ensat.entities") 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(Application.class); 
    } 

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

} 

ProductController.java

package com.ensat.controllers; 

import com.ensat.entities.Product; 
import com.ensat.services.ProductService; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

/** 
* Product controller. 
*/ 
@Controller 
public class ProductController { 

    private ProductService productService; 

    @Autowired 
    public void setProductService(ProductService productService) { 
     this.productService = productService; 
    } 

    /** 
    * List all products. 
    * 
    * @param model 
    * @return 
    */ 
    @RequestMapping(value = "/products", method = RequestMethod.GET) 
    public String list(Model model) { 
     model.addAttribute("products", productService.listAllProducts()); 
     System.out.println("Returning rpoducts:"); 
     return "products"; 
    } 

    /** 
    * View a specific product by its id. 
    * 
    * @param id 
    * @param model 
    * @return 
    */ 
    @RequestMapping("product/{id}") 
    public String showProduct(@PathVariable Integer id, Model model) { 
     model.addAttribute("product", productService.getProductById(id)); 
     return "productshow"; 
    } 

    // Afficher le formulaire de modification du Product 
    @RequestMapping("product/edit/{id}") 
    public String edit(@PathVariable Integer id, Model model) { 
     model.addAttribute("product", productService.getProductById(id)); 
     return "productform"; 
    } 

    /** 
    * New product. 
    * 
    * @param model 
    * @return 
    */ 
    @RequestMapping("product/new") 
    public String newProduct(Model model) { 
     model.addAttribute("product", new Product()); 
     return "productform"; 
    } 

    /** 
    * Save product to database. 
    * 
    * @param product 
    * @return 
    */ 
    @RequestMapping(value = "product", method = RequestMethod.POST) 
    public String saveProduct(Product product) { 
     productService.saveProduct(product); 
     return "redirect:/product/" + product.getId(); 
    } 

    /** 
    * Delete product by its id. 
    * 
    * @param id 
    * @return 
    */ 
    @RequestMapping("product/delete/{id}") 
    public String delete(@PathVariable Integer id) { 
     productService.deleteProduct(id); 
     return "redirect:/products"; 
    } 

} 

ProductService.java

package com.ensat.services; 

import com.ensat.entities.Product; 

public interface ProductService { 

    Iterable<Product> listAllProducts(); 

    Product getProductById(Integer id); 

    Product saveProduct(Product product); 

    void deleteProduct(Integer id); 

} 

ProductServiceImpl.java

package com.ensat.services; 

import com.ensat.entities.Product; 
import com.ensat.repositories.ProductRepository; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

/** 
* Product service implement. 
*/ 
@Service 
public class ProductServiceImpl implements ProductService { 

    private ProductRepository productRepository; 

    @Autowired 
    public void setProductRepository(ProductRepository productRepository) { 
     this.productRepository = productRepository; 
    } 

    @Override 
    public Iterable<Product> listAllProducts() { 
     return productRepository.findAll(); 
    } 

    @Override 
    public Product getProductById(Integer id) { 
     return productRepository.findOne(id); 
    } 

    @Override 
    public Product saveProduct(Product product) { 
     return productRepository.save(product); 
    } 

    @Override 
    public void deleteProduct(Integer id) { 
     productRepository.delete(id); 
    } 

} 

這是我的錯誤:

*************************** 
APPLICATION FAILED TO START 
*************************** 

Description: 

Parameter 0 of method setProductService in com.ensat.controllers.ProductController required a bean of type 'com.ensat.services.ProductService' that could not be found. 


Action: 

Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration. 

我有充分的日誌:https://gist.github.com/donhuvy/b918e20eeeb7cbe3c4be4167d066f7fd

這是我的全部源代碼 https://github.com/donhuvy/accounting/commit/319bf6bc47997ff996308c890eba81a6fa7f1a93

如何修正錯誤?

+0

我接着說:ProductServiceImpl'的問題,因爲它是缺失的豆。 – davidxxx

回答

6

豆子因爲componentScan屬性錯過這裏ProductServiceImpl所在的包是不是由Spring創建。

此外,@EnableJpaRepositories丟失。所以,Spring不能連接你的倉庫。

@SpringBootApplication 
@ComponentScan(basePackages = {"hello","com.ensat.controllers"}) 
@EntityScan("com.ensat.entities") 

應改爲:

@SpringBootApplication 
@ComponentScan(basePackages = {"hello","com.ensat.controllers", "com.ensat.services"; 
}) 
@EntityScan("com.ensat.entities") 
@EnableJpaRepositories("com.ensat.repositories") 

這將解決你的問題,但這樣做失敗了春春啓動的配置優勢公約的這種方式。

如果Application bean類是位於所有其他 bean類屬於或它的一個子包父包,你就不需要任何更長的時間來指定這兩個註解:

@ComponentScan(basePackages = {"hello","com.ensat.controllers"}) 
@EntityScan("com.ensat.entities") 

@SpringBootApplication類。

例如,將移動到com.ensat包中,並將所有的bean移動到此包或子包中,這樣既解決了您的配置問題,又緩解了配置問題。

package com.ensat; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class Application extends SpringBootServletInitializer { 
    ... 
} 

爲什麼?

因爲@SpringBootApplication已經包括他們(及以上):

@Target(ElementType.TYPE) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
@Inherited 
@SpringBootConfiguration 
@EnableAutoConfiguration 
@ComponentScan(excludeFilters = { 
     @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), 
     @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) 
public @interface SpringBootApplication { 

但是,使用當前類的包作爲basePackage價值發現豆/實體/存儲庫,等等。

該文件涉及這一點。

Here

Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices above), Spring Boot provides a convenient @SpringBootApplication alternative.

The @SpringBootApplication annotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes

這將討論有關@EnableAutoConfiguration77.3 Use Spring Data repositories point提供實體的發現:

Spring Boot tries to guess the location of your @Repository definitions, based on the @EnableAutoConfiguration it finds. To get more control, use the @EnableJpaRepositories annotation (from Spring Data JPA).

+0

更改後,它仍然不起作用。 https://gist.github.com/donhuvy/a3be9091873593a3631f982fd6163a92 –

+0

你的錯誤是**引起的:org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到符合條件的bean for dependency [com.ensat.repositories.ProductRepository]:expected at至少1個符合自動導線候選資格的bean。依賴註釋:{} ** – davidxxx

+0

謝謝,我知道這是另一個問題 –