package com.example.demo; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; @Component public class MyAppConfig { @Value("${local.ip2:900}") private String port; @Autowired private Environment env; public void show(){ System.out.println("---local.ip="+port); System.out.println("---local.ip="+env.getProperty("local.ip","9000")); } }
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication public class DemoApplication { @RequestMapping String index(){ return "hello world index"; } // @Bean // public Runnable createRunnable(){ // return ()->{ // System.out.println(Thread.currentThread().getName()+"run2---"); // System.out.println(" spring is running"); // }; // } public static void main(String[] args) { ConfigurableApplicationContext configurableApplicationContext=SpringApplication.run(DemoApplication.class, args); // // configurableApplicationContext.getBean(Runnable.class).run(); // System.out.println(Thread.currentThread().getName()+"1run---"); // System.out.println( configurableApplicationContext.getEnvironment().getProperty("local.ip")); configurableApplicationContext.getBean(MyAppConfig.class).show(); configurableApplicationContext.close(); } }