企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**1.User.java** ~~~ package com.nobb.bean; public class User { private Integer id; private String name; public User(){ System.out.println("调用了无参构造"); } public void init(){ System.out.println("初始化成功"); } public void destroy(){ System.out.println("销毁成功") ; } ~~~ **2.xml配置** ~~~ <bean id="user4" class="com.nobb.bean.User" init-method="init" destroy-method="destroy"> </bean> ~~~ **3.测试代码** ~~~ public class App { 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"); ((ClassPathXmlApplicationContext) ac).close(); System.out.println(user4); } } ~~~ **4.测试结果** 调用了无参构造 初始化成功 销毁成功 User{id=null, name='null'}