DEV Community

Query Filter
Query Filter

Posted on

docker138

if (localfile && fileloc == null) {
    JFileChooser fc = new JFileChooser();
    fc.showSaveDialog(fc);
    File tempFilelocal = fc.getSelectedFile();
    filenotspecified = 0;

    if (tempFilelocal == null) {
        filenotspecified = 1;
        return;
    }

    this.logger.info("saved to temp file " + tempFilelocal);
    // Best practice for Java 21: use try-with-resources
    try (FileOutputStream fos = new FileOutputStream(tempFilelocal)) {
        baos.writeTo(fos);
    } catch (IOException e) {
        this.logger.error("Failed to write to temp file", e);
    }

    // Initialize factory and reader
    DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(dlbf);

    // Load from byte array
    reader.loadBeanDefinitions(new ByteArrayResource(baos.toByteArray()));

    this.beanFactory = dlbf;
    this.logger.info("Spring context " + this.beanFactory);

} else {
    // Initialize factory and reader for the file system path
    DefaultListableBeanFactory dlbf = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(dlbf);

    // Load from the provided file location
    reader.loadBeanDefinitions(new FileSystemResource(fileloc));

    // Modern replacement for eager loading
    dlbf.preInstantiateSingletons();

    this.beanFactory = dlbf;
    this.logger.info("Spring context " + this.beanFactory);
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)