该bean不是由Spring创建的,因为
componentScanattribute错过了所在的包
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的配置优势。
如果
Applicationbean类位于所有其他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;@SpringBootApplicationpublic 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 /实体/存储库等。
文档参考了这一点。
在这里:
许多Spring Boot开发人员的主类始终带有@ Configuration,@
EnableAutoConfiguration和@ComponentScan注释。由于这些注释经常一起使用(特别是如果您遵循上述最佳实践),因此Spring
Boot提供了一种方便的@SpringBootApplication替代方法。@SpringBootApplication注释等效于使用@ Configuration,@
EnableAutoConfiguration和@ComponentScan及其默认属性
这里讨论了
@EnableAutoConfiguration77.3 Use Spring Data Repository
point提供的关于实体发现的内容 :
Spring Boot会根据发现的@EnableAutoConfiguration尝试猜测@Repository定义的位置。
要获得更多控制权,请使用@EnableJpaRepositories批注(来自Spring Data JPA)。



