为了账号安全,请及时绑定邮箱和手机立即绑定

在配置xml文件時出現的問題

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [beans-b.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/context/index/CandidateComponentsIndexLoader

這個是在單元測試的時候出現的錯誤,好像顯示的是無法找到spring framework中的某個類,但是我在配置的過程中dataSource是沒問題的,

錯誤應該是出在EntityManagerFactory裏,但是那段代碼我是直接複製過來的,包掃描的包名也被我改了,所以出了什麽錯誤請大家指正

正在回答

2 回答

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDataTest {
    /**
     * 檢測連接石的正確性
     */
    private ApplicationContext ctx;
    @Before
    public void Setup(){
        System.out.println("setup!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        ctx = new ClassPathXmlApplicationContext("beans-b.xml");
        System.out.println("setup!!!");

    }
    @After
    public void tearDown(){
        ctx = null;
    }
    @Test
    public void testEntityManagementFactory(){

    }
}

這個是測試代碼

0 回复 有任何疑惑可以回复我~
<?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"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="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-1.3.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    <!-->jdbc 連接<!-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///test"/>
            <property name="username" value="root"/>
        </bean>
    <!--2 配置EntityManagerFactory-->
    <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>
        <property name="packagesToScan" value="com.example"/>

        <property name="jpaProperties">
            <props>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

    </bean>



    <!-->jpa 連接<!-->



</beans>

這是我的xml文件

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

在配置xml文件時出現的問題

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信