企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**一.单例测试** **1.xml配置,这里scope是singleton** ~~~ <bean id="user4" class="com.nobb.bean.User" init-method="init" destroy-method="destroy" scope="singleton"> </bean> ~~~ **2.测试代码** ~~~ 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); User user4 = (User)ac.getBean("user4"); User user5 = (User)ac.getBean("user4"); // ((ClassPathXmlApplicationContext) ac).close(); System.out.println(user4==user5); } ~~~ **3.测试结果** 初始化成功 true ***** **二.原型测试** **1.xml配置** ~~~ <bean id="user4" class="com.nobb.bean.User" init-method="init" destroy-method="destroy" scope="prototype"> </bean> ~~~ **2.测试代码** ~~~ 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); User user4 = (User)ac.getBean("user4"); User user5 = (User)ac.getBean("user4"); // ((ClassPathXmlApplicationContext) ac).close(); System.out.println(user4==user5); } ~~~ **3.测试结果** 初始化成功 false