질문하십시오
봄에 콩을 인스턴스화하는 것은 콩 수명주기의 중요한 부분입니다. 일반적으로 초기화 후에 Bean이 변경되지 않습니다.
그렇다면 스프링 인스턴스 콩의 과정은 무엇입니까? !
스프링 인스턴스테이션 Bean 프로세스 분석
Bean 객체를 얻으려면 먼저 Beanfactory의 getBean () 메소드를 통해이를 얻어야합니다. 이 기간 동안 Bean 객체를 인스턴스화하기 위해 일련의 단계가 전달됩니다.
1 단계 : Bean 인스턴스를 생성하려면 Bean의 기본 생성자 (물론 지정된 다른 생성자 일 수도 있음)를 호출하십시오. Bean1.
2 단계 : Bean의 속성 값이 Bean 구성 파일에 주입되는지 확인하십시오. 주입이있는 경우 Bean1 인스턴스를 기반으로 속성을 주입하고 원래 Bean1을 덮어 쓸어 새 Bean 인스턴스를 형성하십시오. Bean2.
3 단계 : Bean이 초기화 비 인터페이스를 구현하는지 확인하십시오. 이 인터페이스가 구현되면 AfterProperTiesset () 메소드를 호출하여 Bean2에서 해당 작업을 수행하고 Bean2를 덮어 쓰려면 새로운 Bean 인스턴스를 형성하십시오. Bean3.
4 단계 : Bean 구성 파일에 초기화 속성이 지정되어 있는지 확인하십시오. 지정된 경우이 속성의 해당 메소드를 호출하고 Bean3에서 해당 작업을 수행하고 Bean3을 덮어 쓰기 위해 새 인스턴스를 형성하십시오. Bean4.
위의 단계를 통해 스프링이 콩을 사용하면 콩이 끊임없이 변화하고 있음을 발견했습니다!
스프링 인스턴스테이션 빈 프로세스 코드 데모
위의 단계를 더 잘 설명하려면 다음 코드를 참조하십시오.
엔티티 클래스 :
/*** Entity Class*/Public Class Employee는 초기화 비안, 일회용 비안, BeannameAware {개인 문자열 ID; // 직원 번호 개인 문자열 이름; // 직원 이름 개인 문자열 섹스; // 직원 성별 개인 문자열 연령; // Employee Age Private String Dative; // 직원 개인 문자열 부서; // Public Employname; // BAN 이름 () {System.out.println ( "************ 1 단계 : Bean의 기본 생성자를 호출 *************"); this.id = "bean1 : g080405214"; system.out.println ( "bean1의 값 :" + this); system.out.println ( "*************************************************************************************************. Println ("********워 {System.out.println ( "bean2의 값 :" + this); System.out.println ( "************ 3 단계 : Bean이 초기화 인터페이스 *************"); this.name = "콩 3 : li Xiaohong"; this.sex = "bean3 : this.age ="Bean3 : 25 "; 값 : " + this);} public void init () {system.out.println ("************** 4 단계 : init-method가 Bean 구성 파일에 지정되어 있는지 확인하십시오. {System.out.println ( "************** 서비스 중지 *****************");} public void setbeanname (String arg0) {system.out.println ( "********* 설정 ************************"); this.beanname = "mybeanname" id) {this.id = id;} public string getName () {return name;} public void setName (문자열 이름) {this.name = name;} public String getSex () {return sex;} public void setsex (string sex) {this.sex = sex;} public string getage (} public void setage (thistage) {thistage =) getNativePlace () {return nativeplace;} public void setnativeplace (String void setnativeplace) {this.nativeplace = aviretplace;} public string getDepartment () {return department;} public void setDepartment (} public void setDepartment (문자열 부서) {department =} public string getBeanName () {retainname;}@revaintride (retainname;}). [id = " + id +", name = " + name +", sex = " + sex +", age = " + age +", nativeplace = " + aviationplace +", department = " + department +", beanname = " + beanname +"];}}도구 :
/** * Bean context tool class */public class BeanContextHelper {private static ApplicationContext _instance;static {if (_instance == null) _instance = buildApplicationContext();}private BeanContextHelper() {}/** * Rebuild the ApplicationContext object*/public static ApplicationContext buildApplicationContext() {return new classPathXmlApplicationContext ( "ApplicationContext-base.xml");}/*** ApplicationContext 객체를 가져옵니다*/public static applicationContext getApplicationContext () {return _instance;}}봄의 콩 구성 :
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" xmlns : context = "http://www.springframework.org/schema/context"xmlns : aop = "http://www.springframework.org/schema/aop"xmlns : tx = "http://www.spramwork.org/schema/schema/schema/schema. xsi : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.xsd http://www.springframework.org/schema/contet http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/sprring--5-2.5.xsd http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/tx/spring-2.5.xsd " <!-================ Spring Beanfactory에서 콩을 인스턴스화하는 과정-> <Bean id = "Employee"init-method = "init"destroy-method = "destroy"> <!-기본 부서는 R & D 부서입니다-> <property name = "department "> value> Bean2 : r & d 부서 </value> </bean>>/bean>>/bean>>/ban>>>> </bean>.
테스트 클래스 :
/*** BeanFactory Instantiation Bean Engineering Test Class*/Public Class Test {public static void main (String args []) {test = new test (); test.test ();} public void test () {ApplicationContext Context = BeanContexThelper.getApplicationContext (); 직원). Context.getBean ( "Employee"); System.out.println ( "************** 봄부터 BeanFactory ***********"에 의해 얻은 최종 Bean 인스턴스; System.out.println ( "최종 Bean의 값 :" + Employee);}}실행 결과 :
************Step 1: Call the default constructor of the bean**************** The value of bean1: Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=null, beanName=null] ************Stand the name of the bean*******************Set the name of the bean******************* The value of bean2: Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=bean2: R&D department, beanName=myBeanName] ********* Step 3: Check whether the bean has implemented the InitializingBean interface******************* The value of bean3: Employee [id = bean1 : g080405214, name = bean3 : li Xiaohong, sex = bean3 : 여성, 나이 = bean3 : 25, NativePlace = null, department = bean2 : r & d 부서, beaname = myBeanName] ********* 단계가 Bean 구성 파일에 Init-Method가 지정되었는지 확인합니다. This attribute**************** The value of bean4: Employee [id=bean1:G080405214, name=bean3:Li Xiaohong, sex=bean3:female, age=bean3:25, nativePlace=bean3:Beijing, department=bean2:R&D department, beanName=myBeanName] *************The final bean instance obtained from Spring Beanfactory *************** 최종 Bean 값 : 직원 [id = bean1 : g080405214, name = bean3 : li Xiaohong, sex = bean3 : been3 : age = bean3 : 25, native place = bean3 : beijing, department = bean2 : r & d 부서, bean 이름 = mybeanname]
작업 결과를 판단하면 Bean 인스턴스화의 특정 과정에 대해 매우 분명해야합니다.
직원은 3 개의 인터페이스를 구현합니다.
초기화 비안 :이 인터페이스는 AfterProperTiesset () 메소드를 제공하며, 이는 초기화를 정의하는 기능을 Bean에게 제공하는 데 사용됩니다.
처분 가능 비인 :이 인터페이스는 Despare () 메소드를 제공하며, Bean 인스턴스가 파괴되기 전에 작동 함수를 제공하는 데 사용됩니다.
Beannameaware :이 인터페이스는 SetBeanName () 메소드를 제공하며, 이는 Bean 이름을 설정하는 기능을 제공하는 데 사용됩니다. 위의 실행 결과 에서이 방법은 두 번째 단계에서 수행됩니다.
요약
위는 봄에 콩을 인스턴스화하는 과정에 대한이 기사의 전체 내용입니다. 모든 사람에게 도움이되기를 바랍니다. 관심있는 친구들은이 사이트를 계속 참조 할 수 있습니다.
" 봄에 콩 획득을 사용자 정의하는 두 가지 방법 "
" Java의 스프링 주석 구성 Bean 인스턴스 코드 분석 "
" 스프링 구성 및 사용의 수명주기에 대한 자세한 설명 "
단점이 있으면 메시지를 남겨 두십시오. 이 사이트를 지원해 주신 친구들에게 감사드립니다!