下面是spring boot项目中YML文件中的条目。
application:
applicationPath: "/demo"
defaultFilePath: ${application.applicationPath}childDemo/import_files/
actDocsPath: ${application.applicationPath}childDemo2/act_docs/
bgJobsLogs: ${application.applicationPath}childDemo3/bgjobs_logs/
在这些位置上,我将一些文档保留在我的项目中。
需求是..在项目启动时,我想创建所有这些目录结构,因此我创建了一个类...
package com.sbill.app.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "application", ignoreUnknownFields = false)
public class ApplicationProperties {
@Value("${application.applicationPath}")
public String applicationPath;
@Value("${application.defaultFilePath}")
public String defaultFilePath;
@Value("${application.actDocsPath}")
public String actDocsPath;
@Value("${application.bgJobsLogs}")
public String bgJobsLogs;
public String getDefaultFilePath() {
return defaultFilePath;
}
public void setDefaultFilePath(String defaultFilePath) {
this.defaultFilePath = defaultFilePath;
}
public String getActDocsPath() {
return actDocsPath;
}
public void setActDocsPath(String actDocsPath) {
this.actDocsPath = actDocsPath;
}
public String getBgJobsLogs() {
return bgJobsLogs;
}
public String getApplicationPath() {
return applicationPath;
}
public void setApplicationPath(String applicationPath) {
this.applicationPath = applicationPath;
}
}
我创建了一个用CommandLineRunner
实现的类,以便在启动时调用
package com.sbill.app.config;
import java.io.File;
import java.lang.reflect.Field;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import com.sbill.app.domain.User;
import com.sbill.app.repository.UserRepository;
import com.sbill.app.service.util.FieldReqValidationUtil;
@Component
public class SbillStartupRunner implements CommandLineRunner {
@Autowired
ApplicationProperties applicationProperties;
@Override
public void run(String... args) throws Exception {
System.out.println(applicationProperties.getDefaultFilePath());
System.out.println(applicationProperties.getApplicationPath());
Field[] fields = applicationProperties.getClass().getDeclaredFields();
for(Field f : fields){
f.setAccessible(true);
Class t = f.getType();
Object v = f.get(applicationProperties);
File applicationpath = new File(v.toString());
if (!applicationpath.exists()) {
if (applicationpath.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
System.out.println(applicationpath.getAbsolutePath());
}
}
}
在这里,我的问题是:-使用setter / getter路径正在打印,但是使用反射时,我无法访问字段。
答案 0 :(得分:2)
getClass()
将返回对象的实际类,例如它可能是子类,并且getDeclaredFields()
仅返回在该类中声明的字段。要获取在ApplicationProperties
中声明的字段,只需使用ApplicationProperties.class.getDeclaredFields()
。
请注意,您可以使用AccessibleObject.setAccessible
来改进代码,以一次设置所有对象的可访问性状态,以避免重复进行可能昂贵的安全检查。另外,强烈建议在此使用NIO,因为它可以在目录创建失败时进行诊断,而不是返回无用的boolean
值。
Field[] fields = ApplicationProperties.class.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for(Field f : fields){
Object v = f.get(applicationProperties);
Path p = Files.createDirectories(Paths.get(v.toString()));
System.out.println(p.toAbsolutePath());
}