更改字符串值的注释

发布于 2024-12-01 11:28:40 字数 307 浏览 0 评论 0原文

spring或java中是否有注释可以转换给定的字符串?

例如,Spring有注释@Value(“some string”)。如果我不想将“某个字符串”分配给参数/实例变量,而是想分配该字符串的转换值,该怎么办?假设字符串是“foo”。每次我看到这个注释时,我都希望返回的字符串是“bar:foo”而不是foo。我想要的只是能够在参数或实例变量上放置注释,并使该转换自动发生。也许甚至是一个接受类和字符串的注释,以便该类充当给定字符串的转换器。

spring或java中是否有注释可以做到这一点,如果没有,实现这样的事情的最佳方法是什么?

谢谢 劳伦

Is there an annotation in spring or java that transforms a given string?

For example, Spring has the annotation @Value("some string"). What if instead of assigning "some string" to the parameter/instance variable whatever, I want to assign a transformed value of that string. Let's say the string is "foo". Every time I see this annotation I want the string returned to be "bar:foo" and not foo. All I want is to be able to place an annotation over a parameter or instance variable and for that transformation to occur automatically. Perhaps maybe even an annotation that takes a class as well as the string such that the class acts as the transformer for the given string.

Is there an annotation in spring or java that does this and if not what would be the best way to go about implementing such a thing?

Thanks
Lauren

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由范儿 2024-12-08 11:28:40

您可以在 @Value 注释中使用 Spring 表达式语言。

这就是您将其用于静态方法的方式:

@Value("#{T(fully.qualified.package.name.to.class).getFooString()}")
public void setBar(String value){
   this.bar = value;
}

如果您之前定义了一个 bean,那么您可以简单地使用
@Value("#{bean.method()}")@Value("#{bean.property}")

You can use Spring Expression Language inside your @Value annotation.

This is how you'll use it for a static method:

@Value("#{T(fully.qualified.package.name.to.class).getFooString()}")
public void setBar(String value){
   this.bar = value;
}

If you have a bean define previously then you can simply use
@Value("#{bean.method()}") or @Value("#{bean.property}")

那小子欠揍 2024-12-08 11:28:40

Spring 的 @Value 注释对于使用 Spring 表达式语言将常量以外的值分配给属性非常有用。例如,从属性文件读取的值。如果它所做的只是分配一个常量,您也可以使用private String foo = "bar";

因此,您可以使用它从某些配置文件或系统属性中获取所需的值。

如果您想要的是将某种转换算法应用于字段,我真的不明白使用注释的意义。只需调用 Java 方法:

@Value("foo") // or some EL expression which evaluates to "foo"
public void setBar(String value) {
    this.bar = someStringTransformer.transform(value);
}

这似乎很容易阅读、测试和维护。

The @Value annotation of Spring is useful to assign a value other than a constant to a property, using the Spring expression language. For example, a value read from a properties file. If all it did was assigning a constant, you could as well use private String foo = "bar";.

So, you might use it to get the value you want from some configuration file or system property.

If what you want is to apply some transformation algorithm to a field, I don't really see the point of using an annotation. Just call a Java method:

@Value("foo") // or some EL expression which evaluates to "foo"
public void setBar(String value) {
    this.bar = someStringTransformer.transform(value);
}

That seems easy to read, test and maintain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文