ServletContainer와 SpringContainer는 무엇이 다른가?

Controller 1개는 어떻게 수십 만개의 요청을 처리하는가

Controller 1개는 어떻게 수십 만개의 요청을 처리하는가

공유는 공유다. 공유를 덕질하려고 locking 할 필요는 없다. 당신은 공유의 잘생긴 얼굴 상태를 바꿀 수도 없고 그럴 자격도 없다…

서블릿 컨테이너와 스프링 컨테이너는 어떻게 다른가?

Servlet에 대하여

public class MyServlet extends HttpServlet {
private Object thisIsNOTThreadSafe;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object thisIsThreadSafe;
}

ServletContainer에 대하여

https://ssup2.github.io/theory_analysis/Servlet_Servlet_Container/
Tomcat의 구조: https://velog.io/@hyunjae-lee/Tomcat-2-%EA%B5%AC%EC%A1%B0
https://12bme.tistory.com/555
https://12bme.tistory.com/555
https://stackoverflow.com/questions/27765547/how-does-a-single-servlet-handle-multiple-requests-from-client-side/27765697

Spring Container는 어떻게 생성되는가?

https://mossgreen.github.io/Servlet-Containers-and-Spring-Framework/
www.youtube.com/watch?v=2pBsXI01J6M&t=257s
https://12bme.tistory.com/555

Spring Boot는 어떻게 동작하는가?

https://12bme.tistory.com/555

rootApplicationContext vs childApplicationContext

https://live-everyday.tistory.com/164 https://kingofbackend.tistory.com/78
RootWebApplicationContext와 Servlet(child) WebApplicationContext는 서로 부모-자식 관계이다

Servlet은 어떻게 Spring Container를 look-up하나?

public class ServiceLocator {
private InitialContext ic;
private Map cache;
private static ServiceLocator serviceLocator;
static {
serviceLocator = new ServiceLocator();
}
private ServiceLocator() {
cacheMap = Collections.synchronizedMap(new HashMap());
}
public InitialContext getInitalContext() throws Exception {
try {
if (ic == null) {
ic = new InitialContext();
}
} catch (Exception e) {
throw e;
}
return ic;
}
public static ServiceLocator getInstance() {
return serviceLocator;
}

public EJBLocalHome getLocalHome(String jndiHomeName) throws Exception {
EJBLoclaHome home = null;
try {
if (cache.containsKey(jndiHomeName)) {
home = (EJBLocalHome)cache.get(jndiHomeName);
} else {
home = (EJBLocalHome)getInitialContext().lookup(jndiHomeName);
cache.put(jndiHomeName, home);
}
} catch (Exception e) {
throw new Exception(e.getMessage());
}
return home;
}
}
// Inject.java

@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
// SampleContainer.java

public class SampleContainer
{

public static <T> T getObject(Class<T> clazz)
{
T instance = createInstance(clazz);

for (Field field : clazz.getDeclaredFields())
{
if (field.getDeclaredAnnotation(Inject.class) != null)
{
Object filedInstance = createInstance(field.getType());
try
{
field.setAccessible(true);
field.set(instance, filedInstance);
}
catch (IllegalAccessException e)
{
throw new RuntimeException("fail to set field", e);
}
}
}

return instance;
}

private static <T> T createInstance(Class<T> clazz)
{
try
{
return clazz.getConstructor().newInstance();
}
catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e)
{
throw new RuntimeException("fail to create instance", e);
}
}
}
// SampleComponent.java

public class SampleComponent
{
@Inject
private SampleRepository sampleRepository;

public void doSomething()
{
List<String> results = sampleRepository.findAll();

for (String str : results)
{
System.out.println("result : " + str);
}
}
}

}
// SampleRepository.java

public class SampleRepository
{
public List<String> findAll()
{
return Arrays.asList("AA", "BB", "CC");
}
}
// Main.java

public class Main{

public static void main(String[] args)
{
SampleComponent sampleComponent = SampleContainer.getObject(SampleComponent.class);
sampleComponent.doSomething();
}
}

결론

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-basics

그래서 Servlet 없이 Spring MVC만 있으면 된다고 하는것은 비지니스 로직을 Spring을 통해 처리하겠다는것이지 Servlet이 필요없다는 얘기가 아니다.

Other References that has not been aforementioned

writing about technology