Add in the program entry:
@SpringBootApplicationpublic class Springbootfirstapplication {public static void Main (string[] args) { Springapplication.run (Springbootfirstapplication.class, args); } @Bean public Commandlinerunner Commandlinerunner (ApplicationContext ctx) { return args, { SYSTEM.OUT.PRINTLN ("Let's inspect the beans provided by Spring Boot:"); string[] Beannames = Ctx.getbeandefinitionnames (); Arrays.sort (beannames); for (String beanname:beannames) { System.out.println (beanname);}} ;} }
Program output:
Let's inspect the beans provided by Spring Boot:basicerrorcontroller beannamehandlermapping beannameviewresolver charact Erencodingfilter Commandlinerunner conventionerrorviewresolver defaultservlethandlermapping defaultViewResolver Dispatcherservlet dispatcherservletregistration Duplicateserverpropertiesdetector Embeddedservletcontainercustomizerbeanpostprocessor Error Errorattributes Errorpagecustomizer Errorpageregistrarbeanpostprocessor .....
When the program starts, Springboot automatically injects 40-50 beans.
Open annotations with @runwith () @SpringBootTest:
@RunWith (Springrunner.class) @SpringBootTest (webenvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public Class Hellocontrollerit { @LocalServerPort private int port; Private URL base; @Autowired private testresttemplate template; @Before public void SetUp () throws Exception { this.base = new URL ("http://localhost:" + port + "/"); } @Test public void Gethello () throws Exception { responseentity<string> response = Template.getforentity (Base.tostring (), string.class); Assertthat (Response.getbody (), Equalto ("Greetings from Spring boot!");} }
Running it will first open the Sprigboot project, then test, test through
Source Source
Enterprise Distribution Micro Service Cloud Springcloud springboot MyBatis (18) springboot which beans were injected at startup