簡體   English   中英

我收到“啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序。” 解決方案?

[英]I am getting “Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.” Solution for this?

我的文件夾結構

我的控制台:

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

2020-08-09 18:04:58.332  INFO 16828 --- [           main] com.api.main.ProjectApiApplication       : Starting ProjectApiApplication on LAPTOP-I4F37P4O with PID 16828 (C:\Users\mitad\Documents\STS_Workspace\ProjectAPI\target\classes started by mitad in C:\Users\mitad\Documents\STS_Workspace\ProjectAPI)
2020-08-09 18:04:58.334  INFO 16828 --- [           main] com.api.main.ProjectApiApplication       : No active profile set, falling back to default profiles: default
2020-08-09 18:04:58.866  INFO 16828 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-08-09 18:04:58.872  INFO 16828 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-08-09 18:04:58.872  INFO 16828 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.37]
2020-08-09 18:04:58.928  INFO 16828 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-08-09 18:04:58.928  INFO 16828 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 566 ms
2020-08-09 18:04:59.039  INFO 16828 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-08-09 18:04:59.148  INFO 16828 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-08-09 18:04:59.155  INFO 16828 --- [           main] com.api.main.ProjectApiApplication       : Started ProjectApiApplication in 1.04 seconds (JVM running for 1.534)
2020-08-09 18:04:59.231  WARN 16828 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
2020-08-09 18:04:59.233  INFO 16828 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-08-09 18:04:59.245 ERROR 16828 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:798) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:779) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:322) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at com.api.main.ProjectApiApplication.main(ProjectApiApplication.java:26) [classes/:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springApplicationAdminRegistrar' defined in class path resource [org/springframework/boot/autoconfigure/admin/SpringApplicationAdminJmxAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:226) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:89) ~[spring-context-5.2.6.RELEASE.jar:5.2.6.RELEASE]
    at com.api.main.ProjectApiApplication.run(ProjectApiApplication.java:19) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    ... 5 common frames omitted
Caused by: javax.management.InstanceAlreadyExistsException: org.springframework.boot:type=Admin,name=SpringApplication
    at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437) ~[na:1.8.0_202]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898) ~[na:1.8.0_202]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966) ~[na:1.8.0_202]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900) ~[na:1.8.0_202]
    at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324) ~[na:1.8.0_202]
    at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522) ~[na:1.8.0_202]
    at org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar.afterPropertiesSet(SpringApplicationAdminMXBeanRegistrar.java:129) ~[spring-boot-2.3.2.RELEASE.jar:2.3.2.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790) ~[spring-beans-5.2.8.RELEASE.jar:5.2.8.RELEASE]
    ... 17 common frames omitted

2020-08-09 18:04:59.362  INFO 16828 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

ConnectToDatabaseImpl:

package com.api.databse.service;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.core.env.Environment;

import org.springframework.stereotype.Service;

@Service("connectTodb")

public class ConnectToDatabaseImpl implements ConnectToDatabase{
    
    @Value("${jdbc.username:default}")
    private String userName;
    
    @Value("${jdbc.password:default}")
    private String password;
    
    @Autowired
    private Environment environment;
    
    @Override
    public void getDatabseDetails() {
        
        try{        
            Class.forName(environment.getProperty("jdbc.driverClassName"));  
            Connection connection = DriverManager.getConnection(  
            environment.getProperty("jdbc.url"),userName,password);  
         
            Statement stmt=connection.createStatement();  
            ResultSet resultSet = stmt.executeQuery(environment.getProperty("jdbc.SQLQuery"));  
            
            while(resultSet.next())  
            System.out.println(resultSet.getInt(1)+"  "+resultSet.getString(2)+"  "+resultSet.getString(3));
            connection.close();
            }
        catch(SQLException se){ 
            se.printStackTrace();
        }  
        catch(ClassNotFoundException ce) {
            ce.printStackTrace();
        }
        
        
    }  
        
}

應用配置:

package com.api.configuration;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@Configuration

@ComponentScan(basePackages="com.api")

@PropertySource(value= {"classpath:application.properties"})

public class AppConfig {    
    @Bean
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
        return new PropertySourcesPlaceholderConfigurer();
    
    }
}

項目Api應用:

package com.api.main;

import org.springframework.boot.CommandLineRunner;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import org.springframework.context.support.AbstractApplicationContext;

import com.api.configuration.AppConfig;

import com.api.databse.service.ConnectToDatabase;

@SpringBootApplication

@EnableAutoConfiguration

public class ProjectApiApplication implements CommandLineRunner{
    
    @Override
    public void run(String...args) throws Exception{
        AbstractApplicationContext  context = new AnnotationConfigApplicationContext(AppConfig.class);
        ConnectToDatabase connection = (ConnectToDatabase)context.getBean("connectTodb");
        connection.getDatabseDetails();
        context.close();
    }
    public static void main(String[] args) {
        SpringApplication.run(ProjectApiApplication.class, args);
    }
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>ProjectAPI</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ProjectAPI</name>
    <description>Project for Spring Boot and API Calling</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>8.0.21</version>
            </dependency>
            
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>5.2.6.RELEASE</version>
            </dependency>
            
        
        
    </dependencies>
    

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

刪除spring-context依賴項 - 您擁有 spring 引導父項,並且通常不引入具有不同版本的 spring 的依賴項。

重要提示:不要在實現 class 的 CommandLineRunner 內運行 SpringBoot main。 一般來說,從run方法中獲取所有這些東西並在 main 中使用它並刪除實現 CommandLineRunner 以使一切正常工作。

你也不需要EnableAutoConfiguration因為@SpringBootApplication注釋包括它

您也不需要PropertySourcesPlaceholderConfigurer ,因為 Spring Boot 提供了一個。 考慮閱讀有關 Spring 引導的文檔。

此外,如果您在application.properties文件中提供屬性,則在 SpringBoot 中自動完成與數據庫的連接 - 如下所示:

spring.datasource.url=jdbc:mysql://localhost:3306/yourDatabaseName?useSSL=false
spring.datasource.username=yourDatabaseUsername
spring.datasource.password=yourDatabasePassword

這是 Spring JDBC https://javamondays.com/simple-java-jdbc-example/的博客文章示例

此外,Spring Boot 正在創建容器,您會收到此錯誤,因為您有 2 個上下文不在同一層次結構中運行。 在 ProjectApiApplication 中添加此字段以檢索上下文:

@Autowired
private ApplicationContext context;

暫無
暫無

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

相關問題 啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序。 我該如何解決問題? 啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序。 錯誤 2332 如何修復“啟動 ApplicationContext 時出錯。要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序。” 啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序 Spring 啟動:啟動 ApplicationContext 時出錯。 要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序 在 Spring Boot 中繼續獲取“啟動 ApplicationContext 時出錯。要顯示條件報告,請在啟用“調試”的情況下重新運行您的應用程序” “啟動 ApplicationContext 時出錯。 在運行 Spring 啟動項目時,顯示條件報告重新運行您的應用程序並啟用“調試” 啟動ApplicationContext時出錯。 要顯示條件報告,請在spring-boot中啟用“ debug”重新運行您的應用程序 Spring Boot 請求:“在啟用 &#39;debug&#39; 的情況下重新運行您的應用程序” - 我該怎么做? 在ApplicationContext中刷新/替換bean。 可能還是我錯過了重點?
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM