在propreties文件中引用另一个properties文件中的内容

转载:http://www.avajava.com/tutorials/lessons/how-do-i-filter-resources-based-on-values-from-a-properties-file.html?page=1

 
 
 
How do I filter resources based on values from a properties file?
Author: Deron Eriksson
Description: This tutorial describes how to filter resources based on properties in a file.
Tutorial created using: Windows Vista || JDK 1.6.0_04 || Eclipse Web Tools Platform 2.0.1 (Eclipse 3.3.1)

 

Page:    1 2  >

 

In another tutorial, we saw how we could filter resource files so that we could replace references to properties with the values of those properties and how this substitution takes place during the "process-resources" default lifecycle phase. We can also specify properties in a properties file and use these properties in the substitutions during the "process-resources" lifecycle phase.

For example, suppose we have the following resource file.

src/main/resources/textfile.txt

this is a test
artifact id: ${project.artifactId}
my.property: ${my.property}

The project.artifactId property value comes from the project's pom.xml, but we'd like the my.property to be specified in a properties file, such as the following:

src/main/filters/myfilter.properties

my.property=hamburger and fries

The project containing the resource file and the filter properties file are shown here:

Project structure

Here is the project's pom.xml file. It specifies to use the properties in the myfilter.properties file via the build.filters.filter value. It specifies to turn on filtering by setting build.resources.resource.filtering to true. It specifies the resource directory via the build.resources.resource.directory (this is needed since we overrid the default behavior by setting filtering to true).

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.maventest</groupId> <artifactId>aproject</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <name>aproject</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> </dependencies> <build> <filters> <filter>src/main/filters/myfilter.properties</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> </project>



page2:

Now, I'll perform a "mvn clean process-resources" on the project.

Executing 'mvn clean process-resources' on 'aproject'

This generates the target/classes/textfile.txt file.

textfile.txt in target/classes

If we examine the contents of the textfile.txt file, we can see that it has been filtered to contain the property values. The project.artifactId value came from the pom.xml file, and the my.property value came from the myfilter.properties file.

target/classes/textfile.txt

this is a test
artifact id: aproject
my.property: hamburger and fries

转载于:https://www.cnblogs.com/kongweiteng/p/6733132.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个示例代码,供您参考: 1. pom.xml ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>jcifs</groupId> <artifactId>jcifs</artifactId> <version>2.1.32</version> </dependency> </dependencies> ``` 2. application.properties ```properties # JCIFS jcifs.smb.client.username=your_username jcifs.smb.client.password=your_password jcifs.smb.client.domain=your_domain # Log files directory log.files.directory=D:\logs ``` 3. 日志处理类 ```java import jcifs.smb.NtlmPasswordAuthentication; import jcifs.smb.SmbException; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileFilter; import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; @Component public class LogFileProcessor { @Value("${jcifs.smb.client.username}") private String smbUsername; @Value("${jcifs.smb.client.password}") private String smbPassword; @Value("${jcifs.smb.client.domain}") private String smbDomain; @Value("${log.files.directory}") private String logFilesDirectory; private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // Map<用户名, Map<文件路径, 操作类型>> private final Map<String, Map<String, String>> userOperationsMap = new HashMap<>(); public void processLogFile(String filePath, String username) { Map<String, String> fileOperationsMap = userOperationsMap.computeIfAbsent(username, k -> new HashMap<>()); // 获取文件名和操作类型 String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1); String operationType = getOperationType(fileName); // 添加或更新操作记录 fileOperationsMap.put(filePath, operationType); } private String getOperationType(String fileName) { if (fileName.endsWith(".txt")) { return "读取"; } else { return "其他操作"; } } @Scheduled(cron = "0 0 12 * * ?") public void generateLogFiles() throws IOException { // 创建日志目录 File logDirectory = new File(logFilesDirectory); if (!logDirectory.exists() && !logDirectory.mkdirs()) { throw new IOException("Failed to create log directory"); } // 遍历用户操作记录,生成日志文件 for (Map.Entry<String, Map<String, String>> userEntry : userOperationsMap.entrySet()) { String username = userEntry.getKey(); Map<String, String> fileOperationsMap = userEntry.getValue(); // 创建用户日志目录 File userLogDirectory = new File(logDirectory, username); if (!userLogDirectory.exists() && !userLogDirectory.mkdirs()) { throw new IOException("Failed to create user log directory"); } // 生成日志文件 String logFilePath = userLogDirectory.getAbsolutePath() + "\\" + dateFormat.format(new Date()) + ".log"; File logFile = new File(logFilePath); if (!logFile.exists() && !logFile.createNewFile()) { throw new IOException("Failed to create log file"); } // 写入日志内容 try (BufferedWriter writer = new BufferedWriter(new FileWriter(logFile))) { for (Map.Entry<String, String> fileEntry : fileOperationsMap.entrySet()) { String filePath = fileEntry.getKey(); String operationType = fileEntry.getValue(); writer.write(filePath + " - " + operationType); writer.newLine(); } } // 清空用户操作记录 fileOperationsMap.clear(); } } public void monitorSharedFolder(String sharedFolder) throws SmbException { // 认证信息 NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(smbDomain, smbUsername, smbPassword); // 共享文件夹 SmbFile sharedFolderFile = new SmbFile(sharedFolder, auth); // 文件过滤器 SmbFileFilter filter = file -> { String name = file.getName(); return !name.equals(".") && !name.equals("..") && !file.isDirectory() && !name.endsWith(".lnk"); }; // 监视共享文件夹 while (true) { SmbFile[] files = sharedFolderFile.listFiles(filter); for (SmbFile file : files) { String filePath = file.getPath(); String username = file.getPrincipal().getName(); processLogFile(filePath, username); } try { Thread.sleep(1000); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } } } ``` 4. 启动类 ```java import jcifs.smb.SmbException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application implements CommandLineRunner { @Autowired private LogFileProcessor logFileProcessor; public static void main(String[] args) { SpringApplication.run(Application.class, args); } @Override public void run(String... args) throws Exception { if (args.length < 1) { System.err.println("Usage: java -jar log-monitor.jar shared_folder_path"); System.exit(1); } String sharedFolder = args[0]; try { logFileProcessor.monitorSharedFolder(sharedFolder); } catch (SmbException e) { System.err.println("Failed to monitor shared folder: " + e.getMessage()); System.exit(1); } } } ``` 以上代码实现了对共享文件夹的监控,并在每天12点生成每个用户的操作日志文件。需要注意的是,为了防止将所有方法都写在启动类,我们使用了组件扫描和依赖注入的方式来处理日志文件

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值