A, 봄 객체 수명주기
노 인수 정적 비 정적 구성하는 방법으로 얻을 name 속성 생성자를 포함하는 제 클래스 및 메소드 세트를 생성 init가 파괴 및 방법, 및 각 방법은 해당 출력을 갖는 사용자 정보 :
package club.affengkuang.vo;
public class UserInfo {
private String name;
public UserInfo() {
System.out.println("构造方法");
}
{
System.out.println("非静态代码块");
}
static {
System.out.println("静态代码块");
}
public String getName() {
System.out.println("get方法");
return name;
}
public void setName(String name) {
System.out.println("set方法");
this.name = name;
}
public void init() {
System.out.println("init");
}
public void destroy() {
System.out.println("destroy");
}
}
이 클래스는 다음 특성 세트 제공 방법 INIT-방법 및 파괴 방식의 초기화 값 (application.xml을 할당하고 호 이름) 파괴 () 메소드에 첨가하고,이 두 특성 의미 객체가 생성되고 이 방법의 init ()를 호출 할 때 파괴를 () 메소드가 파괴입니다 전화 :
<?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.xsd">
<bean id="userInfo" class="club.affengkuang.vo.UserInfo" lazy-init="true" init-method="init" destroy-method="destroy">
<property name="name" value="Tom"></property>
</bean>
</beans>
그런 다음 테스트 클래스 IOC 컨테이너를 생성하고, 사용자 정보 객체, 콘솔에서 한 눈에 봄 객체의 다음 라이프 사이클을 구하는 방법
package club.affengkuang.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
Object object = applicationContext.getBean("userInfo");
System.out.println(object);
applicationContext.close();
}
}
자바 동일한 사용자 정보 취득 IOC 컨테이너의 목적은 초기화 처리에서 오브젝트들 :
첫 번째 통화 사용자 정보 클래스 정적 및 비 정적 블록 (정적 후 비 정적 실행 전에) 후 호출 생성자 사용자 정보 클래스 후 (예컨대 다음 초기화 수행 마무리 최종 객체를 생성하는 이름 지정을 위해 세트의 메소드를 호출 객체 생성의 전체 과정)하는 방법;
파괴 시간 () 객체가 파괴되는 경우가있어서, 컨테이너가 닫힐 때 IOC 때 실행된다.
두주의
파괴 () 메소드 만 단독의 경우 범위 속성치를 수행 할 것이다
: 역할 범위, 블로그 참조 자세한 범위 효과를
값이 범위를 프로토 타입으로 설정되어 있으면, 다음 테스트 클래스 코드 실행 () 메소드를 파괴하지 않을 것이다 :
<?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.xsd">
<bean id="userInfo" class="club.affengkuang.vo.UserInfo" lazy-init="true" scope="prototype" init-method="init" destroy-method="destroy">
<property name="name" value="Tom"></property>
</bean>
</beans>