慕田峪0322561
2017-07-27 22:20
package com.oracle.repository; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.oracle.domain.Employee; public class TestRepository { private ApplicationContext ac = null; private EmployeeRepository employeeRepository=null; @Before public void setup(){ ac = new ClassPathXmlApplicationContext("beans.xml"); employeeRepository = (EmployeeRepository) ac.getBean("employeeRepository"); System.out.println("setup"); } @After public void teardown(){ ac = null; System.out.println("teardown"); } @Test public void testFindByName(){ Employee emp = employeeRepository.findByName("cyf2"); System.out.println(emp); } }
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 1 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="url" value="jdbc:mysql://localhost:3306/scott"/>
</bean>
<!-- <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean> -->
<!--配置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.oracle" />
<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>
</beans>
package com.oracle.repository;
import org.springframework.data.repository.Repository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.oracle.domain.Employee;
public interface EmployeeRepository extends Repository<Employee, Integer>{
public Employee findByName(String name);
}
ac = new ClassPathXmlApplicationContext("beans.xml");
这一行代码中,你需要改成
ac = new ClassPathXmlApplicationContext("beans-new.xml");
后面换了配置文件的
请试着从下面几方面解决问题:
1、是否将其EmployeeRepository配置到bean配置文件中
2、测试类中是否加载正确的bean配置文件
3、获取的bean的时候,是否为正确的bean名称
擦,是不是找到了你加载的beans.xml文件名称错了
这个也不行,有没有谁知道其他方法吗?谢谢
试试 ac.getBean(EmployeeRepository.class)
轻松愉快之玩转SpringData
34089 学习 · 119 问题
相似问题