如何解决此问题的春季启动应用程序?

时间:2019-01-12 01:40:03

标签: java spring-boot

  

我正在开发一个Spring Boot应用程序并面向下面
      异常尝试了很多方法仍然无法解决它的帮助       我在这个
          下面是显示未自动连接数据源的错误。

> 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
  

下面是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
  

这是我的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);
                  }
                }
  

这是我的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

1 个答案:

答案 0 :(得分:0)

DataSource类不是自动装配的,其实例不能由Spring控制。 您可能需要将@Component批注放在DataSource类的声明上方,或创建一个@Bean来返回该类的实例。

但是首先请检查src / main / resources / application.properties中存在的数据库连接设置。

  var TabContent = React.createClass({
   render: function(){
     var paragraph = [];
     for (var i = 0; i < this.props.nrPar; i++) {
          var x = "p" + i; //or can I use a variable after this.props.?
          paragraph.push(<p key={i}>{this.props.p + i}<br></br></p>);
     }

     return(              

        <div>            
              {paragraph}
        </div>
       );
   }
  });

  ReactDOM.render(

     <TabContent nrPar="3" 
      p1="first paragraph of text" 
      p2="second paragraph of text" 
      p3="third paragraph of text"/>

      ,month
  )

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

如果这两个选项都不起作用,请检查src / main / resources / application.properties中存在的数据库连接设置