大佬教程收集整理的这篇文章主要介绍了考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
该bean不是由Spring创建的,因为componentScan
attribute错过了所在的包ProductserviceImpl
。
此外,@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")
它将解决您的问题,但是这种方式克服了Spring和Spring Boot的配置优势。
如果Application
bean类位于所有其他bean类所属的父包中或属于它的子包,则不再需要指定这两个注释:
@ComponentScan(basePackages = {"Hello","com.ensat.controllers"})
@EntityScan("com.ensat.entitIEs")
在@SpringBootApplication
课堂上。
例如,移入Application
该 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
值来发现bean /实体/存储库等。
文档参考了这一点。
在这里:
这里讨论了@EnableautoConfiguration
77.3 Use Spring Data Repository
point提供的关于实体发现的内容 :
我有
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,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);
}
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
如何解决错误?
以上是大佬教程为你收集整理的考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean全部内容,希望文章能够帮你解决考虑在配置中定义类型为“ com.ensat.services.ProductService”的bean所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。