외부 config.properties
파일에 액세스하려고하는 간단한 스프링 부팅 응용 프로그램을 만들고 있습니다.봄 부팅시 속성 파일 액세스
IndexController.java config.properties
파일이 src/main/resources
에 위치해
@PropertySource("classpath:config.properties")
public class XmlOperation {
@Autowired
Environment env;
public String readXml(InputStream is) throws IOException {
System.out.println(env.getProperty("filepath"));
StringWriter writer = new StringWriter();
IOUtils.copy(is, writer, StandardCharsets.UTF_8);
String fileContent = writer.toString();
return fileContent;
}
@Controller
public class IndexController {
XmlOperation xmlOperation = new XmlOperation();
@RequestMapping("/")
public String greeting() {
return "greeting";
}
@RequestMapping(params = "btnOpen", method = RequestMethod.POST)
public String uploadFile(@RequestParam("file") MultipartFile file, Model model) {
try {
InputStream is = file.getInputStream();
model.addAttribute("fileContent", xmlOperation.readXml(is));
} catch (IOException e) {
System.out.println(e.getMessage());
}
return "greeting";
}
}
XmlOperation.java. 속성 파일에서 값을 가져올 수 없습니다.
어떤 도움을 주시면 감사하겠습니다 ...
src/main/resources
에서
감사 @VladoDemcak, 속임수를 썼는지! –
'@ Component' 대신'@ Service' Annotation을 추가 할 수 있습니까 ?? 그렇지 않다면, 왜? 나는 봄에 익숙하지 않으므로 문서를 이해하는 것이 거의 어렵다. –
@NewBeeDeveloper XmlOperation에서 비즈니스 메소드를 사용하려는 경우 클래스를 서비스로 주석 처리 할 수 있습니다. 컴포넌트는 스프링 컨테이너 내에서 관리되는 일종의 일반 애노테이션 선언 클래스입니다. – VladoDemcak