如果您找不到错误,至少说是或否我已经执行了所有正确的步骤,这样至少我可以认为我的上下文 xml 文件中还有另一个错误
错误如下
原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有为依赖找到类型 [net.com.htts.transaction.service.mapping.FundService] 的匹配 bean:预计至少有 1 个 bean 有资格作为此依赖的自动装配候选者. 依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
我有一个无法加载存储库 bean 的 Junit 测试。
1)我创建了一个接口 FundRepository 视图 plaincopy 到剪贴板打印?注意:代码块中的文本内容是自动换行的包 net.com.htts.transaction.data.repository;
import org.springframework.stereotype.Repository;
import net.com.htts.transaction.data.entity.Fund;
@Repository
public interface FundRepository {
public void createFund(Fund fund);
}
package net.com.htts.transaction.data.repository;
import org.springframework.stereotype.Repository;
import net.com.htts.transaction.data.entity.Fund;
@Repository
public interface FundRepository {
public void createFund(Fund fund);
}
该接口具有@Repository
2)我创建了实现我的接口的 FundRepositoryImpl
查看plaincopy到剪贴板打印?注意:代码块中的文本内容是自动换行的包 net.com.htts.transaction.data.repository;
import java.util.List;
import net.com.htts.transaction.data.entity.Fund;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
public class FundRepositoryImpl implements FundRepository{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public void createFund(Fund fund) {
entityManager.persist(fund);
}
}
package net.com.htts.transaction.data.repository;
import java.util.List;
import net.com.htts.transaction.data.entity.Fund;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
public class FundRepositoryImpl implements FundRepository{
@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;
public void createFund(Fund fund) {
entityManager.persist(fund);
}
}
创建弹簧数据上下文
在这种情况下,我添加了以下内容
查看plaincopy到剪贴板打印?注意:代码块中的文本内容会自动换行
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
<!-- Scan the data layer -->
<context:component-scan base-package="net.com.htts.transaction.data" />
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
根据我读过的所有内容,这应该足以让我在 FundRepository 中使用 @Autowired。
Junit如下
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:/spring/fund-test-data.xml")
public class FundRepositoryTest {
@Autowired
private FundRepository repository;
@Test
public void createFund(){
}
}
上下文如下
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>net.com.htts.transaction.data</value>
<value>net.com.htts.transaction.data.repository</value>
</list>
</property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="generateDdl" value="true" />
</bean>
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.ejb.naming_strategy">
net.com.htts.transaction.data.naming.NamingStrategy</prop>
</props>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/funds" />
<property name="username" value="fund_user" />
<property name="password" value="fund_user" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!-- Scan the data layer -->
<context:component-scan base-package="net.com.htts.transaction.data" />
<!-- Scan the repositories -->
<jpa:repositories base-package="net.com.htts.transaction.data.repository" />
</beans>