Philipp Grigoryev
Philipp Grigoryev

Reputation: 2143

@Transactional annotated class can not be autowired

I'm trying to perform an integration test and my class with @Transactional annotation can't be autowired into a test class with NoSuchBeanDefinitionException. I commented it out and checked a list of loaded beans and my service is there, just not injected.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.icsynergy.scim.service.DBIntegrationServiceTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.icsynergy.scim.service.DBIntegrationService com.icsynergy.scim.service.DBIntegrationServiceTest._service; nested exception is

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

If I delete this annotation, it's injected but there is no transaction support.

Here is my class

@Slf4j
@Service(value = 'integrationService')
@EnableConfigurationProperties(DBIntegrationConfig.class)
//@Transactional
class DBIntegrationService implements IntegrationService, HealthIndicator {...

I try to inject it as

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
class DBIntegrationServiceTest {

@Autowired
DBIntegrationService _service
...

and below is 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 { 
....

and this is a fragment from 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>

EDIT: Found a workaround. As my service implements IntegrationService, so an injection of it actually helps to avoid an exception. Still don't understand what is wrong with my original injection

Will appreciate any ideas, TIA

Upvotes: 0

Views: 865

Answers (1)

xierui
xierui

Reputation: 1055

you would better to use interface but no the concrete class, so you should make your code like this:

@Autowired
IntegrationService _service

If you do not have the interface, you should create one.

Upvotes: 1

Related Questions