๋ถ€ํŠธ ํด๋ž˜์Šค ๋งŒ๋“œ๋Š” ๋ฒ•

2023. 4. 18. 09:15ใ†์ธํ”„๋Ÿฐ/์Šคํ”„๋ง ๋ถ€ํŠธ - ํ•ต์‹ฌ ์›๋ฆฌ์™€ ํ™œ์šฉ

728x90

 

๋ถ€ํŠธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์€ ์ฆ‰, ์ง€๊ธˆ๊นŒ์ง€ ์ง„ํ–‰ํ–ˆ๋˜ ๋‚ด์žฅ ํ†ฐ์บฃ ์‹คํ–‰, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ, ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋ก์˜ ๋ชจ๋“  ๊ณผ์ •์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. 

(๋ถ€ํŠธ ๋ง๊ทธ๋Œ€๋กœ ์‹œ์ž‘์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.) 

 

 

MySpringApplication 

public class MySpringApplication
{
   public static void run(Class configClass, String[] args)
   {
      System.out.println("MySpringApplication.run args = " + List.of(args));
      
      // ํ†ฐ์บฃ ์„ค์ •
      Tomcat tomcat = new Tomcat();
      Connector connector = new Connector();
      connector.setPort(8080);
      tomcat.setConnector(connector);
      
      // ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์„ค์ •
      AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
      appContext.register(configClass);
      
      // ์Šคํ”„๋ง MVC ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ ์ƒ์„ฑ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์—ฐ๊ฒฐ
      DispatcherServlet dispatcher = new DispatcherServlet(appContext);
      
      // ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ ๋“ฑ๋ก
      Context context = tomcat.addContext("", "/");
      File docBaseFile = new File(context.getDocBase());
      if (!docBaseFile.isAbsolute()) {
         docBaseFile = new File(
               ((org.apache.catalina.Host) context.getParent()).getAppBaseFile(), docBaseFile.getPath());
      }
      docBaseFile.mkdirs();
      tomcat.addServlet("", "dispatcher", dispatcher);
      context.addServletMappingDecoded("/", "dispatcher");
      try
      {
         tomcat.start();
      }
      catch (LifecycleException e)
      {
         throw new RuntimeException(e);
      }
   }
}

๊ธฐ์กด์— ๋‚ด์žฅ ํ†ฐ์บฃ ์„ค์ •ํ•˜๊ณ , ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑํ•ด์„œ ์ด๋ฅผ ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์— ๋“ฑ๋กํ•˜๊ณ  ์ด๋žฌ๋˜ ์ฝ”๋“œ๋ฅผ ๋ชจ์•„์„œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” MySpringApplication ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค. 

๊ทธ ๋‹ค์Œ MySpringApplication.run(). ์„ ์‹คํ–‰ํ•˜๋ฉด ๋ฐ”๋กœ ๋™์ž‘ํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋‹ค. 

 

Class configClass 

: ์Šคํ”„๋ง ์„ค์ •์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋„๋ก ํ–ˆ๋‹ค. 

 

args 

: main(args)๋ฅผ ์ „๋‹ฌ ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ 

 

tomcat.start 

: ์ €๋ฒˆ์—๋Š” ์˜ˆ์™ธ๋ฅผ ๋˜์กŒ๋Š”๋ฐ ์ด๋ฒˆ์—๋Š” ์˜ˆ์™ธ๋ฅผ catchํ•ด์„œ runtimeException์œผ๋กœ ๋ณ€๊ฒฝํ–ˆ๋‹ค. 

 

 

 

@MySpringBootApplication 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@ComponentScan
public @interface MySpringBootApplication
{
}

์–ด๋…ธํ…Œ์ด์…˜์„ ํ•˜๋‚˜ ๋งŒ๋“ค์—ˆ๊ณ  ์ด ์–ด๋…ธํ…Œ์ด์…˜์€ ์ปดํฌ๋„ŒํŠธ ์Šค์บ” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋’€๊ณ  ์‹œ์ž‘ํ•  ๋•Œ ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ์„œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. 

๊ทธ ๋‹ค์Œ ์ €๋ฒˆ์— ๋งŒ๋“ค์—ˆ๋˜ HelloConfig ํด๋ž˜์Šค์˜ @Configuration์–ด๋…ธํ…Œ์ด์…˜์€ ์ฃผ์„ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค. 

 

 

MySpringBootMain

@MySpringBootApplication
public class MySpringBootMain
{
   public static void main(String[] args)
   {
      System.out.println("MySpringBootMain.main");
      MySpringApplication.run(MySpringBootMain.class, args);
   }
}

์ด ํด๋ž˜์Šค์˜ ํŒŒ์ผ ์œ„์น˜๋Š” ์ค‘์š”ํ•œ๋ฐ ๊ฐ€์žฅ ์ƒ์œ„ ํŒจํ‚ค์ง€ hello์— ์œ„์น˜ํ•œ๋‹ค. 

์—ฌ๊ธฐ์— ์œ„์น˜ํ•ด์•ผ ํ•˜๋Š” ์ด์œ ๋Š” @MySpringBootApplication ์— ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์ด ์ถ”๊ฐ€๋˜์–ด ์žˆ๋Š”๋ฐ, ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๊ธฐ๋ณธ ๋™์ž‘์€ ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ํด๋ž˜์Šค์˜ ํ˜„์žฌ ํŒจํ‚ค์ง€๋ถ€ํ„ฐ ๊ทธ ํ•˜์œ„ ํŒจํ‚ค์ง€๋ฅผ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”์˜ ๋Œ€์ƒ์œผ๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. 

๋”ฐ๋ผ์„œ hello ํ•˜์œ„ ํŒจํ‚ค์ง€์— ์žˆ๋Š” HelloController๋ฅผ ์ปดํฌ๋„ŒํŠธ ์Šค์บ”ํ•œ๋‹ค. 

 

 

๐Ÿ’Œ ์ด๋ ‡๊ฒŒ ๋ถ€ํŠธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋ฉด ๋‚ด์žฅ ํ†ฐ์บฃ ์‹คํ–‰, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ, ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ, ์ปดํฌ๋„ŒํŠธ ์Šค์บ”๊นŒ์ง€ ๋ชจ๋“  ๊ธฐ๋Šฅ์ด ํ•œ๋ฒˆ์— ํŽธ๋ฆฌํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  ์ด ๋ถ€ํŠธ ํด๋ž˜์Šค๋Š” ์–ด๋””์„œ ๋งŽ์ด ๋ณธ ๊ฒƒ ๊ฐ™๋‹ค? ํ•  ์ˆ˜์žˆ๋Š”๋ฐ ์‹ค์ œ๋กœ ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๋ฉด ํ•ญ์ƒ ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ทธ ๋ฉ”์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋˜ ํด๋ž˜์Šค์ด๋‹ค!!! 

( ์–ด๋…ธํ…Œ์ด์…˜์— ์ข€ ๋” ๊ธฐ๋Šฅ์ด ์ถ”๊ฐ€๋˜์–ด์žˆ์„ ๊ฒƒ์ธ๋ฐ ๊ธฐ๋ณธ ๋™์ž‘์€ ์œ„์™€ ๊ฐ™๋‹ค.) 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90