Spring Boot 入门(1)[项目文件的开始,prop文件读取,bean的按条件自动装配]

开始

创建Springboot项目
  1. 在 pom.xml中引入spring boot的依赖
    在这里插入图片描述
  2. 创建入口类
      @SpringBootApplication
      public class App {
            @Bean
   			public Runable createRunable(){
   					return() ->{}
                  }

              public Runnable createRunnable(){
                    public static void main(String[] args){
                            ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
                             context.getBean(Runable.class).run();
                       }
   			}
       }  
  • 另一种启动方法
    在这里插入图片描述
    @SpringBootApplication 注解=>
    在这里插入图片描述
    @SpringBootApplication 与 @Configuration 效果一样
SpringBoot 读取配置
  • 方法(1):
    SpringBoot默认配置文件为application.properties,在resources下,新建这个文件。默认的位置在classpath根目录,或者classpath:/config ,file:/或file:/config。
    默认的配置文件名字可以自定义。在runtime配置中 使用 --spring.config.name来指定,只需要指定文件的名字,文件扩展名可以省略。
    默认配置文件路径可以使用: --spring.config.location来指定,配置文件指定全路径,包装目录和文件名字,还可以指定多个,多个逗号隔开。 指定方式:1:classpath: 2. file:。配置文件要全路径,多个文件逗号隔开
    读取:
  @SpringBootApplication
  public class App{
    public static void main(String[] args){
		ConfigurableApplicationContext context = SpringApplication.run(App.class,args);
			context.getEviroment().getProperty("[key]");
			context.close();
    }
  }
  • 方法(2):
    @Values("${local.port}") //local.port为一个properties文件中 key
    private String localPort; //类型可以自动转化到装在的变量。

    ======
    @Autowired
    private Enviroment env; //获取Enviroment对象
    env.getProperty(“local.ip”);
    env.getProperty(“local.ip”,Integer.class);

    @Values("${tomcat.port:9090}") //@Values 默认必须有配置文件,没有配置文件则必须要有默认值 :

    配置文件中也可以互相相互引用

    name = 111
    app.name = this is ${name}                =>this is 111
  • 方法(3):
    @PropertySource(“classpath:XXX.properties”) //or file:e://…
    多个 @PropertySource([@PropertySource(“classpath:XXX.properties”),])

  • 方法(4):
    @ConfigurationProperties注解,preifx设置前缀,类体绑定的变量要有get,set方法

-    @ConfigurationProperties(prefix="ds",locatioins="path:/")    //locations 为配置文件位置,可以为 file:  or classpath:
-    public class Data{
       
     }

  • 配置文件中注入集合和数组
  .properties ===========================
  		ds.porte[0]=111
  		  		ds.porte[1]=111
  		  		  		ds.porte[2]=111
  		  		  		  		ds.porte[3]=111
   class ===============================
   @Component
   @ConfigurationProperties("ds")
   public class  MyProperties{
		private List<String> hosts = new ArrayList<>();
		get();
		set();

  }
  • EnviromentPostProcessor 读取配置文件的扩展类接口
    使用:
    在这里插入图片描述
    文件要使用,必须在这个文件下注册
    在这里插入图片描述
    ===》
    在这里插入图片描述
不同环境下配置文件的读取选择
  • 编程方式制定配置环境
    在这里插入图片描述
  • 通过参数
    Run Configurations =>
    Arguments =>
    – spring.profiles.active=test,dev
    或者 spring.profiles=test //激活一个profile,默认激活不生效
    可以同时激活多个。
  • 不同环境下Bean的按环境装配
    @Profile("")
    可以用在类和方法上,根据启用的配置,控制bean和config的装配和生效。
    在这里插入图片描述
SpringBoot的自动配置

使用@Conditional 注解实现按条件自动装配,@Conditional 基于条件自动配置,一般配合Condition接口一起使用,只有接口的实现类返回true,才会装配。方法上,类上都可以使用。
可以传多个参数{ , }

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

SpringBoot默认提供了很对根据条件配置的Condition,可以根据类的存在,根据jdk版本,资源文件的存在与否进行配置。
@ConditionalOnProperty(name = “”,havingValue="") //某个配置项存在,或者值等于XX时,装配
@ConditionalOnClass(name=“com.soft…”)
//存在某个类时装配。

@ConditionalOnBean(name=“user”)
//容器中存在某个bean时,装配生效(OnMissBean)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值