4

I have a bean that is defined as a service:

@Service
public class FileHandling {
  public void doSomething() {
...

It's possible to autowire it in my app and use it:

@Autowired
@Qualifier("fileHandling")
FileHandling fh;

When I try to use it in a Thymeleaf template I receive this error message:

org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'fileHandling'

This is the relevant part of my template:

<td th:text="${@fileHandling.doSomething()}">...</td>  

This is how I access the templating engine:

final Context ctx = new Context();
ctx.setVariable("files", map);
ctx.setVariable("fileHandling",fh);

String html = templateEngine.process("flattopic", ctx);

I receive the error message no matter if I try to access the bean directly or after setVariable("fileHandling"). The syntax I use complies to what I see in chapter 5 of https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html.

I've seen similar questions which apply to basic SPEL (this one) or an unanswered question specific to Thymeleaf. The alternative to switch from a bean to a static class and use ${T(org.foo.bar.package.FileHandling).doSomething()} is something that I would like to avoid.

How can I solve this or make the bean accessible ?

Marged
  • 10,577
  • 10
  • 57
  • 99
  • Possible duplicate of [How to call a service method with Thymeleaf](https://stackoverflow.com/questions/43841458/how-to-call-a-service-method-with-thymeleaf) – riddle_me_this Dec 16 '18 at 15:22
  • @bphilipnyc Thanks for the link, but I think that my problem is different (see my edit) – Marged Dec 16 '18 at 15:44
  • @Marged the difference is that you are not defining a service on the application context – cralfaro Dec 17 '18 at 16:12
  • @cralfaro You mean that the bean I define with Service and that I can wire in anywhere else successfully is not visible to Thymeleafs context ? How would I achieve this ? – Marged Dec 17 '18 at 16:30
  • @Marged exactly, you need to define the service in a Configuration class, then on startup the service will be loadad in your app context and you can use it from thymeleaf or whatever – cralfaro Dec 18 '18 at 12:53

2 Answers2

6

"Call a bean from Thymeleaf under Spring Boot" is also "Call a bean from Thymeleaf under Spring MVC". For example:

Interface

package com.example;

public interface UrlService {

    String getApplicationUrl();

}

You has component MyConfiguration

package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyConfiguration {

    @Bean(name = "urlService")
    public UrlService urlService() {
        return () -> "domain.com/myapp";
    }

}

In Thymeleaf template file foo.html

<div th:text="${@urlService.getApplicationUrl()}">...</div>

Source: https://www.thymeleaf.org/doc/articles/springmvcaccessdata.html#spring-beans

Vy Do
  • 46,709
  • 59
  • 215
  • 313
0

Simply by declaring the @Service it should give the bean the name fileHandling.

@Service
public class FileHandling {
  public void doSomething() {

You can override it like this:

@Service("someOtherName")
public class FileHandling {
  public void doSomething() {

Make sure you have enabled autoconfiguration.

Read more information in Baeldung Spring Bean Names.

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272