错误:
2018-01-16 20:06:12.431 INFO 22672 --- [ main] c.n.NucLibraryBackstageApplication : Starting NucLibraryBackstageApplication on DESKTOP-SCA7LS6 with PID 22672 (H:\NUCLibraryBackstage\target\classes started by hp in H:\NUCLibraryBackstage)
2018-01-16 20:06:12.433 INFO 22672 --- [ main] c.n.NucLibraryBackstageApplication : The following profiles are active: dev
2018-01-16 20:06:12.469 INFO 22672 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@e056f20: startup date [Tue Jan 16 20:06:12 GMT+08:00 2018]; root of context hierarchy
2018-01-16 20:06:13.551 INFO 22672 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$ef473d70] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-01-16 20:06:13.940 INFO 22672 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-01-16 20:06:13.951 INFO 22672 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-01-16 20:06:13.952 INFO 22672 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.23
2018-01-16 20:06:14.032 INFO 22672 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-01-16 20:06:14.032 INFO 22672 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1566 ms
2018-01-16 20:06:14.166 INFO 22672 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2018-01-16 20:06:14.182 INFO 22672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-01-16 20:06:14.182 INFO 22672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-01-16 20:06:14.182 INFO 22672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-01-16 20:06:14.182 INFO 22672 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-01-16 20:06:14.219 WARN 22672 --- [ main] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'administratorController': Unsatisfied dependency expressed through field 'administratorRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nuclibrarybackstage.repository.AdministratorRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-01-16 20:06:14.219 INFO 22672 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-01-16 20:06:14.235 INFO 22672 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-16 20:06:14.304 ERROR 22672 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field administratorRepository in com.nuclibrarybackstage.controller.AdministratorController required a bean of type 'com.nuclibrarybackstage.repository.AdministratorRepository' that could not be found.
Action:
Consider defining a bean of type 'com.nuclibrarybackstage.repository.AdministratorRepository' in your configuration.
Process finished with exit code 1
在代码仍然可以运行之前,添加一些DataSourceConfig配置和pom.xml,就会出现上述情况。
现在无法启动。 Project structure
更新: 我刚刚重建了一个项目,发现我将DataSourceConfig.class加入,然后运行错误的程序。我不知道错误发生的确切位置。
的pom.xml 这是maven的配置文件。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<!--Generate api automatically-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.springframework.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<version>1.1.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-staticdocs</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.4.Final</version>
</dependency>
DataSourceConfig 这是我稍后添加的数据配置。
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
//@ComponentScan("com.nuclibrarybackstage.repository")
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new
HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new
LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("com.nuclibrarybackstage.domain");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
AdministratorController 这是一个控制类。
@RestController
public class AdministratorController {
@Autowired
private AdministratorRepository administratorRepository;
@PostMapping(value = "/administrator")
public Result<Administrator> administratorAdd(@Valid Administrator
administrator ,BindingResult bindingResult){
if (bindingResult.hasErrors()){
return
ResultUtli.error(1,bindingResult.getFieldError().getDefaultMessage());
}
return
ResultUtli.success(administratorRepository.save(administrator));
}
AdministratorRepository
@Component
@ConfigurationProperties(prefix = "administrator")
public interface AdministratorRepository extends
JpaRepository<Administrator,Integer>{
}
答案 0 :(得分:0)
您的 AdministratorRepository 可以在Springboot未扫描的包中定义。
而是尝试手动定义它:
@Repository
@ComponentScan(basePackages = "com.nucli.etc.repository)
//@Component
@ConfigurationProperties(prefix = "administrator")
public interface AdministratorRepository extends
JpaRepository<Administrator,Integer>{
}
答案 1 :(得分:0)
如果存储库管理的实体未使用@Entity
注释,则会发生此错误。
请确保您的Administrator
课程已使用此注释进行注释。
答案 2 :(得分:0)
删除pom.xml&#39;代码:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.3.Final</version>
</dependency>
答案 3 :(得分:0)
尝试在服务实现类上使用@Service。它对我有用。
在多层应用程序中,我们将具有不同的层,例如表示,服务,业务,数据访问等。当要对类进行注解以供Spring自动检测时,则应使用以下相应的构造型。
@Component –通用,可以在整个应用程序中使用。
@Service –在服务层级别注释类。
@Controller –在表示层级别注释类,主要在Spring MVC中使用。
@Repository –在持久层注释类,它将充当数据库存储库。
了解更多信息:https://javapapers.com/spring/spring-component-service-repository-controller-difference/
答案 4 :(得分:0)
我遇到了同样的问题,我确实在Main类中手动指定了要扫描的软件包。它解决了我的问题。
@SpringBootApplication
@ComponentScan(basePackages = "com.nuclibrarybackstage.repository")
public class Application {}
答案 5 :(得分:0)
添加此依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>