企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**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); } ~~~