I agree to what @fabian has said. You can use @Named annotation to set the bean name. If bean name doesn't matches, it falls back to auto-wiring by type.
I tried couple of examples. They worked for me.
AppConfig.java
package com.named;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
public class AppConfig {
}
NamedService.java
package com.named;
import javax.inject.Named;
@Named("namedTestDependency")
public class NamedService {
public void namedMethod(){
System.out.println("Named method");
}
}
NamedServiceTest.java
package com.named;
import static org.junit.Assert.assertNotNull;
import com.named.AppConfig;
import com.named.NamedService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class)
public class NamedServiceTest {
//Matched by name of dependency
@Autowired
private NamedService namedTestDependency;
//Falls back to auto-wiring by type
@Autowired
private NamedService noDeclaration;
@Test
public void testAutowiring(){
assertNotNull(namedTestDependency);
assertNotNull(noDeclaration);
}
}