💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
**一.单例测试** **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