背景
接到一个需求,需要项目启动时读取excel,将数据缓存,然后程序运行时使用
WINDOWS与LINUX均可用
方式与步骤
文件放到config下
项目初始化时读取文件
步骤1
@Component
@Slf4j
public class Device2LocationRunnerImpl implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
List<DeviceLocation> locations = FileUtil.readDirectory("config/位置表.xls");
log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(locations));
if(StringUtils.isNotEmpty(locations)){
locations.forEach(location -> {
DeviceLocationConstant.nickname2LocMap.put(location.getNickname(),location.getLocation());
});
}
}
}
步骤2
/**
* directory 传入需要读取文件
*
*/
public static List<DeviceLocation> readDirectory(String directory) throws Exception {
ExcelUtil<DeviceLocation> util = new ExcelUtil<DeviceLocation>(DeviceLocation.class);
List<DeviceLocation> locations = Lists.newArrayList();
//取出配置路径下的配置文件
try {
InputStream resourceAsStream = FileUtil.class.getClassLoader().getResourceAsStream(directory);
List<DeviceLocation> locations1 = util.importExcel(resourceAsStream);
log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(locations1));
locations.addAll(locations1);
} catch (Exception e) {
log.error("解析:{}文件出现异常:{}", directory, e);
}
return locations;
}
外置配置文件可用的读取方式
待读取文件
方法展示
/**
* 读取文件夹下所有xml文件
* 传入config即获取xxxxx\target\classes\config目录
*
*/
public static List<Config> readDirectory(String directory) throws Exception {
List<Config> configs = Lists.newArrayList();
Resource resource = new ClassPathResource(directory);
Arrays.stream(Objects.requireNonNull(resource.getFile().listFiles())).filter(File::isDirectory).distinct().forEach(file ->{
//取出配置路径下的配置文件
Arrays.stream(file.listFiles()).filter(File::isFile).distinct().forEach(xml ->{
try {
InputStream inputStreamCheck = FileUtils.openInputStream(xml);
//只解析xml文件
if (!FileUtil.checkFileType(inputStreamCheck, FileType.XML)) {
log.info("config下文件:{}不是xml类型,不符合解析条件,本次跳过", xml.getName());
return;
}
InputStream inputStreamUse = FileUtils.openInputStream(xml);
String xmlStr = new String(readFileStream(inputStreamUse), Constants.GBK);
Config config = (Config) XsteamUtil.xmltoBean(Config.class, xmlStr);
log.info("解析的xml文件内容->{}" + JSONUtil.toJsonStr(config));
configs.add(config);
}catch (IOException e){
log.error("解析:{}文件出现异常:{}" ,xml.getName(),e);
}
});
});
return configs;
}
外置配置文件的方式
<plugins>
<!--打包jar-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<!--不打包资源文件-->
<excludes>
<exclude>**/*.properties</exclude>
<exclude>**/*.xml</exclude>
<exclude>**/*.yml</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<!--MANIFEST.MF 中 Class-Path 加入前缀-->
<classpathPrefix>lib/</classpathPrefix>
<!--jar包不包含唯一版本标识-->
<useUniqueVersions>false</useUniqueVersions>
<!--指定入口类-->
<mainClass>com.xxx.xxxApplication</mainClass>
</manifest>
<manifestEntries>
<!--MANIFEST.MF 中 Class-Path 加入资源文件目录-->
<Class-Path>./resources/</Class-Path>
</manifestEntries>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</plugin>
<!--拷贝依赖 copy-dependencies-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib/
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--拷贝资源文件 copy-resources-->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<!--spring boot repackage,依赖 maven-jar-plugin 打包的jar包 重新打包成 spring boot 的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!--重写包含依赖,包含不存在的依赖,jar里没有pom里的依赖-->
<includes>
<include>
<groupId>com.xxxx</groupId>
<artifactId>xxxxx-xxxx</artifactId>
</include>
</includes>
<layout>ZIP</layout>
<!--使用外部配置文件,jar包里没有资源文件-->
<addResources>true</addResources>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<!--配置jar包特殊标识 配置后,保留原文件,生成新文件 *-run.jar -->
<!--配置jar包特殊标识 不配置,原文件命名为 *.jar.original,生成新文件 *.jar -->
<!--<classifier>run</classifier>-->
</configuration>
</execution>
</executions>
</plugin>
</plugins>