Below are the entries which are there in YML files in spring boot project.
application:
applicationPath: "/demo"
defaultFilePath: ${application.applicationPath}childDemo/import_files/
actDocsPath: ${application.applicationPath}childDemo2/act_docs/
bgJobsLogs: ${application.applicationPath}childDemo3/bgjobs_logs/
On these location i keep some document in my project.
Requirement is.. on project startup i want to create all these directory structure, so for that i created one class...
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;
}
}
And one class where i have implemnted with 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());
}
}
}
Here my problem is :- With setter/getter path is getting print , but using reflection i am not able to access fields..
Aucun commentaire:
Enregistrer un commentaire