SpringStudy

- 生命周期学习
This commit is contained in:
2026-09-28 09:10:52 +08:00
parent df62e8c01c
commit 397af979d1
4 changed files with 93 additions and 0 deletions
@@ -0,0 +1,47 @@
package com.ycjy;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class LifecycleDemo implements BeanFactoryAware, InitializingBean, DisposableBean {
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("111" + beanFactory);
}
@Override
public void destroy() throws Exception {
System.out.println("--destroy捣毁, 销毁");
}
public void xiaohui() {
System.out.println("--xiaohui销毁");
}
private String p1;
private String p2;
public void setP1(String p1) {
System.out.println("装配属性p1");
this.p1 = p1;
}
public void setP2(String p2) {
System.out.println("装配属性p2");
this.p2 = p2;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("--afterPropertiesSet属性设置之后");
}
public void init() {
System.out.println("初始化init");
System.out.println("--p1 " + p1);
System.out.println("--p2 " + p2);
}
}
@@ -0,0 +1,11 @@
package com.ycjy;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
context.getBean("lifecycle", LifecycleDemo.class);
context.close();// 上下文关闭
}
}