#35 springboot: my springboot
This commit is contained in:
13
springboot/embed/src/main/java/hello/MySpringBootMain.java
Normal file
13
springboot/embed/src/main/java/hello/MySpringBootMain.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package hello;
|
||||
|
||||
import hello.boot.MySpringApplication;
|
||||
import hello.boot.MySpringBootApplication;
|
||||
|
||||
@MySpringBootApplication
|
||||
public class MySpringBootMain {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("MySpringBootMain.main");
|
||||
MySpringApplication.run(MySpringBootMain.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package hello.boot;
|
||||
|
||||
import hello.spring.HelloConfig;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.connector.Connector;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package hello.boot;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@ComponentScan
|
||||
public @interface MySpringBootApplication {
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package hello.spring;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
//@Configuration
|
||||
public class HelloConfig {
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user