废话不多说,直接上代码,文末有源码,直接测试
package com.cj.interfaces;
public interface ITestService {
void test();
}
package com.cj.interfaces;
import org.springframework.stereotype.Service;
@Service("s1")
public class TestServiceImpl1 implements ITestService {
@Override
public void test() {
System.out.println("接口1实现类 ...");
}
}
package com.cj.interfaces;
import org.springframework.stereotype.Service;
@Service
public class TestServiceImpl2 implements ITestService {
@Override
public void test() {
System.out.println("接口2实现类 ...");
}
}
package com.cj.interfaces;
import org.springframework.stereotype.Service;
@Service("s3")
public class TestServiceImpl3 implements ITestService {
@Override
public void test() {
System.out.println("接口3实现类 ...");
}
}
上面就构造了一个接口,三个实现类
ok了吗?
package com.cj.interfaces;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ITestServiceTest {
//有指定bean注入名字的,使用bean的名字
//qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,我们修改调用代码,添加@Qualifier注解,
// 需要注意的是@Qualifier的参数名称必须为我们之前定义@Service注解的名称之一!
@Autowired
@Qualifier(value = "s1")
ITestService testService; //正常启动
//没有指定bean注入名字的,使用该类首字符小写的bean的名字
//使用默认的
@Resource(name = "testServiceImpl2")
ITestService testService2; //正常启动
//通过@Resource注入,根据@Service指定的名称区分
@Resource(name = "s3")
ITestService testService3; //正常启动
@Test
public void test1() {
testService.test();
testService2.test();
testService3.test();
}
}
上接口
package com.cj.interfaces;
public interface IPrimaryService {
void test();
}
package com.cj.interfaces;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
@Service
@Primary
public class TestPrimaryImpl1 implements IPrimaryService{
@Override
public void test() {
System.out.println("Primary 1");
}
}
package com.cj.interfaces;
import org.springframework.stereotype.Service;
@Service
public class TestPrimaryImpl2 implements IPrimaryService{
@Override
public void test() {
System.out.println("Primary 2");
}
}
@Autowired
IPrimaryService primaryService;
@Test
public void test1() {
primaryService.test();
}
为了和面区分,我们再来一个接口,分开讨论
package com.cj.interfaces;
public interface ITestServiceConditional {
void test();
}
package com.cj.interfaces;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
/**
* 使用配置文件控制注入:
* 我们需要在实现类上添加配置文件注解,使用配置文件控制该实现类是否生效
*/
@Service
@Configuration
@ConditionalOnProperty(prefix = "demo.test.service",name = "impl",havingValue = "one")
public class TestServiceImpl4 implements ITestServiceConditional {
@Override
public void test() {
System.out.println("接口4实现类 ...");
}
}
package com.cj.interfaces;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
@Service
@Configuration
@ConditionalOnProperty(prefix = "demo.test.service",name = "impl",havingValue = "two")
public class TestServiceImpl5 implements ITestServiceConditional {
@Override
public void test() {
System.out.println("接口5实现类 ...");
}
}
demo:
test:
service:
impl: one
@Autowired
ITestServiceConditional testServiceConditional;
@Test
public void test1() {
testServiceConditional.test();
}