【发布时间】:2014-11-25 15:52:37
【问题描述】:
我使用方法级别的安全性。在课堂上我注释了一些方法,表达式使用了这个类的字段。但我看到 SpEL 异常,我无法引用它们。 这是该类的部分代码。在表达式中,我想使用字段 repPrefix,但我收到它是未知变量的异常。
@Component("c2rTableManager")
@Scope("prototype")
public class C2RTableManager implements TableManager {
private final TableManager tableManager;
private final String repPrefix;
@Autowired
private SecurityInfoService securityInfoService;
public C2RTableManager(TableManager tableManager, String repository) {
this.tableManager = tableManager;
this.repPrefix = repository + "__";
}
...some methods
@Override
@PreAuthorize("hasRole('DBA') || hasPermission(repPrefix + #table, 'TABLE', 'DELETE_TABLE')")
public void dropTable(String table) throws InterruptedException, IOException {
tableManager.dropTable(table);
}
...other methods
}
如果我用另一种方式写,表达式根本不会被评估。不明白为什么。
@Component("c2rTableManager")
@Scope("prototype")
public class C2RTableManager implements TableManager {
private final TableManager tableManager;
private final String repPrefix;
@Autowired
private SecurityInfoService securityInfoService;
public C2RTableManager(TableManager tableManager, String repository) {
this.tableManager = tableManager;
this.repPrefix = repository + "__";
}
...some methods
@Override
public void dropTable(String table) throws InterruptedException, IOException {
dropTable(table, repPrefix);
}
@PreAuthorize("hasRole('DBA') || hasPermission(#repPrefix + #table, 'TABLE', 'DELETE_TABLE')")
public void dropTable(String table, String repPrefix) throws InterruptedException, IOException {
tableManager.dropTable(table);
}
...other methods
}
如何使用该类的字段值为类的方法编写表达式?
【问题讨论】:
-
究竟是什么:SPEL 异常?
-
诸如未知变量之类的东西,无法评估等等。
-
如果您使用接口 AOP 代理,来自同一实例的方法调用将不会评估任何注释。
-
我对AOP一无所知。但是我可以从同一个实例进行方法调用评估注释吗?如果我克隆实例并调用所需的方法,可以吗?
this.clone().dropTable(table, repPrefix);
标签: java spring spring-security spring-el