spring bean 생 성 세부 사항
일례 scope = "singleton", 기본 값, 즉 기본 값 은 단일 예 [service / dao / 도구 류] 입 니 다. scope = "prototype", 여러 예;[액 션 대상]
2) 언제 만 듭 니까?
scope="prototype" 대상 을 사용 할 때 만 대상 을 만 듭 니 다. scope="singleton" 시작 (용기 초기 화 전) 에 bean 이 만 들 어 졌 고 전체 응용 프로그램 은 하나 입 니 다.
3) 생 성 지연 여부
lazy-init="false" 기본 값 은 false, 생 성 지연 없 이 시작 할 때 대상 을 만 듭 니 다. lazy-init="true" 초기 화 지연, 대상 을 사용 할 때 만 대상 을 만 듭 니 다. (lazy - init 는 하나의 예 에 만 유효 합 니 다)
4) 대상 생 성 후 초기 화 / 폐기
init-method="init_user" [대상 에 대응 하 는 init user 방법, 대상 생 성 후 실행] destroy-method="destroy_user"
[용기 대상 의 destriy 방법 을 호출 할 때 실행 합 니 다. (용기 용 구현 클래스), scope = "singleton" 시 유효 합 니 다.]
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="userId" class="b_bean.User" scope="singleton" lazy-init="false"
init-method="init_user" destroy-method="destroy_user"></bean>
</beans>
package b_bean;
public class User {
private int id;
private String name;
public User() {
System.out.println("User ....");
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void init_user(){
System.out.println("init_user.....");
}
public void destroy_user(){
System.out.println("destroy_user.....");
}
}
package b_bean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CreateBean {
@Test
public void test(){
//ApplicationContext context=new ClassPathXmlApplicationContext("b_bean/applicationContext.xml");
// destroy-method,
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("b_bean/applicationContext.xml");
System.out.println("ApplicationContext ....");
User user1=(User) context.getBean("userId");
User user2=(User) context.getBean("userId");
System.out.println(user1);
System.out.println(user2);
context.destroy();
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.