接口:
public interface Score {
String getString(String str);
}
实现类:
@Component("A")
public class ScoreA implements Score {
@Override
public String getString(String str){
return null;
}
}
使用:
@Component
public class GetScore {
static GetScore getScore ;
/**
* 初始化的时候将所有的Score自动加载到Map中
*/
@Autowired
Map<String,Score>strategys=new ConcurrentHashMap<>();
@PostConstruct
public void init(){
getScore =this;
getScore .strategys=this.strategys;
}
public static String getScore(String str){
//注意这个“A”跟实现类上面的声明一致
Score score=xinfuScore.strategys.get("A");
if(score==null){
throw new RuntimeException("未定义score");
}
return score.getScore(str);
}
}
基本代码就这些,可自行扩展。