因为spring boot使用@Value默认是读取application.properties/application.yml中的属性,如果我们需要读取自定义的properties文件的属性,如log4j.properties/db.properties等自定义的属性的时候,做法如下
首先:新建一个类,该类用来解析properties文件的,我这里以读取log4j.properties为例
新建properties配置文件类,log4jConfig.java
package com.tencent.aics.shortlink.conf;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component //主要是为了在其他引用的地方直接使用@Autowired
@ConfigurationProperties //这个注解为标识为一个properties配置文件类,可以给定前缀
@PropertySource("classpath:log4j.properties") //指定需要读取的配置文件(以及指定位置)
public class Log4jConfig {
@Value("${log4j.appender.db.File}") //直接使用@Value来获取值
private String file;
//如果指定了前缀的话,可以省略掉@Value注解,但是这个时候必须保证变量名和前缀拼起来是log4j.properties中的key,这里需要注意一下
@Value("${log4j.appender.db.layout.ConversionPattern}")
private String conversionPattern;
public String getConversionPattern() {
return conversionPattern;
}
public void setConversionPattern(String conversionPattern) {
this.conversionPattern = conversionPattern;
}
public String getFile() {
return file;
}
public void setFile(String file) {
file = file;
}
}
在需要使用的地方直接引入
package com.tencent.aics.shortlink.timer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tencent.aics.shortlink.conf.Log4jConfig;
import com.tencent.aics.shortlink.model.LogEntity;
import com.tencent.aics.shortlink.service.OperateLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
@Component
@EnableScheduling //启动定时任务
public class LogTimer {
@Autowired
private Log4jConfig log4jConfig; //注入配置文件类
private static final Logger LOG = LoggerFactory.getLogger(LogTimer.class);
/**
上次文件的大小
*
*/
private long pointer = 0;
@Autowired
private OperateLogService operateLogService;
//每5秒执行一次
@Scheduled(cron = "0/5 * * * * *")
public void testDemo() {
try {
System.out.println("获取的log4j的路径为:"+log4jConfig.getFile()+"sha:"+log4jConfig.getConversionPattern());
//读取日志文件
File file = new File(log4jConfig.getFile());
//指定权限只读
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
//移动文件读取指针的位置,从此处读取
randomAccessFile.seek(pointer);
if (file.isFile() && file.exists()) {
String lineTxt = null;
while ((lineTxt = randomAccessFile.readLine()) != null) {
LogEntity logEntity = handleLog(lineTxt);
operateLogService.insertLog(logEntity);
//更新指针位置
pointer = randomAccessFile.getFilePointer();
}
randomAccessFile.close();
} else {
LOG.error("找不到文件路径!");
}
}catch (Exception e){
e.printStackTrace();
}
// return new ModelAndView("redirect:http://www.baidu.com");
}
private LogEntity handleLog(String logText){
logText = logText.substring(logText.indexOf("{"));
LogEntity logEntity = null;
ObjectMapper mapper = new ObjectMapper();
try {
logEntity = mapper.readValue(logText, LogEntity.class);
} catch (IOException e) {
e.printStackTrace();
}
return logEntity;
}
}