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

来源:3-1 开发环境搭建

慕UI7532152

2018-11-21 19:44

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回答

  • 慕UI7532152
    2018-11-21 20:05:35
    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(){
    
        }
    }

    這個是測試代碼

  • 慕UI7532152
    2018-11-21 19:46:43
    <?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文件

轻松愉快之玩转SpringData

利用Spring Data提高开发效率,提升程序员的幸福指数

34089 学习 · 119 问题

查看课程

相似问题