aolish
aolish

Reputation: 21

Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Error:

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

Before the code can still run, add some DataSourceConfig configuration and pom.xml, the above situation occurs.

Can not start now. Project structure

updated: I just rebuilt a project, found that I will DataSourceConfig.class join, then run on the wrong program. I do not know exactly where the error occurred.

pom.xml This is maven's configuration file。

 <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 This is my Data configuration added later.

@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 This is a control class.

    @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>{
}

Upvotes: 1

Views: 11542

Answers (6)

DON
DON

Reputation: 835

Add this dependency

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

Upvotes: 0

sandeep seervi
sandeep seervi

Reputation: 111

I had the same problem, I did manually specified the packages to scan in the Main class. and it resolved my problem.

@SpringBootApplication
@ComponentScan(basePackages = "com.nuclibrarybackstage.repository")
public class Application {}

Upvotes: 0

Pankhuri Agarwal
Pankhuri Agarwal

Reputation: 1

Try using @Service on your service implementation class. It worked for me.

In a multitier application, we will have different layers like presentation, service, business, data access etc. When a class is to be annotated for auto-detection by Spring, then we should use the respective stereotype as below.

@Component – generic and can be used across application.

@Service – annotate classes at service layer level.

@Controller – annotate classes at presentation layers level, mainly used in Spring MVC.

@Repository – annotate classes at persistence layer, which will act as database repository.

for more detail: https://javapapers.com/spring/spring-component-service-repository-controller-difference/

Upvotes: 0

aolish
aolish

Reputation: 21

delete pom.xml'code:

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

Upvotes: 1

AchillesVan
AchillesVan

Reputation: 4356

Your AdministratorRepository May be defined in a package that is not scanned by Springboot.

instead try defining it manually:

  @Repository
  @ComponentScan(basePackages = "com.nucli.etc.repository)
//@Component
  @ConfigurationProperties(prefix = "administrator")
  public interface AdministratorRepository extends 
  JpaRepository<Administrator,Integer>{
    }

Upvotes: 0

Yannic B&#252;rgmann
Yannic B&#252;rgmann

Reputation: 6581

This error can occur if the entity managed by the repository is not annotated with @Entity.

Please make sure your Administrator class is annotated with this annotation.

Upvotes: 0

Related Questions