Spring Boot 에서 자원 을 초기 화 하 는 몇 가지 방법
8327 단어 공부 하 다.
만약 에 이러한 수요 가 있다 고 가정 하면 프로젝트 시작 과정 에서 스 레 드 탱크 의 초기 화, 암호 화 인증서 로드 등 기능 을 완성 해 야 합 니 다. 어떻게 하 시 겠 습 니까?답 이 생각 나 지 않 으 면 계속 내 려 다 보 세 요.오늘 은 Spring Boot 에서 자원 을 초기 화 하 는 몇 가지 방식 을 소개 하여 여러분 이 이 문 제 를 해결 하고 대답 할 수 있 도록 도 와 드 립 니 다.
CommandLineRunner
MyCommandLineRunner CommandLineRunner 방법 을 실현 합 니 다. 이 방법 에서 논리 초기 화 run() 주 해 를 추가 하면 됩 니 다 @Component @Component public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("...init resources by implements CommandLineRunner"); } 『 8195 』 CommandLine Runner 인 터 페 이 스 를 실현 한 Component 는 모든 Spring Beans 초기 화 완료 후 SpringApplication. run () 이 실행 되 기 전에 완 료 됩 니 다.다음은 두 줄 의 인쇄 를 통 해 우리 의 테스트 를 검증 합 니 다.
} @SpringBootApplication public class DemoApplication { public static void main(String[] args) { System.out.println("... start SpringApplication.run()"); SpringApplication.run(DemoApplication.class, args); System.out.println("... end SpringApplication.run()"); } 콘 솔 인쇄 결 과 는 다음 과 같 습 니 다.
} ... start SpringApplication.run() . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.11.RELEASE) 。。。。。。( ) 2018-05-02 17:01:19.700 INFO 21236 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) ...init resources by implements CommandLineRunner 2018-05-02 17:01:19.708 INFO 21236 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.282 seconds (JVM running for 3.125) ApplicationRunner
... end SpringApplication.run() MyApplicationRunner 방법 을 실현 합 니 다. 이 방법 에서 논리 초기 화 ApplicationRunner 주 해 를 추가 하면 됩 니 다 run() @Component @Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("...init resources by implements ApplicationRunner"); * 8195 에서 볼 수 있 듯 이 ApplicationRunner 인 터 페 이 스 를 실현 하 는 것 과 CommandLine Runner 인 터 페 이 스 를 실현 하 는 것 을 통 해 프로젝트 의 초기 화 작업 을 완성 하고 똑 같은 효 과 를 실현 할 수 있 습 니 다.둘 사이 의 유일한 차이 점 은
} 방법 에서 자체 적 으로 가지 고 있 는 형 삼 이 다 르 고 CommandLine Runner 에 서 는 간단 한 } 형 삼 일 뿐 이 며, ApplicationRunner 는 ApplicationArguments 대상 을 포함 하여 더욱 풍부 한 프로젝트 정 보 를 얻 을 수 있다 는 것 이다.ApplicationArguments
@Order
프로젝트 에
run() 인터페이스의 초기 화 류 가 있 고 String... args 인터페이스의 초기 화 류 가 있다 면 어느 것 이 먼저 실 행 될 까요?테스트 에 따 르 면 답 은 ApplicationRunner 인터페이스의 초기 화 류 가 먼저 실 행 된 것 이 라 고 합 니 다. 그 이 유 는 여러분 이 지나치게 관심 을 가 질 필요 가 없다 고 생각 합 니 다.그러나 두 초기 화 클래스 간 의 기본 실행 순 서 를 바 꾸 려 면 CommandLineRunner 주 해 를 사용 하면 이 문 제 를 해결 하 는 데 도움 이 된다.@Order
ApplicationRunner @Order @Component @Order(1) public class MyCommandLineRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("...init resources by implements CommandLineRunner"); } } @Component @Order(2) public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments applicationArguments) throws Exception { System.out.println("...init resources by implements ApplicationRunner"); 마지막 으로 콘 솔 에서 다음 과 같이 인쇄 합 니 다.콘 솔 출력 을 통 해
} 주석 값 이 작 을 수록 초기 화 클래스 도 일찍 실 행 됩 니 다. } @Order 。。。。。。( ) 2018-05-02 17:27:31.450 INFO 28304 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) ...init resources by implements CommandLineRunner @PostConstruct
사용
...init resources by implements ApplicationRunner 주석 역시 우리 가 자원 의 초기 화 작업 을 완성 하 는 데 도움 을 줄 수 있다. 전 제 는 이러한 초기 화 작업 이 다른 Spring beans 의 초기 화 작업 에 의존 하지 않 아 도 된다 는 것 이다.@PostConstruct
2018-05-02 17:27:31.453 INFO 28304 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.086 seconds (JVM running for 2.977) 주 해 는 방법 에 쓰 이 는 것 을 볼 수 있 습 니 다. 방법 을 써 서 테스트 해 보 세 요. @PostConstruct @PostConstruct @PostConstruct public void postConstruct() { 프로젝트 를 시작 합 니 다. 콘 솔 에서 최종 인쇄 는 다음 과 같 습 니 다.
System.out.println("... PostConstruct"); } ... start SpringApplication.run() . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v1.5.11.RELEASE) 。。。。。。( ) ... PostConstruct 。。。。。。( ) 2018-05-02 17:40:22.300 INFO 29796 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http) ...init resources by implements CommandLineRunner ...init resources by implements ApplicationRunner 문말 소결
종합 적 으로
2018-05-02 17:40:22.303 INFO 29796 --- [ main] cn.mariojd.demo.DemoApplication : Started DemoApplication in 2.387 seconds (JVM running for 3.267) 주 해 를 사용 하여 초기 화 작업 을 하 는 순서 가 가장 빠 르 고 전 제 는 이런 작업 이 다른 Bean 의 초기 화 에 의존 해 서 는 안 된다 는 것 이다.주 해 를 추가 ... end SpringApplication.run() 하면 같은 등급 간 에 서로 다른 Bean 의 로드 순 서 를 바 꿀 수 있 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
자바 두 수의 최대 공약수 구하 기 (세 가지 방법)자바 두 수의 최대 공약수 구하 기 (세 가지 방법) 1. 역법 전에 저도 몰 랐 습 니 다. 인터넷 에서 찾 아 봤 는데 0 과 0 이 아 닌 수의 약 수 는 모두 이 0 이 아 닌 숫자 입 니 다. 결실 2. 전...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.