首先,我们在一个类中写下与生命周期相关的多种元素
package com.jd.vo;
public class UserInfo {
static {
System.out.println("静态代码块");
}
{
System.out.println("非静态代码块");
}
private String name;
public String getName() {
System.out.println("getter方法");
return name;
}
public void setName(String name) {
System.out.println("setter方法");
this.name = name;
}
public UserInfo() {
System.out.println("构造方法");
}
public void init() {
System.out.println("init");
}
public void destroy() {
System.out.println("destroy");
}
}
并创建test类来测试其输出
package com.jd.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
Object