為什么我的employeeRepository沒(méi)法成功注入???
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 配置數(shù)據(jù)源 -->
<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);
}
2017-12-01
ac?=?new?ClassPathXmlApplicationContext("beans.xml");
這一行代碼中,你需要改成
ac?=?new?ClassPathXmlApplicationContext("beans-new.xml");
后面換了配置文件的
2017-11-02
請(qǐng)?jiān)囍鴱南旅鎺追矫娼鉀Q問(wèn)題:
1、是否將其EmployeeRepository配置到bean配置文件中
2、測(cè)試類中是否加載正確的bean配置文件
3、獲取的bean的時(shí)候,是否為正確的bean名稱
2017-09-24
擦,是不是找到了你加載的beans.xml文件名稱錯(cuò)了
2017-09-24
這個(gè)也不行,有沒(méi)有誰(shuí)知道其他方法嗎?謝謝
2017-07-28
試試? ac.getBean(EmployeeRepository.class)