Discuss / Java / 练习

练习

Topic source
@Componentpublic class AppService {

    @Value("classpath:/logo.txt")
    private Resource resource;    
    private String logo;    
    @Value("classpath:/app.properties")
    private Resource properties;        
    private String version;    

  @PostConstruct    
    public void init() throws IOException {
        try (var reader = new BufferedReader(new InputStreamReader(resource.getInputStream(),StandardCharsets.UTF_8))) {
         //lines():Returns a Stream, the elements of which are lines read from this BufferedReader.            
        this.logo = reader.lines().collect(Collectors.joining("\n"));//连接流中每个元素的toString方法生成的字符串        }

        try (var reader = new BufferedReader(
           new InputStreamReader(properties.getInputStream(), StandardCharsets.UTF_8))) {
           this.version = reader.lines().collect(Collectors.joining("\n"));        }
    }

    @PreDestroy    
        public void shutDown() {
        System.out.println("End!");    
    }

        public void printLogo() {
        System.out.println(logo);        
        System.out.println("app.version: " + version);    
    }


}

  • 1

Reply