简体   繁体   English

有没有一种方法可以将 Java 代码嵌入到 YAML 文件中以在运行时进行处理。

[英]Is there a way I can embed Java code inside a YAML file to be processed at runtime.

I know ruby has this facility using ERB.我知道 ruby​​ 有这个使用 ERB 的设施。 But does Java have this capability?但是Java有这种能力吗? I'm trying to do something like this:我正在尝试做这样的事情:

MY.yaml CURRENT_DATE: <%=LocalDateTime.now()%> MY.yaml CURRENT_DATE:<%=LocalDateTime.now()%>

Ya, the classic way is using Java server pages https://en.wikipedia.org/wiki/JavaServer_Pages Something like是的,经典的方法是使用 Java 服务器页面https://en.wikipedia.org/wiki/JavaServer_Pages类似

Date : <% Date() %>
myArray:
<% for (int i=1; i<4; i++) { %>
    - This  is <% i %> th element.
<% } %>

For modern curly bracket based templating use www.thymeleaf.org, or better JTwig with double curly braces and other cool features对于基于现代花括号的模板,请使用 www.thymeleaf.org,或者更好的带有双花括号和其他酷功能的 JTwig

 CurrentDate: {{ "now"|date("m/d/Y") }}

https://twig.symfony.com/doc/2.x/filters/date.html https://twig.symfony.com/doc/2.x/filters/date.html

I had a similar requirement where a YAML had an expression which needed to be parsed in runtime in Java.我有一个类似的要求,其中 YAML 有一个需要在 Java 运行时解析的表达式。

We ended up using MVEL for this.我们最终为此使用了MVEL We read the contents of the yaml and passed the expression to MVEL.我们读取 yaml 的内容并将表达式传递给 MVEL。 Its syntax is quite similar to that of Java.它的语法与Java 的语法非常相似。

You could also try other template library like Velocity Template .您也可以尝试其他模板库,如Velocity Template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定一个字符串作为泛型类的类型名称,我如何在运行时创建实例。 如果不可能,还有其他方法吗? - Given a string as the type name of a generic class, how can I create the instance in runtime. If it is not possible, Is there another way? 将 java 代码嵌入 html 文件的最佳方法? - Best way to embed java code into an html file? 如何在运行时使用 spring 引导应用程序中的 java 代码对 application.yaml 进行读写操作? - How can I write and read to and from a application.yaml at runtime using java code in a spring boot application? 如何在lua中嵌入Java代码 - How can I Embed java code in lua 在运行时释放OS的java内存。 - Release java memory for OS at runtime. 我可以在Android运行时动态地将Java代码放入Java代码中吗? - Can i put java code inside java code dynamically at runtime in android? 将Java代码嵌入模板中 - embed java code inside a template 如何在Java中的代码中插入java文件 - How can I insert java file inside of code in Java 我如何在jsf页面中嵌入java代码? - How do i embed java code inside jsf page? 在运行时更改数据库。 如何刷新 JdbcTemplate bean 的数据源? - Changing database during runtime. How can i refresh data source for JdbcTemplate bean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM