From 9d94c37cf7d8130b4b9db7d5ec67703a3e42f0b0 Mon Sep 17 00:00:00 2001 From: qinfuxiang Date: Wed, 24 Nov 2021 17:24:59 +0800 Subject: [PATCH] =?UTF-8?q?Autowired=20=E7=90=86=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../autowired/AutowiredBootstrap.java | 35 +++++++++++++++---- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/fast-boot-examples/auto-configure/src/main/java/fast/boot/autoconfigure/autowired/AutowiredBootstrap.java b/fast-boot-examples/auto-configure/src/main/java/fast/boot/autoconfigure/autowired/AutowiredBootstrap.java index 526430d..3bfbc06 100644 --- a/fast-boot-examples/auto-configure/src/main/java/fast/boot/autoconfigure/autowired/AutowiredBootstrap.java +++ b/fast-boot-examples/auto-configure/src/main/java/fast/boot/autoconfigure/autowired/AutowiredBootstrap.java @@ -1,14 +1,35 @@ package fast.boot.autoconfigure.autowired; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; -@SpringBootApplication +@ComponentScan +@Configuration public class AutowiredBootstrap { public static void main(String[] args) { - ConfigurableApplicationContext context = SpringApplication.run(AutowiredBootstrap.class, args); - A a = context.getBean(A.class); - System.out.println(a); + AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AutowiredBootstrap.class); + A a = applicationContext.getBean(A.class); + a.test(); + applicationContext.registerShutdownHook(); } + + @Component + public class A { + + @Autowired + private B b; + + public void test() { + System.out.println(b); + } + + } + + @Component + public class B { + } + }