Skip to content

Commit

Permalink
Autowired 理解
Browse files Browse the repository at this point in the history
  • Loading branch information
qinfuxiang committed Nov 24, 2021
1 parent 643fd72 commit 9d94c37
Showing 1 changed file with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
}

}

0 comments on commit 9d94c37

Please sign in to comment.