spring boot lombok 在 application.properties 指定配置文件

logging.config.包名=配置文件名
例如:
logging.config.com.example.demo=logback-spring.xml

logback-spring.xml 需要放在 resources目录下。

默认情况lombok使用的是logback,这里给一份参考的配置文件,注意需要修改日志输出的路径,不然会启动失败。

<?xml version="1.0" encoding="UTF-8"?>
<!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 -->
<!-- scan:当此属性设置为true时,配置文档如果发生改变,将会被重新加载,默认值为true -->
<!-- scanPeriod:设置监测配置文档是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。
                 当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
<!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
<configuration  scan="true" scanPeriod="10 seconds">
    <contextName>logback</contextName>

    <!-- name的值是变量的名称,value的值时变量定义的值。通过定义的值会被插入到logger上下文中。定义后,可以使“${}”来使用变量。 -->
    <property name="log.path" value="I:/logs/pmp" />

    <!--0. 日志格式和颜色渲染 -->
    <!-- 彩色日志依赖的渲染类 -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
    <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
    <conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
    <!-- 彩色日志格式 -->
    <property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>

    <!--1. 输出到控制台-->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <!--此日志appender是为开发使用,只配置最底级别,控制台输出的日志级别是大于或等于此级别的日志信息-->
        <filter class="ch.qos.logback.cl
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要读取application.properties配置文件,可以使用Spring框架提供的PropertiesLoaderUtils工具类。首先,需要导入相应的依赖包和类,并创建一个方法readPropertiesFile(String fileName)来读取配置文件。在该方法,使用ClassPathResource类来指定配置文件的路径,然后通过PropertiesLoaderUtils.loadProperties()方法加载配置文件并返回Properties对象。最后,通过getProperty()方法来获取具体的配置项的值。以下是一个示例代码: 引用: ```java import java.util.Properties; import org.springframework.core.io.Resource; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.support.PropertiesLoaderUtils; public static Properties readPropertiesFile(String fileName) { try { Resource resource = new ClassPathResource(fileName); Properties props = PropertiesLoaderUtils.loadProperties(resource); return props; } catch (Exception e) { System.out.println("读取配置文件:" + fileName + "异常,读取失败"); e.printStackTrace(); } return null; } Properties properties = readPropertiesFile("application.properties"); System.out.println(properties.getProperty("spring.rabbitmq.host")); ``` 此外,还可以通过创建配置类和属性类来读取配置文件。首先,创建一个@ConfigurationProperties注解的属性类,用于映射配置文件的属性值。然后,在配置类上使用@EnableConfigurationProperties注解,并指定属性类,以将属性类注册为配置类的属性。最后,通过@Autowired注解将属性类注入到需要读取配置的类。以下是一个示例代码: 引用: ```java import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.context.properties.EnableConfigurationProperties; @EnableConfigurationProperties(TestProperties.class) @SpringBootConfiguration public class TestConfiguration { } ``` 引用: ```java import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; @Getter @Setter @ConfigurationProperties(prefix = "spring.rabbitmq") public class TestProperties { private String host; private String port; private String username; private String password; } ``` 通过以上的配置,可以通过@Autowired注解将TestProperties类注入到需要读取配置的类,并直接使用其的属性值。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot读取application.properties配置文件](https://blog.csdn.net/watson2017/article/details/124732267)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zonson9999

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值