springBoot配置(-)
1、SpringBoot 注解SpringBootApplication 点进去看源码
@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配置类相当于一个容器
@ComponetnScan 扫描包
@Configration 局限比较小
都可以当作注解来使用装载类
SpringApplication.run方法来装载类
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();
}
2、SpringBoot加载配置文件
SpringBoot默认配置文件的名字为aplication.properties
当默认的名字发生变动的时候,
例如
/*当配置文件发生变化的时候 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);
}
}
3、如何获取配置文件的属性
1、有2种方式
@Environment
SpringApplication.run(Class<?> primarySource, String... args).getEnviroment().getPropertory(" ");
System.out.println(SpringApplication.run(DemoApplication.class, args).getEnvironment().getProperty("local.ip"));
2、@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"));
}
}
4、在本地配置文件来读取配置文件
@在系统配置里面可以使用
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);
}
}

浙公网安备 33010602011771号