java定义product_考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean

我有

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

如何解决错误?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值