简体   繁体   English

我如何解决这个问题 Spring Boot 应用程序?

[英]How do I fix this issue spring boot application?

I am developing a spring boot application and facing below我正在开发一个弹簧启动应用程序并面向下方
exception tried many way still not able to resolve it help me on this.异常尝试了很多方法仍然无法解决它帮助我解决这个问题。
Below is the error shown that auto wire with datasource is not instantiating.下面是显示与数据源的自动连线未实例化的错误。

> Please find the below structure of my project

                Structure
                Redglass-core       
                    |———> src/main/java
                            ———>com.javalabs.redglass
                                        `   ———>RedglassCoreAPPlication.java

                    |———> src/main/resources
                                ———>application.properties


                    |———> src/test/java
                            ———>com.javalabs.redglass.test
                                        `   ———>RedglassCoreAPPlicationTest.java

                    |————>/src
                            |————>/target
                            |————>pom.xml

Below is the application.properties file下面是 application.properties 文件

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/company?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.hibernate.ddl-auto=update

This is my Springboot starter class RedglassCoreAPPlication.java这是我的 Springboot 入门类 RedglassCoreAPPlication.java

            package com.javalabs.redglass;

            import org.springframework.boot.SpringApplication;
            import org.springframework.boot.autoconfigure.SpringBootApplication;
            import org.springframework.context.annotation.PropertySource;

            RedglassCoreApplication.java
            @SpringBootApplication
            public class RedglassCoreApplication {
            public static void main(String[] args) {
                SpringApplication.run(RedglassCoreApplication.class, args);
            }
            }

> This is my Springboot Junit test class 

           RedglassCoreApplicationTest.java**


               package com.javalabs.redglass.test;

                import static org.junit.Assert.assertEquals;

                import java.sql.SQLException;
                import javax.sql.DataSource;
                import org.junit.Test;
                import org.junit.runner.RunWith;
                import org.springframework.beans.factory.annotation.Autowired;
                import   
org.springframework.boot.autoconfigure.SpringBootApplication;
                import org.springframework.test.context.ContextConfiguration;
                import
                    org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
                import com.javalabs.redglass.RedglassCoreApplication;
                @RunWith(SpringJUnit4ClassRunner.class)
                @SpringBootApplication(scanBasePackageClasses =        

                 RedglassCoreApplication.class)
                 @ContextConfiguration({"classpath*:/redglass-      
                        core/applicationContext.xml"})
                  public class RedglassCoreApplicationTest {
                  @Autowired
                  DataSource datasource;

                  @Test
                  public void testDummy() throws SQLException
                  {
                  String schema = datasource.getConnection().getCatalog();
                  System.out.println("Schema name  :- "+schema);
                  assertEquals("company", schema);
                  }
                }

This is my maven pom.xml file Pom.xml这是我的 maven pom.xml 文件 Pom.xml

            <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  c.      
              http://maven.apache.org/xsd/maven-4.0.0.xsd">
             <modelVersion>4.0.0</modelVersion>
             <parent>
              <groupId>com.javalabs</groupId>
              <artifactId>redglass</artifactId>
              <version>0.0.1-SNAPSHOT</version>
             </parent>
             <artifactId>redglass-core</artifactId>
             <dependencies>
             <!-- JPA Data (We are going to use Repositories, Entities,   
              Hibernate, etc...) -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
             </dependency>
             <!-- Use MySQL Connector-J -->
             <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
             </dependency>
             <dependency>
               <groupId>javax.el</groupId>
               <artifactId>javax.el-api</artifactId>
               <version>2.2.4</version>
             </dependency>
             <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            </dependencies>


            </project>
> Seen below is the error which I am not able to fix it :- 

                org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.javalabs.redglass.test.RedglassCoreApplicationTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.sql.DataSource com.javalabs.redglass.test.RedglassCoreApplicationTest.datasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:385) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118) ~[spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) ~[spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228) ~[spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit.jar:4.12]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) [junit.jar:4.12]
                    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) [junit.jar:4.12]
                    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) [junit.jar:4.12]
                    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) [junit.jar:4.12]
                    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) [junit.jar:4.12]
                    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.junit.runners.ParentRunner.run(ParentRunner.java:363) [junit.jar:4.12]
                    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193) [spring-test-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) [.cp/:na]
                    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) [.cp/:na]
                    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) [.cp/:na]
                    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) [.cp/:na]
                    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) [.cp/:na]
                    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) [.cp/:na]
                Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: javax.sql.DataSource com.javalabs.redglass.test.RedglassCoreApplicationTest.datasource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    ... 26 common frames omitted
                Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1373) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.3.RELEASE.jar:4.2.3.RELEASE]
                    ... 28 common frames omitted
                11:16:38.402 [main] DEBUG o.s.t.c.s.AbstractDirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@6bedbc4d testClass = RedglassCoreApplicationTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@932bc4a testClass = RedglassCoreApplicationTest, locations = '{classpath*:/redglass-core/applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null].
                11:16:38.404 [Thread-0] INFO  o.s.c.s.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@35aea049: startup date [Fri Jan 11 11:16:38 EST 2019]; root of context hierarchy
                11:16:38.405 [Thread-0] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
                11:16:38.405 [Thread-0] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@38145825: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy

The DataSource class is not Autowired, its instance can not be controlled by Spring. DataSource 类不是 Autowired,它的实例不能被 Spring 控制。 You probably need to put the @Component annotation above the declaration of the DataSource class or create an @Bean that returns an instance of the class.您可能需要将 @Component 注释放在 DataSource 类的声明上方,或者创建一个返回该类实例的 @Bean。

But first check the database connection settings present in src / main / resources / application.properties.但首先检查 src/main/resources/application.properties 中存在的数据库连接设置。

 @Component public class DataSource { (...) }

 @SpringBootApplication public class RedglassCoreApplication { public static void main(String[] args) { SpringApplication.run(RedglassCoreApplication.class, args); } @Bean public DataSource dataSource() { return //DataSource Instance } }

If neither option works check your database connection settings present in src / main / resources / application.properties如果这两个选项都不起作用,请检查 src/main/resources/application.properties 中存在的数据库连接设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM