spring 단일 사례 와 여러 사례 에 대한 상세 한 설명.어떻게 단일 사례 에서 여러 개의 대상 을 호출 합 니까?

spring 생 성 대상 은 기본적으로 단일 예 입 니 다.scope 속성 을 통 해 여러 예 로 변경 할 수 있 습 니 다.
<bean id="user" class="modle.User" scope="prototype">
	</bean>

지금 은 또 이런 상황 이다.
User 클래스 는 하나의 service 를 호출 하고, 이 service 는 또 하나의 tool 을 호출 합 니 다.
때때로 우 리 는 User 가 여러 번, service 는 한 번, tool 은 여러 번 이 기 를 바 랍 니 다.
자 연 스 럽 게 프로필 이 라 고 생각 하고 쓰 는 거 예요.
	<bean id="user" class="modle.User" scope="prototype">
		<property name="service" ref="userservice"></property>
	</bean>
	
	<bean id="userservice" class="service.userService" >
		<property name="tool" ref="tool"></property>
	</bean>
	
	<bean id="tool" class="service.ToolImpl" scope="prototype"></bean>

하지만 이런 문법 은 틀 렸 다!spring 의 자동 주입 을 사용 할 수 없습니다!
서 비 스 는 하나의 예 이기 때문에 이런 방법의 결 과 는 User 여러 예, service 와 tool 이 모두 하나의 예 이다.(왜?)
홈 페이지 문서:
4.5.3 Singleton beans with prototype-bean dependencies

When you use singleton-scoped beans with dependencies on prototype beans, be aware that dependencies are resolved at instantiation time. Thus if you dependency-inject a prototype-scoped bean into a singleton-scoped bean, a new prototype bean is instantiated and then dependency-injected into the singleton bean. The prototype instance is the sole instance that is ever supplied to the singleton-scoped bean.

However, suppose you want the singleton-scoped bean to acquire a new instance of the prototype-scoped bean repeatedly at runtime. You cannot dependency-inject a prototype-scoped bean into your singleton bean, because that injection occurs only once, when the Spring container is instantiating the singleton bean and resolving and injecting its dependencies. If you need a new instance of a prototype bean at runtime more than once, see Section 4.4.6, “Method injection”

정확 한 쓰 기 는 tool 을 호출 할 때마다 새로운 tool 대상 을 만 드 는 것 입 니 다.하지만 우리 가 수 동 으로 new 를 할 수도 없고, BeanFactory 를 빌려 야 합 니 다.
public class User {

	private userService service;
	private int age;
	private Date date;
	private String name;
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	public Date getDate() {
		return date;
	}
	public void setDate(Date date) {
		this.date = date;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public userService getService() {
		return service;
	}
	public void setService(userService service) {
		this.service = service;
	}
	
}

UserService 구현 BeanFactoryAware 인터페이스 로 factory 획득
spring 의 자동 주입 을 사용 하지 않 기 때문에 set 방법 은 제거 해 야 합 니 다!
public class userService implements BeanFactoryAware{
	
	private Tool tool;
	private BeanFactory factory;
	public void service(){
		this.tool = (Tool)factory.getBean("tool");
		System.out.println(this+":service");
		tool.work();
	}
	public Tool getTool() {
		
		return tool;
	}
//	public void setTool(Tool tool) {
//		
//		this.tool = (Tool)factory.getBean("tool");
//	}
	public void setBeanFactory(BeanFactory f) throws BeansException {
		factory = f;
	}
	
}

설정 파일, 주입 을 다시 사용 할 수 없습니다.그래서 tool 대상 의 주입 을 제거 해 야 합 니 다!
	<bean id="user" class="modle.User" scope="prototype">
		<property name="service" ref="userservice"></property>
	</bean>
	
	<bean id="userservice" class="service.userService" >
	</bean>
	
	<bean id="tool" class="service.ToolImpl" scope="prototype"></bean>
public interface Tool {
	public void work();
}
public class ToolImpl implements Tool{

	public void work() {
		System.out.println(this+":Tool Work");
	}
	
}

테스트 클래스:
public class Test {
	public static void main(String[] args) {
		ClassPathResource res = new ClassPathResource("applicationContext.xml");
		XmlBeanFactory factory = new XmlBeanFactory(res);
		User user = (User)factory.getBean("user");
		User user2 =  (User)factory.getBean("user");
		
		System.out.println(user);
		user.getService().service();
		System.out.println();
		System.out.println(user2);
		user2.getService().service();
	}
}

Output:
modle.User@42552c
service.userService@19e15c:service
service.ToolImpl@11a75a2:Tool Work
modle.User@210b5b
service.userService@19e15c:service
service.ToolImpl@170888e:Tool Work

좋은 웹페이지 즐겨찾기