2015-02-19 5 views
0

Я пытаюсь установить базовый шаблон для приложения spring/hibernate. Я никогда не делал этого раньше, поэтому я действительно не знаю, в чем проблема. Вот вывод ошибки и ссылка на код. Я думаю, что он пытается настроить auto вместо чтения моего файла spring.xml.Ошибка Настройка Spring + Hibernate Application

ссылка: https://github.com/cfeher/Party-App/tree/0.0.x/Api

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Injection of autowired dependencies failed; nested exception is 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.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$NonEmbeddedConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.sql.DataSource org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$NonEmbeddedConfiguration.dataSource()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. 

spring.xml

  <?xml version="1.0" encoding="UTF-8"?> 
      <beans xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
             http://www.springframework.org/schema/context 
             http://www.springframework.org/schema/context/spring-context-3.2.xsd 
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 



      <!-- Hibernate 3 Annotation SessionFactory Bean definition--> 
       <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource" /> 
        <property name="packagesToScan" value="org.mobiengineering"/> 
        <property name="hibernateProperties"> 
         <value> 
          hbm2ddl.auto=create 
          hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect 
          hibernate.show_sql=true    
         </value> 
        </property> 
       </bean> 

       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="org.postgresql.Driver"/> 
        <property name="url" value="jdbc:postgresql://localhost:5432/postgres"/> 
        <property name="username" value="postgres"/> 
        <property name="password" value=""/> 
       </bean> 


       <bean id="personDAO" class="org.mobiengineering.dao.PersonDAOImpl"> 
        <property name="sessionFactory" ref="sessionFactory" /> 
       </bean> 

      </beans> 

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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
       <modelVersion>4.0.0</modelVersion> 
       <groupId>org.springframework.samples</groupId> 
       <artifactId>SpringHibernateExample</artifactId> 
       <version>0.0.1-SNAPSHOT</version> 

       <properties> 

        <!-- Generic properties --> 
        <java.version>1.7</java.version> 
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 

        <!-- Spring --> 
        <spring-framework.version>4.0.3.RELEASE</spring-framework.version> 

        <!-- Hibernate/JPA --> 
        <!-- <hibernate.version>4.3.5.Final</hibernate.version> --> 
        <hibernate.version>3.6.9.Final</hibernate.version> 

        <!-- Logging --> 
        <logback.version>1.0.13</logback.version> 
        <slf4j.version>1.7.5</slf4j.version> 

       </properties> 

       <parent> 
        <groupId>org.springframework.boot</groupId> 
        <artifactId>spring-boot-starter-parent</artifactId> 
        <version>1.1.10.RELEASE</version> 
       </parent> 


       <dependencies> 
        <!-- Spring and Transactions --> 
        <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-context</artifactId> 
         <version>${spring-framework.version}</version> 
        </dependency> 
        <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-tx</artifactId> 
         <version>${spring-framework.version}</version> 
        </dependency> 
          <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
        </dependency> 

        <!-- Spring ORM support --> 
        <dependency> 
         <groupId>org.springframework</groupId> 
         <artifactId>spring-orm</artifactId> 
         <version>${spring-framework.version}</version> 
        </dependency> 

        <!-- Logging with SLF4J & LogBack --> 
        <dependency> 
         <groupId>org.slf4j</groupId> 
         <artifactId>slf4j-api</artifactId> 
         <version>${slf4j.version}</version> 
         <scope>compile</scope> 
        </dependency> 
        <dependency> 
         <groupId>ch.qos.logback</groupId> 
         <artifactId>logback-classic</artifactId> 
         <version>${logback.version}</version> 
         <scope>runtime</scope> 
        </dependency> 

        <!-- Hibernate --> 
        <dependency> 
         <groupId>org.hibernate</groupId> 
         <artifactId>hibernate-entitymanager</artifactId> 
         <version>${hibernate.version}</version> 
        </dependency> 
        <dependency> 
         <groupId>org.hibernate</groupId> 
         <artifactId>hibernate-core</artifactId> 
         <version>${hibernate.version}</version> 
        </dependency> 

        <dependency> 
         <groupId>mysql</groupId> 
         <artifactId>mysql-connector-java</artifactId> 
         <version>5.1.9</version> 
        </dependency> 
        <dependency> 
         <groupId>commons-dbcp</groupId> 
         <artifactId>commons-dbcp</artifactId> 
         <version>1.4</version> 
        </dependency> 

        <dependency> 
         <groupId>postgresql</groupId> 
         <artifactId>postgresql</artifactId> 
         <version>9.1-901-1.jdbc4</version> 
        </dependency> 
       </dependencies> 

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

       <repositories> 
        <repository> 
         <id>spring-releases</id> 
         <url>https://repo.spring.io/libs-release</url> 
        </repository> 
       </repositories> 
       <pluginRepositories> 
        <pluginRepository> 
         <id>spring-releases</id> 
         <url>https://repo.spring.io/libs-release</url> 
        </pluginRepository> 
       </pluginRepositories> 
      </project> 
+0

разместим ваш конфигурационный файл, он жалуется, что вы вмятина положил driverClassName –

+0

1) Вы (хотите) использовать весенний ботинок? – Ralph

+0

2) напишите свой pom.xml – Ralph

ответ

1

Вы должны добавить следующий класс аннотацию Application.java:

@ImportResource(value = {"/spring.xml"}) 
@ComponentScan 
@EnableAutoConfiguration 
public class Application { 
    ... 
} 

@ComponentScan только автоматически обнаруживает аннотации стереоизображений Spring Components. Для xml вам нужно будет добавить @ImportResource.

также:

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml"); 
context.close(); 

те строки выше, являются избыточными: Application.class, арг), `достаточно для пружинной загрузки для загрузки приложения.

В качестве альтернативы, если вы не хотите использовать загрузку пружины, просто закомментируйте строку выше (т.е. SpringApplication.run(...))

+0

Отлично! Все работает! –

Смежные вопросы