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

自动连接失败:不是托管类型

昝唯
2023-03-14

我有一个很大的问题在我的文凭项目,将非常高兴,如果你们能帮助我!我做了一个Maven多模块项目,有3个“核心项目”

  • naviclean:(父级)
  • NavicleAndomain:包含具有所有实体的域模型和NavicleanServerNavicleAncleIntHessianProtocol
  • 所需的接口 MeinRemotedIenst
  • NavicleanClient:连接到NavicleanServer
  • 的GUI和Hessian连接
  • NavicleanServer:以下是我的存储库、我与DB的连接以及接口eInRemotedIenstNavicleanServer&Navicleanclient的实现,在Maven中有NavicleanDomain作为依赖项。
ERROR: org.springframework.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'transaktionsRepository': 
Injection of persistence dependencies failed; 
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: 
Error loading class [at.naviclean.service.impl.MeinRemoteDienstImpl] for bean with name 'meinRemoteDienstImpl' defined in file [C:\Users\Fredy\Documents\workspace-sts-3.1.0.RELEASE\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\NaviCleanServer\WEB-INF\classes\at\naviclean\service\impl\MeinRemoteDienstImpl.class]: 
problem with class file or dependent class; 
nested exception is java.lang.NoClassDefFoundError: at/naviclean/service/MeinRemoteDienst
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    ……………….

模型库:

package at.naviclean.domain;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Version;

@SuppressWarnings("serial")
@MappedSuperclass
public class ModelBase implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "ts")
    private Date timestamp;

    public Long getId() {
        return id;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

}

卡萨:

package at.naviclean.domain;

import javax.persistence.Column;
import javax.persistence.Entity;

@SuppressWarnings("serial")
@Entity
public class Kassa extends ModelBase {

    @Column(name = "name", unique = true)
    private String name;

    @Column(name = "geld")
    private int geld;

    public Kassa(String name, int geld) {
        this.name = name;
        this.geld = geld;
    }

    public Kassa() {
    }

    public String getName() {
        return name;
    }

    public int getGeld() {
        return geld;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setGeld(int geld) {
        this.geld = geld;
    }

}

MeinRemotedIenst:

package at.naviclean.service;

import at.naviclean.domain.Kassa;

public interface MeinRemoteDienst {

    int getKassaCount(int plus);

    String getNameFromKassa(int id);

    Kassa findById(int id);
}
package at.naviclean.repositories;

import org.springframework.data.jpa.repository.JpaRepository;

import at.naviclean.domain.ModelBase;

public interface BaseRepository<T extends ModelBase> extends
        JpaRepository<T, Long> {
    T findById(long id);

}
package at.naviclean.repositories;

import java.util.List;

import org.springframework.data.jpa.repository.Query;

import at.naviclean.domain.Kassa;

public interface KassaRepository extends BaseRepository<Kassa> {
    List<Kassa> findByGeld(int geld);

    Kassa findByName(String name);

    @Query("select k from Kassa k where k.geld = ?1")
    Kassa findByGeld1(int geld);
}
package at.naviclean.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.naviclean.domain.Kassa;
import at.naviclean.repositories.KassaRepository;
import at.naviclean.service.MeinRemoteDienst;

@Service
public class MeinRemoteDienstImpl implements MeinRemoteDienst {

    @Autowired(required = true)
    public KassaRepository kassaR;

    public int getKassaCount(int plus) {
        return 2;
    }


    public String getNameFromKassa(int id) {
        return kassaR.findById(id + 0l).getName();
    }

    @Override
    public Kassa findById(int id) {
        return = kassaR.findById(id + 0l);
    }

}
<?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:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:repository="http://www.springframework.org/schema/data/repository"
    xsi:schemaLocation="http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <import resource="infrastructures.xml" />

    <jpa:repositories base-package="at.naviclean.repositories">
        <repository:exclude-filter type="regex"
            expression="at.naviclean.repositories.BaseRepository" />
    </jpa:repositories>

    <context:component-scan base-package="at.naviclean.service.impl" />

</beans>

infrastructures.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:jdbc="http://www.springframework.org/schema/jdbc"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">



        <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="jpaVendorAdapter">
                        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                                <property name="showSql" value="true" />
                                <property name="generateDdl" value="true" />
                                <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
                        </bean>
                </property>
        </bean>

        <bean id="dataSource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost/kassatest" />
                <property name="username" value="root" />
                <property name="password" value="" />
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>


        <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

</beans>

servlet-context.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:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:c="http://www.springframework.org/schema/c"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">



<import resource="../root-context.xml" />
    <bean id="idMeinRemoteDienst" class="at.naviclean.service.impl.MeinRemoteDienstImpl" />
    <bean name="/MeinRemoteDienstHessian"
        class="org.springframework.remoting.caucho.HessianServiceExporter"
        p:serviceInterface="at.naviclean.service.MeinRemoteDienst"
        p:service-ref="idMeinRemoteDienst" />

</beans>

root-context.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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <import resource="classpath:META-INF/spring/application-context.xml" />

</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

