Coder Social home page Coder Social logo

Comments (3)

CODe5753 avatar CODe5753 commented on September 25, 2024 1

request scope의 생존 주기는 알겠는데, Controller에서 호출하고 Service에서 호출 할 때 내 Bean인지 어떻게 구분하는걸까? 그 사이에 다른 사람들도 Bean 호출을 하게되면 빈 컨테이너에 MyLogger 빈이 쌓일텐데 어떻게 구분할 수 있을까? 마치 우리가 로그의 UUID를 보고 고유한 값을 판단하는 것 처럼 ObjectProvider는 어떻게 getObject를 할 때 이것이 구분될까 만약 이게 가능하려면 구분하는 방법이 있거나, 각 스레드별로 빈이 관리되어야 할텐데 이게 가능한가

HTTP request 요청마다 request scope bean이 생성되고 해당 bean은 각 요청별로 고유하다는 것 까지 이해했다. 그렇다면, 해당 빈은 서버에서 어떻게 저장되길래 구분이 되는걸까?

public abstract class AbstractRequestAttributesScope implements Scope {

	@Override
	public Object get(String name, ObjectFactory<?> objectFactory) {
		RequestAttributes attributes = RequestContextHolder.currentRequestAttributes();
		Object scopedObject = attributes.getAttribute(name, getScope());
		if (scopedObject == null) {
			scopedObject = objectFactory.getObject();
			attributes.setAttribute(name, scopedObject, getScope());
			// Retrieve object again, registering it for implicit session attribute updates.
			// As a bonus, we also allow for potential decoration at the getAttribute level.
			Object retrievedObject = attributes.getAttribute(name, getScope());
			if (retrievedObject != null) {
				// Only proceed with retrieved object if still present (the expected case).
				// If it disappeared concurrently, we return our locally created instance.
				scopedObject = retrievedObject;
			}
		}
		return scopedObject;
	}
//... 중략
}
  • 위 로직은 현재 요청의 Attributes 객체를 가져와서 저장된 Request scope를 가지는 빈이 있는지 찾는 로직이다.

각 요청이 들어오게 되면 request scope를 가진 bean을 찾으면 생성된 빈이 없기 때문에 해당 빈을 생성한다.
이렇게 생성된 빈은 요청의 Attribute 저장공간에 이름을 key로 저장한다.

이후 해당 요청이 종료되기 전까지 한번더 빈을 호출하면 호출되는 빈의 스코프를 확인하고 만약 request scope면 해당 request의 attributes를 살펴보고 빈의 이름으로 저장된 빈이 있는지 확인한다. 그리고 빈이 존재하면 그것을 반환한다.

즉, 각 요청마다 rqeust scope로 생성된 빈을 들고 다니는 개념이고 요청이 종료됨과 동시에 사라진다.

따라서, 수 많은 요청들 속에서 특정 빈을 찾아내는 것이 아니라 현재 요청이 갖고 있는 attributes 속에서 특정 빈의 이름으로 빈을 가지고 있는지 확인하게 된다.

from spring-core-principles-basic.

CODe5753 avatar CODe5753 commented on September 25, 2024

request scope의 생존 주기는 알겠는데,
Controller에서 호출하고 Service에서 호출 할 때 내 Bean인지 어떻게 구분하는걸까?
그 사이에 다른 사람들도 Bean 호출을 하게되면 빈 컨테이너에 MyLogger 빈이 쌓일텐데 어떻게 구분할 수 있을까?
마치 우리가 로그의 UUID를 보고 고유한 값을 판단하는 것 처럼 ObjectProvider는 어떻게 getObject를 할 때 이것이 구분될까
만약 이게 가능하려면 구분하는 방법이 있거나, 각 스레드별로 빈이 관리되어야 할텐데 이게 가능한가

from spring-core-principles-basic.

CODe5753 avatar CODe5753 commented on September 25, 2024

request scope의 생존 주기는 알겠는데, Controller에서 호출하고 Service에서 호출 할 때 내 Bean인지 어떻게 구분하는걸까? 그 사이에 다른 사람들도 Bean 호출을 하게되면 빈 컨테이너에 MyLogger 빈이 쌓일텐데 어떻게 구분할 수 있을까? 마치 우리가 로그의 UUID를 보고 고유한 값을 판단하는 것 처럼 ObjectProvider는 어떻게 getObject를 할 때 이것이 구분될까 만약 이게 가능하려면 구분하는 방법이 있거나, 각 스레드별로 빈이 관리되어야 할텐데 이게 가능한가

HTTP request 요청마다 request scope bean이 생성되고 해당 bean은 각 요청별로 고유하다는 것 까지 이해했다.
그렇다면, 해당 빈은 서버에서 어떻게 저장되길래 구분이 되는걸까?

from spring-core-principles-basic.

Related Issues (7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.