问答详情
源自:3-2 起步程序开发

为什么我的employeeRepository没法成功注入啊?

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

}


提问者:慕田峪0322561 2017-07-27 22:20

个回答

  • Mr_zhanghuadi
    2017-12-01 09:40:49

    ac = new ClassPathXmlApplicationContext("beans.xml");

    这一行代码中,你需要改成

    ac = new ClassPathXmlApplicationContext("beans-new.xml");

    后面换了配置文件的

  • 小明同学爱思考
    2017-11-02 19:37:34

    请试着从下面几方面解决问题:

    1、是否将其EmployeeRepository配置到bean配置文件中

    2、测试类中是否加载正确的bean配置文件

    3、获取的bean的时候,是否为正确的bean名称

  • 慕无忌126509
    2017-09-24 11:48:07

    擦,是不是找到了你加载的beans.xml文件名称错了

  • 慕无忌126509
    2017-09-24 11:44:14

    这个也不行,有没有谁知道其他方法吗?谢谢

  • 我是大树
    2017-07-28 20:46:16

    试试  ac.getBean(EmployeeRepository.class)