Согласно документу Spring, бины @ApplicationScope видны как атрибуты ServletContext, если контейнер Spring IO C поддерживает веб-интерфейс.
Я знаю, что могу получить доступ к бинам @ApplicationScope, используя ${@beanName}
в шаблоне тимелифа , но почему я не могу получить к ним доступ, используя ${#servletContext.getAttribute('myAttribute')}
в качестве документа тимелист?
Ниже приведен фрагмент кода.
@SpringBootApplication
public class Application {
@Autowired
private BranchService branchService;
public static void main(String[] args) {
SpringApplication.run(RetailStoreApplication.class, args);
}
@ApplicationScope
@Bean(value = "branches")
public List<Branch> branches() {
return branchService.queryAllBranches();
}
}
@Controller
public class HomeController {
@GetMapping(value = { "/", "/home" })
public String home() {
return "home.html";
}
}
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Test</title>
</head>
<body>
Below are all the branch, please choose one for this branch.
<span th:if="${#servletContext.getAttribute('branches')} == null">NULL</span>
<ul th:each="branch : ${#servletContext.getAttribute('branches')}">
<li th:text="${branch.name}" />
</ul>
</body>
</html>
Возвращаемый результат 'ветвления' равен нулю. Я использую spring-boot2 с web-starter и thymeleaf-starter.
Уже пробовал this , но в моем случае это не работает.
Любой ответ приветствуется , спасибо!