init-method:配置一個方法作為生命週期的初始化方法,Spring會在對象建立後立即調用。
destory-method:配置一個方法作為生命週期的銷毀方法,Spring會在關閉和銷毀所有對象前調用。
applicationContext.xml<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean name="user" class="cn.jjs.bean.User" init-method="init" destroy-method="destroy"></bean></beans>
User.javapackage cn.jjs.bean;public class User { public User() { System.out.println("User空參構造器"); } private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void init() { System.out.println("init方法"); } public void destroy() { System.out.println("destroy方法"); }}