一、SpringBoot 註解SpringBootApplication 點進去看源碼java
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan( excludeFilters = {@Filter( type = FilterType.CUSTOM, classes = {TypeExcludeFilter.class} ), @Filter( type = FilterType.CUSTOM, classes = {AutoConfigurationExcludeFilter.class} )} ) public @interface SpringBootApplication { @AliasFor( annotation = EnableAutoConfiguration.class ) Class<?>[] exclude() default {}; @AliasFor( annotation = EnableAutoConfiguration.class ) String[] excludeName() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackages" ) String[] scanBasePackages() default {}; @AliasFor( annotation = ComponentScan.class, attribute = "basePackageClasses" ) Class<?>[] scanBasePackageClasses() default {}; }
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }
SpringBoot配置類至關於一個容器spring
@ComponetnScan 掃描包springboot
@Configration 侷限比較小app
均可以看成註解來使用裝載類url
SpringApplication.run方法來裝載類spa
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) { return (new SpringApplication(primarySources)).run(args); }
返回一個ConfigurableApplicationContext 對象對象
@Bean public Runnable Cretebean(){ return () -> {System.out.println("springboot run");}; } public static void main(String[] args) { ConfigurableApplicationContext context=SpringApplication.run(DemoApplication.class, args); context.getBean(Runnable.class).run(); }
二、SpringBoot加載配置文件blog
SpringBoot默認配置文件的名字爲aplication.propertiesip
當默認的名字發生變更的時候,get
例如
/*當配置文件發生變化的時候 app.propertory 能夠使用 spring.config.name=app 來進行獲取 */ @Component public class Config { @Value("${local.host}") private String local_host; public void see(){ System.out.println("local_host="+local_host); } }
三、如何獲取配置文件的屬性
一、有2種方式
@Environment
SpringApplication.run(Class<?> primarySource, String... args).getEnviroment().getPropertory(" ");
System.out.println(SpringApplication.run(DemoApplication.class, args).getEnvironment().getProperty("local.ip"));
二、@Value
@Component public class MyBean { @Autowired private Environment ev; @Value("${local.port}") private String local_port; public void show(){ System.out.println("---------------"+ev.getProperty("local.ip")); System.out.println("------"+local_port); System.out.println("name="+ev.getProperty("name")); System.out.println("app.name"+ev.getProperty("app.name")); } }
四、在本地配置文件來讀取配置文件
@在系統配置裏面能夠使用
spring.config.name=app
spring.lconfig.location=classpath:conf/app.perproties ----獲取在conf文件下的配置文件,不可省略配置文件尾綴
spring.config.location=file:D:/app.propertory,若是須要同時使用,中間用,分割
例如
@在本地環境中配置,進行獲取配置文件
@PropertySource("classpath:app.priperties")
@propertySources({ @PropertySource("classpath:app.priperties"),@propertySource("file:D/app.propertory")}) 同時獲取多個配置文件用,分割
@Configuration /*@PropertySource("file:D:/config1.properties")*/ @PropertySources({@PropertySource("file:D:/config1.properties"),@PropertySource("classpath:jdbc.properties")}) public class config1 { @Value("${jdbc_url}") private String jdbc_url; @Value("${jdbc_username}") private String jdbc_username; @Value("${username}") private String username; @Value("password") private String password; public void speek(){ System.out.println("jdbc_url="+jdbc_url); System.out.println("jdbc_username="+jdbc_username); System.out.println("username="+username); System.out.println("password="+password); } }