Java 从自定义批注读取属性
我有一个带有如下注释的Java 从自定义批注读取属性,java,spring,spring-aop,Java,Spring,Spring Aop,我有一个带有如下注释的方面 @Target(value = { ElementType.METHOD, ElementType.TYPE}) @Retention(value = RetentionPolicy.RUNTIME) public @interface LoggerExecute { public String action(); } 因此,我可以将注释放在要记录的函数上。例如: @RequestMapping(....) @LoggerExecute(action =
方面
@Target(value = { ElementType.METHOD, ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface LoggerExecute {
public String action();
}
因此,我可以将注释放在要记录的函数上。例如:
@RequestMapping(....)
@LoggerExecute(action = "Logging submitting data")
public ModelAndView submitData(...) {
....
}
现在我有了一个eventLogs.properties
,它包含了操作的所有文本,位于参考资料文件夹中。从LoggerExecute
注释中读取.properties
文件中的键的简单方法是什么。在这种情况下,注入环境变量或重写某些类似乎太多了注释属性值必须是常量表达式。因此,你不能做类似的事情
@LoggerExecute(action = loadFromProperties(someKey))
您可以在注释中创建并使用字符串
键值
@LoggerExecute(action = "myapp.action.submit.data")
然后,在您的建议中,检索注释,在属性文件中查找action
值,并使用它