我有一个 build.properties 文件,Maven 将其用于构建目的。我想使用相同的文件并在我的前端应用程序(React-Redux-Webpack)中读取它。如果需要,我会提供更多信息。任何帮助将不胜感激。提前致谢!
假设我有一个src/main/ressources/application.properties
包含以下(示例性)条目:
server.ssl.trust-store=/config/app.jks
Run Code Online (Sandbox Code Playgroud)
以及多个环境的多个 application.properties
/config
|---/prod
| |---/application.properties // overrides "server.ssl.trust-store"
|---/int
|---/application.properties
/src
|---/main
|---/resources
|---/application.properties // contains entry "server.ssl.trust-store"
Run Code Online (Sandbox Code Playgroud)
目录中的两个application.properties/config
都继承自src/main/resources/application.properties。在 /config/prod/application.properties 中,我们要覆盖该条目。这一切都按预期进行。
现在,我的问题是:是否可以禁止属性的继承?我尝试用空值覆盖它,但这不起作用,并且被不同地解释为根本没有设置它。
注意:server.ssl.enabled=false
对于这个特殊情况,我知道(来源)的存在。
我有一个这样的 label.properties 文件:
text1:firstname
text2:middlename
text3:lastname
text4:username
Run Code Online (Sandbox Code Playgroud)
我使用此代码读取属性文件
package test;
import java.util.Enumeration;
import java.util.ResourceBundle;
public class labelclass {
public static String read(int n) {
ResourceBundle rb = ResourceBundle.getBundle("myfolder.label");
Enumeration <String> keys = rb.getKeys();
while (keys.hasMoreElements()) {
for(int i=1; i<=n; i++){
String key = keys.nextElement();
}
String value = rb.getString(key);
return value;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我打电话read(2)
,它应该返回middlename
。但它返回它返回firstname
的顺序是这样的:
text2:middlename
text1:firstname
text4:username
text3:lastname
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我目前正在使用Apache commons配置库来编写和读取文件中的数据.我能够将键值对colors = hello保存到user..properties文件中,但是当我尝试读取该值时,获取以下异常.
Exception in thread "main" java.lang.IllegalArgumentException: 'hello' does not contain an equals sign
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:625)
at org.apache.commons.configuration.AbstractConfiguration.getProperties(AbstractConfiguration.java:579)
at com.code.prep.CommonsMain.readProperties(CommonsMain.java:21)
at com.code.prep.CommonsMain.main(CommonsMain.java:12)
Run Code Online (Sandbox Code Playgroud)
代码如下
package com.code.prep;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class CommonsMain {
public static void main(String[] args) {
CommonsMain main = new CommonsMain();
main.readProperties();
// main.writeProperties();
}
public void readProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try {
config.load("user.properties");
System.out.println(config.getProperties("colors"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public void writeProperties(){
PropertiesConfiguration config = new PropertiesConfiguration();
try { …
Run Code Online (Sandbox Code Playgroud) 我正在尝试读取属性文件以获取值。但是,代码抛出异常。
例外
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cisco.installbase.hiveconnector.ReadProperties.getInstance(ReadProperties.java:28)
at com.cisco.installbase.hiveconnector.MainApp.main(MainApp.java:7)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:16)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:12)
at com.cisco.installbase.hiveconnector.ReadProperties$PropHolder.<clinit>(ReadProperties.java:23)
... 2 more
Run Code Online (Sandbox Code Playgroud)
读取属性.java
package com.cisco.installbase.hiveconnector;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class ReadProperties {
private final Properties props = new Properties();
private ReadProperties()
{
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
try{
props.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
private static class PropHolder{
private static final ReadProperties INSTANCE = new ReadProperties();
}
public static …
Run Code Online (Sandbox Code Playgroud) 出于某种原因,我处于需要使用属性文件的情况:
1=1
2=2
3=3
4=4
5=5
6=6
7=7
12=12
13=13
14=14
15=15
16=16
17=17
23=23
24=24
25=25
26=26
27=27
34=34
35=35
36=36
37=37
45=45
46=46
47=47
56=56
57=57
67=67
123=123
124=124
125=125
126=126
.................
24567=24567
34567=34567
123456=123456
123457=123457
123467=123467
123567=123567
124567=124567
134567=134567
234567=234567
1234567=1234567
Run Code Online (Sandbox Code Playgroud)
我有实用程序处理程序类来对键进行排序
public class PropertyHandler {
private static PropertyHandler instance;
private Properties properties;
private PropertyHandler() {
InputStream fos = null;
try {
fos = PropertyHandler.class.getClassLoader().getResourceAsStream("dow-pattern.properties");
properties = new Properties() {
@Override
public Set<Object> keySet() {
return Collections.unmodifiableSet(new …
Run Code Online (Sandbox Code Playgroud) 我的应用程序将检查属性文件是否存在,如果不存在则创建一个.
try{
// create new file
String path="c:\\temp\\LaserController.properties";
File file = new File(path);
String comport = "Comport=COM1";
String Parity = "parity=none";
String baud = "baud=9600";
String Stopbits = "StopBits=0";
String databits = "DataBits=8";
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
// write in file
bw.write(comport);
bw.newLine();
bw.write(Parity);
bw.newLine();
bw.write(baud);
bw.newLine();
bw.write(Stopbits);
bw.newLine();
bw.write(databits);
// close connection
bw.close();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试读取这样的属性文件时,我得到一个Null指针.
else {
Properties prop = new …
Run Code Online (Sandbox Code Playgroud) 我有一个int[]
输入的方法.
methodABC(int[] nValue)
Run Code Online (Sandbox Code Playgroud)
我想从Java属性文件中获取此nValue
nValue=1,2,3
Run Code Online (Sandbox Code Playgroud)
如何从配置文件中读取此内容,还是以其他格式存储?
我尝试的是(changing the nValue to 123 instead of 1,2,3
):
int nValue = Integer.parseInt(configuration.getProperty("nnValue"));
Run Code Online (Sandbox Code Playgroud)
我们如何做到这一点?
我正在使用此方法从属性文件中读取:
public void loadConfigFromFile(String path) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
prop.load(input);
/*saved like this:*/ //emails: abc@test.com, bbc@aab.com, ..
String wordsS = prop.getProperty("keywords");
String emailS = prop.getProperty("emails");
String feedS = prop.getProperty("feeds");
emails = Arrays.asList(emailS.split(",")); //ERROR !!
words = Arrays.asList( wordsS.split(","))); //ERROR !!
feeds = Arrays.asList( feedS.split(",")); //ERROR !!
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) { …
Run Code Online (Sandbox Code Playgroud) 在我们的 log4j 属性文件之一中,我可以看到以下行:
log4j.logger.com.sivalabs=调试
有人知道这到底是什么吗?
尽管 Java 属性文件传统上仅支持 ISO-8859-1,但JDK 9 及更高版本支持以 UTF-8 编码的属性文件。虽然只有 JDK 9+ 通过内置默认属性文件读取支持 UTF-8,但它使用的相同技术可以在任何 Java 版本中完成,围绕属性文件添加 UTF-8 支持并回退到 ISO-8859 -1 用于向后兼容(正如 JDK 实现所做的那样)。
我是 Spring Boot 的新手,我正在阅读它带来的所有漂亮的属性配置。Spring Boot 是否支持从以 UTF-8 编码的属性文件加载属性?如果没有,Spring Boot 代码中的何处是属性文件读取合并,以便我可以添加此功能并提交拉取请求?
我试图通过 Spring @Value 注释将一些属性值注入到变量中,但我得到空值。我尝试了不同的配置和技巧,但它不起作用。想想今天之前一切都运转正常。我不知道我改变了什么才能把事情搞砸。
这是我的java类:
@Component
@ConditionalOnProperty(prefix = "studioghibli", name = "get")
public class StudioGhibliRestService {
@Value("${studioghibli.basepath}")
private static String BASE_PATH;
@Value("${studioghibli.path}")
private static String PATH;
@Value("${studioghibli.protocol:http}")
private static String PROTOCOL;
@Value("${studioghibli.host}")
private static String HOST;
private static String BASE_URI = PROTOCOL.concat("://").concat(HOST).concat(BASE_PATH).concat(PATH);
@Autowired
StudioGhibliRestConnector connector;
public List<StudioGhibliFilmDTO> findAllFilms() throws SipadContenziosoInternalException {
var response = connector.doGet(BASE_URI, null, null);
if (!response.getStatusCode().is2xxSuccessful() || !response.hasBody()) {
throw new SipadContenziosoInternalException(Errore.INTERNAL_REST_ERROR, "FindAll(), microservizio ".concat(BASE_URI), null);
}
return (List<StudioGhibliFilmDTO>) response.getBody();
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,该类用 @Component 进行注释,因为我需要将其用作 @Service 层,以便在我的业务逻辑中进行休息调用。该类还以属性条件进行注释... …
properties-file ×12
java ×10
spring-boot ×3
maven ×2
arraylist ×1
arrays ×1
javascript ×1
log4j ×1
parsing ×1
properties ×1
spring ×1
utf-8 ×1
webpack ×1