How to extend @Service with spring

Below is an example class

@Service("testService")
public class TestService {

    public String something() {
        return "abc";
    }

}

I want to extend the class and let the container know that it needs to pick up my extended class now.

@Service("extendedTestService ")
public class ExtendedTestServiceMock extends TestService { 

    @override
    public String something() {
        return "xyz";
    }

}

Testing class

public class TestClass extends SpringTest {

    @Autowired
    @Qualifier ("extendedTestService")
     private ExtendedTestService testService;

    public void testMethod () {
        ......
    }

}
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [TestService] is defined: expected single matching bean but found 2: ExtendedTestServiceMock, testService 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency (DefaultListableBeanFactory.java:865) ~ [spring-beans-3.2.8.RELEASE.jar: 3.2.8.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) ~[spring-beans-3.2.8.RELEASE.jar:3.2.8.RELEASE]
    ... 91 common frames omitted

?

+4
5

.

public interface TestService {
    String something();
}

:

@Service
@Qualifier("testService")
public class TestServiceImpl implements TestService { ... }


@Service
@Qualifier("testServiceMock")
public class TestServiceMockImpl implements TestService { ... }

:

public class TestClass extends SpringTest {

    @Autowired
    @Qualifier("extendedTestService")
    private TestService testService;    

    ...

}
+3

, , @Primary.

TestServiceMockImpl :

@Service("extendedTestService ")
@Primary
public class ExtendedTestServiceMock extends TestService { 

    @override
    public String something() {
        return "xyz";
    }
}

this @Primary

( - , @Primary ), Spring

Spring , , beans, .

+3

, xml.

BeanFactoryPostProcessor , , , .

public class SystemTestBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(final ConfigurableListableBeanFactory factory) throws BeansException
{
   //put your custom code in here
}

}

@DependsOn, , bean , bean

@Service("testService") @DependsOn("testService") public class ExtendedTestService extends TestService {

, .

+1

. Qualifier & Service.

0

, , , ConditionlOnProperty

:

@Service
@ConditionlOnProperty(value = "test.service.extension.enabled")
public class TestService {

}

@Service
@ConditionlOnProperty(value = "test.service.extension.enabled", havingValue = "false")
public class ExtendedTestServiceMock extends TestService {
}

, test.service.extension.enabled=true application.properties

0

All Articles