1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public interface IService { public void action(); }
public class ConfigurableStrategyDemo { public static IService createService() { try { Properties prop = new Properties(); String fileName = "data/c87/config.properties"; prop.load(new FileInputStream(fileName)); String className = prop.getProperty("service"); Class<?> cls = Class.forName(className); return (IService) cls.newInstance(); } catch(Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { IService service = createService(); service.action(); } }
service=shuo.laoma.dynamic.c87.ServiceB
|