gpt4 book ai didi

spring - Spring测试的 Autowiring 问题

转载 作者:行者123 更新时间:2023-11-28 20:12:36 25 4
gpt4 key购买 nike

我花了几个小时试图弄清楚为什么我的代码会抛出以下异常。在这一点上,我希望有人能比我更聪明,因为我正在失去希望......;)

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.ls.forecast.jpa.ForecastElementService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Quali fier(value=main)} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)

我看了很多帖子/教程,但都没有成功。他们似乎都与我的设置相同。我在服务的实现上添加了@Service,我添加了一个限定符,检查了 ForecastElementServiceImpl 是否实际实现了 ForecastElementService 接口(interface)。

服务接口(interface):

public interface ForecastElementService {

Collection<ForecastElement> retrieve(String date);
Collection<ForecastElement> retrieve();
}

接口(interface)实现:

@Stateless
@TransactionAttribute(TransactionAttributeType.REQUIRED)
@Repository("forecastElementService")
@Service
public class ForecastElementServiceImpl implements ForecastElementService {

@PersistenceContext
protected EntityManager em;

@Override
@Cacheable("forecastElements")
public Collection<ForecastElement> retrieve(String date) {
String sql = null;
if(date != null) {
sql = " SELECT fe FROM ForecastElement fe JOIN FETCH fe.forecastType WHERE ?1 between fe.startDate and fe.endDate";
} else {
sql = " SELECT fe FROM ForecastElement fe JOIN FETCH fe.forecastType";
}
final TypedQuery<ForecastElement> query = em.createQuery(sql, ForecastElement.class);

return query.getResultList();
}

@Override
@Cacheable("forecastElements")
public Collection<ForecastElement> retrieve() {

return retrieve(null);
}

上下文.xml:

<bean id="forecastElementService" class="com.ls.forecast.jpa.ForecastElementServiceImpl">
<property name="entityManagerFactory" ref="entityManagerFactory"/>

最后是故障测试类 - forecastElementService 变量是引发异常的变量。

@Repository
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"*-context.xml"})
public class ModelJpaTest extends AbstractTransactionalJUnit4SpringContextTests {

//final Logger logger = LoggerFactory.getLogger(ModelJpaTest.class);

@Autowired
protected ForecastElementService forecastElementService;

@Autowired
@Qualifier("basicDataSource")
@Override
public void setDataSource(DataSource dataSource) {
super.setDataSource(dataSource);
}

@Test
public void LoadModelTest() {
assertNotNull("forecastElementService is null", forecastElementService);
Collection<ForecastElement> elements = forecastElementService.retrieve();
assertTrue(elements.size() > 0);
}

}

任何见解或帮助将不胜感激!

请参阅@ContextConfiguration...如果我将 ModelJpaTest-context.xml 放在我的 Maven 项目的资源(测试)文件夹中并编辑为:

@ContextConfiguration({"/ModelJpaTest-context.xml"})

现在真的很迷茫......

最佳答案

我需要将注释配置和组件扫描添加到 applicationContext.xml,以便它可以正确地“ Autowiring ”bean。一旦完成,我就能够摆脱测试类 (forecastElementService) 中的变量。

关于spring - Spring测试的 Autowiring 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8128923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com