关键是,这仅对单元测试有用。在实际应用程序中,语言环境是无法在注释中进行硬编码的运行时信息。语言环境是根据运行系统中的用户语言环境决定的。
顺便说一句,您可以轻松地自己实现此功能,例如:
@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.FIELD})public @interface Localize { String value();}和
public class CustomAnnotationBeanPostProcessor implements BeanPostProcessor { public Object postProcessAfterInitialization(Object bean, String beanName) { return bean; } public Object postProcessBeforeInitialization(Object bean, String beanName) { Class clazz = bean.getClass(); do { for (Field field : clazz.getDeclaredFields()) { if (field.isAnnotationPresent(Localize.class)) { // get message from ResourceBundle and populate the field with it } } clazz = clazz.getSuperclass(); } while (clazz != null); return bean; }


