我正在尝试执行集成测试,并且我的带有@Transactional注释的类无法自动连接到具有NoSuchBeanDefinitionException的测试类。我评论了它并检查了加载的bean列表,我的服务就在那里,只是没有注入。
org.springframework.beans.factory.BeanCreationException:创建名为“com.icsynergy.scim.service.DBIntegrationServiceTest”的bean时出错:注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.icsynergy.scim.service.DBIntegrationService com.icsynergy.scim.service.DBIntegrationServiceTest._service;嵌套异常是
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.icsynergy.scim.service.DBIntegrationService] 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)}
如果删除此注释,则会注入,但没有事务支持。
这是我的班级
@Slf4j
@Service(value = 'integrationService')
@EnableConfigurationProperties(DBIntegrationConfig.class)
//@Transactional
class DBIntegrationService implements IntegrationService, HealthIndicator {...
我尝试将其注入
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
class DBIntegrationServiceTest {
@Autowired
DBIntegrationService _service
...
以下是Application.class
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = [
"com.icsynergy.scim.repository",
'com.icsynergy.scim.service',
"com.icsynergy.scim.web",
"com.icsynergy.scim.config"
], excludeFilters = [
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = OktaSCIMService.class),
@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = DBIntegrationService.class)
])
@ImportResource("config.xml")
@EnableTransactionManagement
public class Application {
....
这是来自config.xml的片段
<bean name="integrationService" class="com.icsynergy.scim.service.DBIntegrationService"/>
<bean name="service" class="com.icsynergy.scim.service.OktaSCIMService">
<property name="service" ref="integrationService"></property>
<property name="implementedUserManagementCapabilities">
<list value-type="com.okta.scim.server.capabilities.UserManagementCapabilities">
<value>GROUP_PUSH</value>
</list>
</property>
</bean>
编辑:找到了解决方法。由于我的服务实现了IntegrationService,因此注入它实际上有助于避免异常。仍然不明白我的原始注射有什么问题
将欣赏任何想法,TIA
答案 0 :(得分:1)
你最好使用接口而不是具体的类,所以你应该像这样编写代码:
@Autowired
IntegrationService _service
如果您没有界面,则应创建一个。