在定义标记库函数时,可以使用spring方法而不是静态方法吗?
目前,应用程序只使用抽象类的静态方法:
<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd"
version="2.2">
<namespace>http://my/security/facelets/tags</namespace>
<function>
<function-name>isRegion</function-name>
<function-class>my.NovaFaceletsAuthorizeTagUtils</function-class>
<function-signature>boolean isRegion()</function-signature>
</function>发布于 2021-10-05 08:54:53
不是,但您可以委托bean方法。例如,就像这样:
public static boolean isRegion() {
getCurrentApplicationContext().getBean(RegionService.class).isRegion();
}获取当前ApplicationContext的方法有多种,这取决于您是如何引导它的,以及您有多少ApplicationContext。关于相关技术的概述,见:
在简单的情况下,在bean是应用程序作用域且没有AOP通知的情况下(特别是no @Transactional),将bean本身放入静态字段可能会更容易:
@Component
public class RegionService {
private static RegionService instance;
public RegionService() {
instance = this;
}
public static RegionService getInstance() {
return instance;
}
}所以您可以使用RegionService.getInstance()从任何地方访问bean。
https://stackoverflow.com/questions/69446844
复制相似问题