簡體   English   中英

應用程序未運行,“考慮在您的配置中定義一個類型的 bean。”

[英]Application not running, ''Consider defining a bean of type in your configuration."

我正在嘗試為我的項目制作一個玩家列表,並且有一個包含不同對象的工作應用程序(書籍而不是玩家)。 我迫切需要幫助,因為我無法從這里繼續前進..

任何人都有任何想法為什么它會給我這樣的錯誤? 我需要發布更多信息嗎?

這是我的 PlayerApplication.java;

package fi.haagahelia.course;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;

import model.Pelaaja;
import model.PelaajaRepository;
import model.Rooli;
import model.RooliRepository;
import model.User;
import model.UserRepository;


@SpringBootApplication
public class PelaajaApplication {
    private static final Logger log = LoggerFactory.getLogger(PelaajaApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(PelaajaApplication.class, args);
    }

    
    
    @Bean
    public CommandLineRunner pelaajaDemo(PelaajaRepository prepository, RooliRepository rorepository, UserRepository urepository) {
    return (args) -> {
        
        
        log.info("save two users");
        User user1 = new User("eetu", "$2a$06$3jYRJrg0ghaaypjZ/.g4SethoeA51ph3UD4kZi9oPkeMTpjKU5uo6", "USER");
        User user2 = new User("eetu2", "$2a$10$0MMwY.IQqpsVc1jC8u7IJ.2rT8b0Cd3b3sfIBGV2zfgnPGtT4r0.C", "ADMIN");
        urepository.save(user1);
        urepository.save(user2);
        
        
        
        log.info("save a couple players");
        rorepository.save(new Rooli("In-Game Leader"));
        rorepository.save(new Rooli("Rifler"));
        rorepository.save(new Rooli("AWP"));
        prepository.save(new Pelaaja("Atte Ampuja", "1999", 150000, rorepository.findByName("Rifler").get(0)));
        prepository.save(new Pelaaja("Joonas Joonaksela", "2005", 13, rorepository.findByName("AWP").get(0)));
        prepository.save(new Pelaaja("Eetu Leppänen", "1999", 1500000, rorepository.findByName("In-Game Leader").get(0)));
        
        log.info("fetch all players");
        for (Pelaaja pelaaja : prepository.findAll()) {
            log.info(pelaaja.toString());
        }

    };
}
}

但是當我運行它時,這是控制台:

 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.3.RELEASE)

