合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
**1.工厂方法** ~~~ //动态工厂 public User user2(){ User u = new User(); u.setId(2); u.setName("test11"); return u; } ~~~ **2.xml配置** ~~~ <!--动态工厂--> <bean name="userFactory" class="com.nobb.factory.UserFactory"></bean> <bean name="user3" factory-bean="userFactory" factory-method="user2"></bean> ~~~ **3.测试代码** ~~~ public static void main(String[] args){ //类路径Xml应用上下文容器 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); // User user = (User)ac.getBean("user"); // System.out.println(user); // User user2 = (User)ac.getBean("user2"); // System.out.println(user2); User user3 = (User)ac.getBean("user3"); System.out.println(user3); } ~~~