    <!-- The definition of the Root Spring Container shared by all Servlets 
        and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>

    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>/MeinRemoteDienstHessian</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>/MeinRemoteDienstHessian</servlet-name>
        <url-pattern>/remoting/*</url-pattern>
    </servlet-mapping>

</web-app>
package at.spengergasse.kassa;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import at.naviclean.domain.Kassa;
import at.naviclean.repositories.KassaRepository;

@ContextConfiguration("classpath:META-INF/spring/application-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class KassaTest {

    @Autowired(required = true)
    private KassaRepository kassaR;

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void findByIdTest() {
        Kassa k = kassaR.findById(2);

        assertThat(k, is(not(nullValue())));
    }

    @Test
    public void findByGeld() {
        Kassa k = kassaR.findByGeld1(1200);

        assertThat(k, is(not(nullValue())));
    }

    @Test
    public void test() {
        Kassa vorher = new Kassa("ssf", 222);
        kassaR.save(vorher);
        Kassa nachher = kassaR.findById(vorher.getId());
        kassaR.delete(nachher);
        assertThat(vorher.getId(), is(equalTo(nachher.getId())));
    }

}
ERROR: org.springframework.test.context.TestContextManager - 
Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@41e22632] to prepare test instance [at.spengergasse.kassa.KassaTest@6639be68]
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'meinRemoteDienstImpl': 
**Injection of autowired dependencies failed**; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Could not autowire field: public at.naviclean.repositories.KassaRepository at.naviclean.service.impl.MeinRemoteDienstImpl.kassaR; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'kassaRepository': FactoryBean threw exception on object creation; 
nested exception is java.lang.IllegalArgumentException: **Not an managed type: class at.naviclean.domain.Kassa**
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    ………..

多谢!!!(我无法想象没有你会是什么样子:)

共有1个答案

谭正谊
2023-03-14

我从Oliver Gierke那里得到了一个很有帮助的建议:

最后一个异常实际上表明JPA设置有问题。“不是托管bean”意味着JPA提供者不知道的类型。如果您正在设置一个基于Spring的JPA应用程序,我建议您在LocalContainerEntityManagerFactory上配置“PackageStoscan”属性,该属性已配置到包含JPA实体的包中。或者,您也可以在persistence.xml中列出所有实体类,但这通常比较麻烦。

您得到的前一个错误(NoClassDefFound)表示提到的类在项目类路径上不可用。所以您可能需要检查模块间的依赖关系。由于这两个相关的类似乎位于同一个模块中,因此不完全部署到Tomcat也可能是一个问题(WTP有时有点麻烦)。我绝对推荐运行一个测试来进行验证(就像您已经做的那样)。这似乎会导致您遇到一个不同的异常,我想这确实是一些Eclipse故障

谢谢!

 类似资料:
  • 问题内容: 我的文凭项目有一个大问题,如果你们能帮助我,我将非常高兴!我做了一个Maven多模块项目,有3个“核心项目” :(父母) :包含所有的实体和接口域模型是由需要 和对 :将GUI和Hessian关联起来 :这里是我的仓库,我到数据库连接和接口的实现 和具有在Maven作为依赖。 现在,每次尝试在Tomcat上启动服务器时,都会出现以下错误: 模型库: 卡萨: MeinRemoteDien

  • 我在[本教程]后面有个问题(https://hub.docker.com/r/microsoft/mssql-server-linux/)我试图通过sqlcmd连接到docker托管的MSSQL。 我从windows在PowerShell中执行了以下操作: 注意:添加了“-it”和“/bin/bash”,因为如果没有检测到任何活动,docker将自动停止。 我运行了docker container

  • 错误:无法启动连接:错误:WebSocket无法连接。在服务器上找不到连接,endpoint可能不是信号器endpoint,服务器上不存在连接ID,或者存在阻止WebSocket的代理。如果有多台服务器,请检查是否启用了粘性会话。 WebSocketTransport.js:49WebSocket连接到“ws://xxxxxx/生产/网络服务/集线器/spreadhub”失败: Angular.t

  • 我正在使用和。它在这里 我有一个像这样的域名。而且,似乎不推荐使用注释,所以我使用了。 类如下所示 使用该类的如下所示 但当我启动应用程序时,我得到了错误 问题出在哪里?

  • 问题内容: 我使用Spring boot + JPA,启动服务时遇到问题。 这是Application.java文件, 我使用UCp进行连接池,下面是DataSource配置, UserDetailsService Implementation, Service layer implementation, The repository class, @Repository Entity class

  • 我正在创建两个不同的数据源的Spring引导应用程序。我已经为单独的数据库创建了配置文件。对于每个数据库,实体在不同的包中,模型在不同的包中。当我跑的时候 mvn清洁安装 它正确地创建数据库和所有表。但是在创建存储库时总是失败。下面我提供所有必要的细节: 主类 Application.Properties 有人能帮忙吗?我缺少了什么参数,或者我在这里做错了什么?提前感谢!