2020-11-15 00:50:17.538  INFO 15276 --- [  restartedMain] fi.haagahelia.course.PelaajaApplication  : Starting PelaajaApplication on Calgary-PC with PID 15276 (C:\Users\Calgary\eclipse-workspace\koodaus\myllycs2020\target\classes started by Calgary in C:\Users\Calgary\eclipse-workspace\koodaus\myllycs2020)
2020-11-15 00:50:17.546  INFO 15276 --- [  restartedMain] fi.haagahelia.course.PelaajaApplication  : No active profile set, falling back to default profiles: default
2020-11-15 00:50:17.638  INFO 15276 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-11-15 00:50:17.638  INFO 15276 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-11-15 00:50:18.780  INFO 15276 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFERRED mode.
2020-11-15 00:50:18.821  INFO 15276 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 28ms. Found 0 JPA repository interfaces.
2020-11-15 00:50:19.467  INFO 15276 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@3b43b0fc' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-11-15 00:50:19.477  INFO 15276 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-11-15 00:50:20.250  INFO 15276 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-15 00:50:20.269  INFO 15276 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-15 00:50:20.269  INFO 15276 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-11-15 00:50:20.546  INFO 15276 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-15 00:50:20.547  INFO 15276 --- [  restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2909 ms
2020-11-15 00:50:20.670  INFO 15276 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-11-15 00:50:21.004  INFO 15276 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-11-15 00:50:21.021  INFO 15276 --- [  restartedMain] o.s.b.a.h2.H2ConsoleAutoConfiguration    : H2 console available at '/h2-console'. Database available at 'jdbc:h2:mem:testdb'
2020-11-15 00:50:21.529  INFO 15276 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-15 00:50:21.675  INFO 15276 --- [         task-1] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-11-15 00:50:21.760  WARN 15276 --- [  restartedMain] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-11-15 00:50:21.800  INFO 15276 --- [         task-1] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.20.Final
2020-11-15 00:50:22.087  INFO 15276 --- [         task-1] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-11-15 00:50:22.410  INFO 15276 --- [         task-1] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2020-11-15 00:50:22.886  INFO 15276 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-11-15 00:50:22.901  INFO 15276 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-11-15 00:50:23.109  INFO 15276 --- [         task-2] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-11-15 00:50:24.016  INFO 15276 --- [  restartedMain] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@5e48e9e9, org.springframework.security.web.context.SecurityContextPersistenceFilter@780cb960, org.springframework.security.web.header.HeaderWriterFilter@2d0b4ae2, org.springframework.security.web.csrf.CsrfFilter@5c7e2a08, org.springframework.security.web.authentication.logout.LogoutFilter@4352ea5, org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter@634d0e5d, org.springframework.security.web.authentication.ui.DefaultLoginPageGeneratingFilter@4ad01a34, org.springframework.security.web.authentication.ui.DefaultLogoutPageGeneratingFilter@3c18980d, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@20650474, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4815273a, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@48596df1, org.springframework.security.web.session.SessionManagementFilter@26232c18, org.springframework.security.web.access.ExceptionTranslationFilter@525ea091, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@707203bf]
2020-11-15 00:50:24.069  WARN 15276 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'pelaajaDemo' defined in fi.haagahelia.course.PelaajaApplication: Unsatisfied dependency expressed through method 'pelaajaDemo' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'model.PelaajaRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2020-11-15 00:50:24.070  INFO 15276 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-11-15 00:50:24.071  INFO 15276 --- [  restartedMain] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-down'
2020-11-15 00:50:24.077  INFO 15276 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'
2020-11-15 00:50:24.310  WARN 15276 --- [  restartedMain] o.s.b.f.support.DisposableBeanAdapter    : Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
2020-11-15 00:50:24.311  INFO 15276 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown initiated...
2020-11-15 00:50:24.313  INFO 15276 --- [  restartedMain] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Shutdown completed.
2020-11-15 00:50:24.316  INFO 15276 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-11-15 00:50:24.330  INFO 15276 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-11-15 00:50:24.496 ERROR 15276 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method pelaajaDemo in fi.haagahelia.course.PelaajaApplication required a bean of type 'model.PelaajaRepository' that could not be found.


Action:

Consider defining a bean of type 'model.PelaajaRepository' in your configuration.```

請在“跟蹤”模式下運行您的日志,然后檢查。 用您提供的一段代碼很難回答您的查詢。

我建議通過項目(鏈接在下面給出),然后嘗試在您的代碼中實現相同的,希望能解決您的問題。

https://www.programcreek.com/java-api-examples/?code=attacomsian%2Fcode-examples%2Fcode-examples-master%2Fspring-data-jpa%2Fjpa-auditing%2Fsrc%2Fmain%2Fjava%2Fcom%2Fattacomsian %2Fjpa%2FApplication.java#

從提供的日志來看,Spring 容器似乎無法找到/注入任何名為 PelaajaRepository 的 bean。 可能的原因——

  1. 可能您缺少使用 @Component 或 @Repository 注釋您的類。 這些注釋確保您的類在上下文中注冊為 Spring bean。

  2. 如果 PelaajaRepository 是 Spring 數據存儲庫,您可能需要使用 @EnableJpaRepositories 和 @SpringBootApplication 對 PelaajaApplication 進行注釋。

最后,您可能需要使用@ComponentScan 注釋您的 Main 類,這為 Spring 框架提供了在何處搜索組件的線索。

關於流行的spring注解的詳細信息,請參見Spring框架注解

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM