Micronaut-在视图中获取运行环境名称

时间:2019-01-19 07:26:34

标签: thymeleaf micronaut

我有一个带有观察层(百里香叶)的小型Micronaut应用程序。现在,我想集成Google Analytics(分析)或其他一些跟踪工具。当然,这应该只是在生产模式下加载,但是如何在视图层中检查环境?

我通过以下方式启动应用程序:

java -Dmicronaut.environments = prod -jar mywebsite.jar

2 个答案:

答案 0 :(得分:2)

您可以注入io.micronaut.context.env.Environment并将getActiveNames()的结果作为视图模型的一部分传递

编辑:

如果要将此数据添加到每个模型中而不接触每个控制器方法,则可以添加一个在视图过滤器之前处理的服务器过滤器,以处理响应,或者创建一条可以处理操作返回值的周围建议。控制器。

答案 1 :(得分:0)

解决此问题的另一种方法是利用@Replaces 注释覆盖 ThymeleafViewsRenderer 类并在您的类中添加公共数据。

import io.micronaut.context.annotation.Replaces
import io.micronaut.context.env.Environment
import io.micronaut.core.beans.BeanMap
import io.micronaut.core.io.Writable
import io.micronaut.core.io.scan.ClassPathResourceLoader
import io.micronaut.core.util.ArgumentUtils
import io.micronaut.http.HttpRequest
import io.micronaut.views.exceptions.ViewRenderingException
import io.micronaut.views.thymeleaf.ThymeleafViewsRenderer
import io.micronaut.views.thymeleaf.WebContext
import org.thymeleaf.TemplateEngine
import org.thymeleaf.context.Context
import org.thymeleaf.context.IContext
import org.thymeleaf.exceptions.TemplateEngineException
import org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver

import javax.annotation.Nonnull
import javax.annotation.Nullable
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
@Replaces(bean = ThymeleafViewsRenderer.class)
class CustomThymeleafFilter extends ThymeleafViewsRenderer {

    CustomThymeleafFilter(AbstractConfigurableTemplateResolver templateResolver, TemplateEngine templateEngine, ClassPathResourceLoader resourceLoader) {
        super(templateResolver, templateEngine, resourceLoader)
    }

    @Inject
    Environment environment

    @Override
    @Nonnull
    public Writable render(@Nonnull String viewName, @Nullable Object data) {
        ArgumentUtils.requireNonNull("viewName", viewName);
        return (writer) -> {
            
            //following block adds environments variable to model
            Map dataMap = variables(data)
            dataMap.put("environment", environment.activeNames)
            
            IContext context = new Context(Locale.US, dataMap);
            processView(viewName, writer, context);
        };
    }

    @Override
    @Nonnull
    public Writable render(@Nonnull String viewName, @Nullable Object data,
                           @Nonnull HttpRequest<?> request) {
        ArgumentUtils.requireNonNull("viewName", viewName);
        ArgumentUtils.requireNonNull("request", request);
        return (writer) -> {

            //following block adds environments variable to model
            Map dataMap = variables(data)
            dataMap.put("environment", environment.activeNames)
            
            IContext context = new WebContext(request, Locale.US, dataMap);
            processView(viewName, writer, context);
        };
    }

    private static Map<String, Object> variables(@Nullable Object data) {
        if (data == null) {
            return new HashMap<>();
        }
        if (data instanceof Map) {
            return (Map<String, Object>) data;
        } else {
            return BeanMap.of(data);
        }
    }

    private void processView(String viewName, Writer writer, IContext context) {
        try {
            engine.process(viewName, context, writer);
        } catch (TemplateEngineException e) {
            throw new ViewRenderingException("Error rendering Thymeleaf view [" + viewName + "]: " + e.getMessage(), e);
        }
    }
}