2016-05-23 60 views
0

我有一個項目,彈簧配置工作得很好。然而,現在我試圖創建一個maven項目並將所有包複製到新的maven項目中,當我在tomcat服務器上運行它時,出現異常:找不到我的sessionFactoryBean的類。在類路徑資源中定義名爲'sessionFactory'的bean時出錯...找不到類

我不明白爲什麼我的maven項目無法找到我的springframework配置的註釋類,當類的路徑顯然是正確的。

我的Spring配置:

<context:annotation-config /> 
<tx:annotation-driven transaction-manager="transactionManager" /> 

<!-- Database config --> 
<import resource="datasource.xml" /> 

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="annotatedClasses"> 
     <list> 
      <!-- item --> 
      <value>domain.item.Format</value> 
      <value>domain.item.Option</value> 
      <value>domain.item.Category</value> 
      <value>domain.item.Item</value> 
      <value>domain.item.ItemPrice</value> 
      <value>domain.item.PriceDiscount</value> 

      <!-- location --> 
      <value>domain.location.Address</value> .... 

我的web.xml

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:spring.config.xml</param-value> 
</context-param> 

<servlet> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>jersey.config.server.provider.packages</param-name> 
     <param-value>Presto.RestServer</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Jersey Web Application</servlet-name> 
    <url-pattern>/webapi/*</url-pattern> 
</servlet-mapping> 

下面是該項目

下面的截圖是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/maven-v4_0_0.xsd"> 

<modelVersion>4.0.0</modelVersion> 

<groupId>Presto</groupId> 
<artifactId>RestServer</artifactId> 
<packaging>war</packaging> 
<version>0.0.1-SNAPSHOT</version> 
<name>RestServer</name> 

<build> 
    <finalName>RestServer</finalName> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.5.1</version> 
      <inherited>true</inherited> 
      <configuration> 
       <source>1.7</source> 
       <target>1.7</target> 
      </configuration> 
     </plugin> 
    </plugins> 
</build> 

<dependencyManagement> 
    <dependencies> 
     <dependency> 
      <groupId>org.glassfish.jersey</groupId> 
      <artifactId>jersey-bom</artifactId> 
      <version>${jersey.version}</version> 
      <type>pom</type> 
      <scope>import</scope> 
     </dependency> 
    </dependencies> 
</dependencyManagement> 

<dependencies> 

    <dependency> 
     <groupId>org.glassfish.jersey.containers</groupId> 
     <artifactId>jersey-container-servlet-core</artifactId> 
     <!-- use the following artifactId if you don't need servlet 2.x compatibility --> 
     <!-- artifactId>jersey-container-servlet</artifactId --> 
    </dependency> 
    <!-- uncomment this to get JSON support <dependency> <groupId>org.glassfish.jersey.media</groupId> 
     <artifactId>jersey-media-moxy</artifactId> </dependency> --> 

    <!-- SPRING REQUIRED DEPENDENCIES --> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-aop --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aop</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-aspects --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-aspects</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-beans --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-beans</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-core</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-expression --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-expression --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jdbc</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-orm --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-orm</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-jms --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-jms</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-messaging --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-messaging</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-oxm --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-oxm</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-test --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-tx --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-web --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-web</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.springframework/spring-webmvc --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-webmvc</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-websocket --> 
    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-websocket</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

    <!-- HIBERNATE REQUIRED DEPENDENCIES --> 
    <!-- http://mvnrepository.com/artifact/antlr/antlr --> 
    <dependency> 
     <groupId>antlr</groupId> 
     <artifactId>antlr</artifactId> 
     <version>2.7.7</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/com.fasterxml/classmate --> 
    <dependency> 
     <groupId>com.fasterxml</groupId> 
     <artifactId>classmate</artifactId> 
     <version>1.3.0</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/dom4j/dom4j --> 
    <dependency> 
     <groupId>dom4j</groupId> 
     <artifactId>dom4j</artifactId> 
     <version>1.6.1</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.apache.geronimo.specs/geronimo-jta_1.1_spec --> 
    <dependency> 
     <groupId>org.apache.geronimo.specs</groupId> 
     <artifactId>geronimo-jta_1.1_spec</artifactId> 
     <version>1.1.1</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations --> 
    <dependency> 
     <groupId>org.hibernate.common</groupId> 
     <artifactId>hibernate-commons-annotations</artifactId> 
     <version>5.0.1.Final</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-core --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>5.1.0.Final</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-java8 --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-java8</artifactId> 
     <version>5.1.0.Final</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager --> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-entitymanager</artifactId> 
     <version>5.1.0.Final</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.38</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/org.hsqldb/hsqldb --> 
    <dependency> 
     <groupId>org.hsqldb</groupId> 
     <artifactId>hsqldb</artifactId> 
     <version>2.3.4</version> 
    </dependency> 

    <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java --> 
    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.39</version> 
    </dependency> 


</dependencies> 
<properties> 
    <jersey.version>2.23</jersey.version> 
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
</properties> 

例外:

org.springframework.beans.factory.BeanCreationException:錯誤創建具有名稱 '的sessionFactory' 類路徑資源[spring.config.xml]中定義豆:初始化豆失敗;嵌套異常是org.springframework.beans.TypeMismatchException:未能將屬性'annotatedClasses'的類型[java.util.ArrayList]的屬性值轉換爲所需類型[java.lang.Class []];嵌套的例外是java.lang.IllegalArgumentException異常:找不到類[domain.item.Format]

編輯:

package domain.item; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name="FORMAT") 
public class Format { 

    @Id 
    @Column(name="ID") 
    @GeneratedValue 
    private int id; 

    @Column(name="NAME") 
    private String name; 

    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; 
    } 
} 
+0

老實說,你最好再次使用Spring Boot項目重新啓動並將代碼轉儲到其中。您將取消所有XML配置,即使您的pom將通過引導管理依賴版本得到極大的簡化 - 相信我;這將是值得的努力。 –

回答

0

請參見下面的事情:

  1. 檢查Format類和其他域類也正確註釋。

  2. 檢查組件掃描是否正確掃描了存在這些domain classes的所需軟件包。

或者你也可以添加以下屬性:SessionFactory的bean中

<property name="packagesToScan"> 
    <list> 
     <value>domain.item</value> 
    </list> 
</property> 

掃描所有註解類和使用AnnotationSessionFactoryBean創建SessionFactory的

+0

嗨,感謝您的回覆,請您詳細解釋一下您的#2關於如何正確添加組件掃描的更多細節?我嘗試在我的Format類中添加標籤@Component,但這沒有幫助... – William

+0

您必須添加諸如來掃描您的域。項目包。另外如果你的域名是Entity在你的數據庫中映射一個表格,那麼你必須使用@Entity進行註釋。 – shankarsh15

+0

公平地確定這些東西都不相關,因爲'annotatedClasses'屬性被傳遞給'SessionFactory',並且它是Hibernate搜索類並分析它們。 –

1

我面臨同樣的問題yestarday然後我看到這個問題,但沒有一個正確的答案。所以在將來如果有人有這個錯誤這裏是解決方案。

我有同樣的問題。同樣的錯誤即將到來。問題出在pom.xml中。 如果您在pom.xml中多次添加相同的依賴關係,將會出現此錯誤。在我的情況下,我使用的是

<dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
<dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

哪些都是一樣的。當我刪除與組id id jstl的依賴關係時,錯誤消失了。 在您的情況下,您多次使用以下依賴項。

<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-expression</artifactId> 
     <version>4.2.6.RELEASE</version> 
    </dependency> 

刪除其中任何一個,並希望您的問題將得到解決。

相關問題