#ssm#模板#打包成jar无法加载配置文件

解决方法

  @SneakyThrows
    @Override
    public void print(String fromPath, String fromTemplate, Map<String, Object> model) {
        // File ss = fileUtils.getResourceFile(from);
        Configuration cfg = new Configuration();
        // FileTemplateLoader ftl = new FileTemplateLoader(ss);
        cfg.setClassForTemplateLoading(getClass(), "/"+fromPath);


        Template t = cfg.getTemplate(fromTemplate);

        t.process(model,new PrintWriter(System.out));

    }

    @SneakyThrows
    @Override
    public void build(String from, String fromFile, Consumer<Template> callback) {
        // File ss = fileUtils.getResourceFile(from);
        Configuration cfg = new Configuration();
        // FileTemplateLoader ftl = new FileTemplateLoader(ss);
        cfg.setClassForTemplateLoading(getClass(), "/"+from);

        Template t = cfg.getTemplate(fromFile);
        callback.accept(t);
    }
@PostConstruct
    public void init() {
        log.info("加载 autoCode 的配置文件");
        this.properties = new Properties();
        // File f = fileUtils.getResourceFile("autoCode/TypeConfig.properties");

        try (InputStream is = new ClassPathResource("autoCode/TypeConfig.properties").getInputStream();) {
            Properties local = new Properties();
            local.load(is);
            properties.putAll(local);
        } catch (Exception e) {
            log.error("加载 autoCode配置时候出现异常了 {}", e.getMessage());
        }

        String[] prefixList = this.properties.getProperty("tableRemovePrefixes", "").split(",");
        if (prefixList.length > 0) {
            prefix.addAll(Arrays.asList(prefixList));
        }

    }

使用 流的方式读取 .properteis 的配置文件
参考文章如下:

freemaker加载资源文件

©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页