当前位置: 首页 > 知识库问答 >
问题:

创建在类路径资源中定义名为“sessionFactory”的bean时出错...找不到类

耿志义
2023-03-14

我有一个spring配置的项目运行得很好。然而,现在我尝试创建一个maven项目,并将所有包复制到新的maven项目中,当我在tomcat服务器上运行它时,我得到了一个例外:canno find class for my sessionFactoryBean。

我不明白为什么当类的路径明显正确时,我的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'在类路径资源中定义的bean时出错[spring.config.xml]:bean的初始化失败;嵌套异常org.springframework.beans.TypeMismatchException:无法将type[java.util.ArrayList]的属性值转换为属性'annotatedClass'的必需类型[java.lang.Class[]];嵌套异常java.lang.IllegalArgumentException:无法找到class[domain.item.格式]

编辑:

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

共有2个答案

杨高翰
2023-03-14

请看以下内容:

>

  • 检查Format类和其他域类是否注释正确。

    检查组件扫描是否正确扫描了存在这些域类的所需包。

    或者,也可以在下面添加属性:

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

    在SessionFactoryBean内部扫描所有注释类,并使用AnnotationSessionFactoryBean创建sessionfactory

  • 唐弘和
    2023-03-14

    我昨天也遇到了同样的问题,然后我看到了这个问题,但没有正确的答案。所以将来如果有人有这个错误,这里是解决方案。

    我也有同样的问题。同样的错误也来了。问题出在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> 
    

    这只不过是相同的。当我使用group id jstl删除依赖项时,错误消失了。在您的情况下,您多次使用以下依赖项。

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

    移除其中任何一个,希望你的问题会得到解决。

     类似资料: