title: How do I resolve the '@environment' bean in a Thymeleaf template in a Spring Boot app
body: <p>I have a Spring boot application that is headless (only presenting REST APIs), but I am using Thymeleaf for some email templates.</p>
  <p>I have included</p>
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
  &lt;/dependency&gt;
  </code></pre>
  <p>in my dependencies.</p>
  <p>When I include <code>@environment</code> in my template, I get the error</p>
  <pre><code>Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'environment'
  </code></pre>
  <p>My template code is, for example,</p>
  <pre><code>&lt;p&gt;
    &lt;a th:href=&quot;${@environment.getProperty('system.clientUrl')} + '/verify?action=accept&amp;verificationCode=' + ${verificationCode}&quot;&gt;Verify&lt;/a&gt;
  &lt;/p&gt;
  </code></pre>
  <p>where <code>system.clientUrl</code> is defined in the <code>application.properties</code> file.</p>
  <p>Other aspects of Thymeleaf seem to work just fine (including when I defined the client URL in a resources bundle).</p>
  <p>For other objects referenced in the template, I am passing a <code>Set&lt;String,Object&gt;</code> of template model objects.</p>
  <pre><code>Map&lt;String, Object&gt; templateModel = new HashMap&lt;String, Object&gt;();
      templateModel.put(&quot;verificationCode&quot;, emailVerification.getVerificationCode());
  </code></pre>
  <p>I suppose, I could pass the <code>Environment</code> or just the clientUrl in too, but I assumed that <code>@environment</code> is always available to the Thymeleaf renderer.</p>
  <p><strong>Update</strong></p>
  <p>My rendering code is</p>
  <pre><code>Context thymeleafContext = new Context();
  thymeleafContext.setVariables(templateModel);
  String htmlBody = thymeleafTemplateEngine.process(templateName, thymeleafContext);
  </code></pre>
  <p>Is there something I need to pass to the context?</p>