2017-06-22 10 views
0

私のSpringアプリケーションを起動するときにエラーが発生し、Tomcatサーバーが起動しません。 ここに私の仕事は、下にあります(一部を読んで、メインアプリで、細かい作業している部分を休止状態)org.springframework.beans.factory.BeanCreationException:

主な用途

package com.javainuse; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import org.apache.poi.ss.usermodel.Row; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.Transaction; 
import org.hibernate.cfg.AnnotationConfiguration; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

@SpringBootApplication 
public class SpringBootHelloWorldApplication { 

    public static void main(String[] args) throws IOException {   
       System.out.println("Debug Print"); 
       Read(); 


     SpringApplication.run(SpringBootHelloWorldApplication.class, args); 

    } 

    private static void Read() throws IOException { 
      System.out.println("Hacked by?"); 
     SessionFactory sf = new AnnotationConfiguration().configure("hibernate.cfg.xml").buildSessionFactory(); 
     Session session = sf.openSession(); 
     FileInputStream file = new FileInputStream(new File("C:/Users/MONSTER/Desktop/Hello.xlsx")); 
     XSSFWorkbook workbook = new XSSFWorkbook(file); 
     XSSFSheet sheet = workbook.getSheetAt(0); 
     Row row; 
     for(int i=1; i<=sheet.getLastRowNum(); i++){ 
      row = (Row) sheet.getRow(i); //sheet number 


       String id; 
       if(row.getCell(0)==null) { id = "0"; } 
       else id= row.getCell(0).toString(); 

        String name; 
       if(row.getCell(1)==null) { name = "null";} 
        else name = row.getCell(1).toString(); //else copies cell data to name variable 

        String phone; 
       if(row.getCell(2)==null) { phone = "null"; } 
        else phone = row.getCell(2).toString(); 
     Transaction t = session.beginTransaction(); 
     Customer std = new Customer(); 
     std.setId((int) Double.parseDouble(id)); 
     std.setName(name); 
     std.setPhone(phone); 
     System.out.println(std.getId()+" "+std.getName()+" "+std.getPhone()); 
     session.saveOrUpdate(std); 
     t.commit();  
     } 

     file.close(); 
       System.out.println("Completed!"); 

    } 



    } 

Customerクラス

package com.javainuse; 

import java.io.Serializable; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="customer", schema="excel") 
public class Customer { 
    @Id 
// @GeneratedValue(strategy = GenerationType.AUTO) 
private int id; 
     @Column 
private String name; 
     @Column 
private String phone; 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getPhone() { 
    return phone; 
} 
public void setPhone(String phone) { 
    this.phone = phone; 
} 
public Customer(int id, String name, String phone) { 
    super(); 
    this.id = id; 
    this.name = name; 
    this.phone = phone; 
} 
public Customer() { 
    super(); 
} 





} 

コントローラ

package com.javainuse.controllers; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.servlet.ModelAndView; 

@Controller 
public class helloController { 

    @RequestMapping("/welcome.html") 
    public ModelAndView firstPage() { 
     return new ModelAndView("xd"); 
    } 

} 

ポンポン.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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>YEA</groupId> 
    <artifactId>YEA</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>YEA</name> 
    <description>Demo YEA for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.1.RELEASE</version> 
     <relativePath /> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
       <dependency> 
    <groupId>javax.transaction</groupId> 
    <artifactId>jta</artifactId> 
    <version>1.0.1B</version> 
</dependency> 




     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.tomcat.embed</groupId> 
      <artifactId>tomcat-embed-jasper</artifactId> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
     </dependency> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-jpa</artifactId> 
     <version>1.11.4.RELEASE</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi</artifactId> 
     <version>3.14</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.poi</groupId> 
     <artifactId>poi-ooxml</artifactId> 
     <version>3.14</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate</artifactId> 
     <version>3.2.6.ga</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-annotations</artifactId> 
     <version>3.3.1.GA</version> 
     <type>jar</type> 
    </dependency> 
    <dependency> 
     <groupId>javax</groupId> 
     <artifactId>javaee-web-api</artifactId> 
     <version>7.0</version> 
     <type>jar</type> 
    </dependency> 
    </dependencies> 

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


</project> 

エラー

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.sql.DataSource org.springframework.boot.autoconfigure.orm.jpa.JpaBaseConfiguration.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.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.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.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545) ~[spring-beans-4.2.5.RELEASE.jar:4.2.5.RELEASE] 
    ... 28 common frames omitted 

答えて

0

。次の2つのオプションがあります。

  1. Spring Boot JPA Starterを使用してください。
  2. DataSourceを設定します。下のように()。

    @Bean 
    public DataSource dataSource(){ 
        DriverManagerDataSource dataSource = new DriverManagerDataSource(); 
        dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); 
        dataSource.setUrl("jdbc:hsqldb:hsql://localhost:"); 
        dataSource.setUsername("sa"); 
        dataSource.setPassword(""); 
    
        return dataSource; 
    }; 
    
1

は、ログによると、あなたが使用しているデータベースに接続するデータソースBeanを作成する必要があります。例えば。どこかの構成では、データソースBeanを定義する必要があります:あなたはどのDataSourceを宣言していない

@Bean 
public DataSource dataSource() { 
    ... 
} 
関連する問題