2016-10-22 2 views
1

외부 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에서

답변

1

config.properties 파일이 괜찮아하지만 당신은 왜 초기화 않는다 : IndexController에서

XmlOperation xmlOperation = new XmlOperation();

를? 또한 XmlOperation이 스프링 구성 요소인지 여부도 확실하지 않습니다. 질문에 XmlOperation보다 @PropertySource 만 있습니다.

기본적으로 나는 @ComponentXmlOperation을하고 IOC의와 IndexController에이 구성 요소를 삽입합니다. XmlOperation에서

public String readXml(InputStream is)는 표준 서비스처럼 동작하고 나는 재산 filepath을 만들고 @Value 주석을 구성 파일 (config.properties)에서 값을 주입하는 것입니다.


FULL 예 :

@Controller 
public class IndexController { 

    @Autowired 
    private XmlOperation 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"; 
    } 
} 

@Component 
@PropertySource("classpath:config.properties") 
public class XmlOperation { 

    // use this when XmlOperation is @Configuration bean and you want to create @Bean-s e.g 
    // @Autowired 
    // Environment env; 

    // for your case inject property like this 
    @Value("${filepath}") 
    private String filepath; 

    public String readXml(InputStream is) throws IOException { 

     // dont use this 
     //System.out.println(env.getProperty("filepath")); 

     // rather this 
     System.out.println(filepath); 

     StringWriter writer = new StringWriter(); 
     IOUtils.copy(is, writer, StandardCharsets.UTF_8); 
     String fileContent = writer.toString(); 
     return fileContent; 

    } 
} 
+0

감사 @VladoDemcak, 속임수를 썼는지! –

+0

'@ Component' 대신'@ Service' Annotation을 추가 할 수 있습니까 ?? 그렇지 않다면, 왜? 나는 봄에 익숙하지 않으므로 문서를 이해하는 것이 거의 어렵다. –

+1

@NewBeeDeveloper XmlOperation에서 비즈니스 메소드를 사용하려는 경우 클래스를 서비스로 주석 처리 할 수 ​​있습니다. 컴포넌트는 스프링 컨테이너 내에서 관리되는 일종의 일반 애노테이션 선언 클래스입니다. – VladoDemcak

관련 문제