zoukankan      html  css  js  c++  java
  • springboot笔记

    1.每个springboot项目都会有一个名为*Application的入口类,入口类中有一个main方法,这个方法是一个标准的java应用的入口方法。在main方法中使用SpringApplication.run(*Application.class,args)来启动springboot项目

    2.@SpringBootApplication是Spring Boot的核心注解,启动项目时,Spring Boot会自动扫描@SpringBootApplication所在类的同级包及下级包中的Bean

    3.在SpringBootApplication启动类中,可以关闭特定的自动配置

      @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

    4.@PropertySource注解不支持加载.yml文件

    在spring中可以使用@PropertySource("classpath:property文件地址")注解指明配置文件,然后使用@Value注解注入值

    而在SpringBoot中可以直接使用@Value注解注入,

      1.当使用@Value注解的时候,不能new对象,而要使用依赖注入。这样才能获取到值。

           2.使用@Value注解时不能注解在静态(static)变量或final变量上

      直接使用@Value注解加载不了配置文件时:https://blog.csdn.net/qq_42274641/article/details/83107807

    SpringBoot还提供了基于类型安全的配置方式,通过@ConfigurationProperties将properties属性和一个Bean及其属性关联起来,如:

    /**
       *配置文件 application.properties
       * author.name=zhanghl
       * author.age=27
      **/
    @Component
    @ConfigurationProperties(prefix = "author")
    // @
    ConfigurationProperties(prefix = "author",locations = {"classpath:config/author.properties"}) 可以使用locations来指定配置文件的路径,application.properties 是默认的配置文件,所以不需要使用locations
    @Data
    public class Author {

    private String name;

    private Long age;
    }

    5.可以使用@ImportResource({"classpath:xxx.xml","classpath:aaa.xml"})来加载xml配置

    6.加载yml格式的配置文件:https://blog.csdn.net/u010922732/article/details/91048606

    public class PropertiesUtil {
    
        public static String active;
    
        static {
            try {
                ClassPathResource resource = new ClassPathResource("application.yml");
    //          直接读取properties文件
    //          Properties properties = PropertiesLoaderUtils.loadProperties(resource);
                YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
                yaml.setResources(resource);
                Properties properties = yaml.getObject();
                active = properties.getProperty("spring.profiles.active", "prod");
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }        
  • 相关阅读:
    RPC、HTTP、IDL
    秋招拿了7个offer,分享一些反思和经验
    “功能测试”到“测试开发”升级之路
    Github最全买房租房攻略,火了!
    王垠:怎样尊重一个程序员?
    为什么曾经优秀的人突然变得平庸?
    你这么努力,为什么能力提升还这么慢?
    打工人到什么状态,就可以离职了?
    怎样才能迅速晋升?
    如何做个优秀的经理?
  • 原文地址:https://www.cnblogs.com/zhlblogs/p/13268652.html
Copyright © 2011-2022 走看看