/**
 * @author xyf
 * @date 2021/7/8 13:29
 */
public interface BaseInterface {

 void method1();

 void method2();


}
Copy the code

/** * public enum beanNames {/** ** / IMP_TEST1("1", "test1"), IMP_TEST2("2","test2"); private String name; private String method; beanNames(String name, String method) { this.name = name; this.method = method; } public String getName() { return this.name; } public String getMethod() { return this.method; } /** * find data that matches the enumeration class, if there is no data that matches the enumeration class, give it the default, @return method */ public static String getMethod(String name){for(beanNames) value :beanNames.values()){ if (value.getName().equals(name)){ return value.getMethod(); } } return IMP_TEST1.getMethod(); }Copy the code

}


/* * set the name for the convenience, */ @Component("test1") public class impTest1 implements BaseInterface{@override public void */ @component ("test1") public class impTest1 implements BaseInterface{@override public void method1() { System.out.println("test001---method1"); } @Override public void method2() { System.out.println("test001---method2"); }}Copy the code

@Component("test2") public class impTest2 implements BaseInterface{ @Override public void method1() { System.out.println("test002---method1"); } @Override public void method2() { System.out.println("test002---method2"); }}Copy the code

// Note that this class needs to inject to get the applicationContext otherwise it will raise a null pointer exception when it gets the applicationContext @Component // Implement ApplicationContextAware ApplicaitonContext public class strategyFactort implements ApplicationContextAware {private ApplicationContext applicationContext; Public <T> T getAgent(Class<T> clazz, String beanName) { Return to conform to the class for the Map < String, T > maps = applicationContext. GetBeansOfType (clazz); return maps.get(beanName); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; }}Copy the code

Public class test {public static void main(String[] args) {// Scan components in the given package [spring_test_strategy], register bean definitions for these components, And automatically refresh the context. ApplicationContext applicationContext = new AnnotationConfigApplicationContext("spring_test_strategy"); testController testController = applicationContext.getBean(spring_test_strategy.testController.class); testController.test("1"); }}Copy the code

@Controller public class testController { private final strategyFactort strategyFactort; public testController(spring_test_strategy.strategyFactort strategyFactort) { this.strategyFactort = strategyFactort; } public void test(String name){ BaseInterface baseInterface = strategyFactort.getAgent(BaseInterface.class,beanNames.getMethod(name)); System.out.println(baseInterface); baseInterface.method1(); baseInterface.method2(); }}Copy the code