简体   繁体   English

如何使用属性中的Spring @Value来设置注释属性

[英]How to use a Spring @Value from properties to set an annotation attribute

I'm trying to set an attribute in an annotation, using Spring @Value , but I get Type mismatch: cannot convert from Value to String . 我正在尝试使用Spring @Value在注释中设置属性,但我得到Type mismatch: cannot convert from Value to String Here is what I tried: 这是我尝试过的:

@Table(name = "myTable", catalog = @Value("${database.myCatalog}") )

Is it possible? 可能吗? And if yes, how to do it? 如果是的话,该怎么做?

I think you are a little bit confused with how Spring uses that annotation. 我认为你对Spring如何使用该注释有点困惑。

As far as I know, the only way that annotation can only be set at field or method/constructor parameters . 据我所知,注释只能在字段或方法/构造函数参数中设置的唯一方法

Also, for Spring to resolve it, the POJO must be a Spring managed bean. 另外,要Spring解决它,POJO必须是Spring托管bean。 That means that it must be defined in the Spring (Web)ApplicationContext implementation to be resolved. 这意味着必须在Spring(Web)ApplicationContext实现中定义它才能被解析。

Your question seems like you are annotating a JPA Entity which is not a Spring bean but a Class to be used by the JPA implementation that you are using (eg Hibernate). 您的问题似乎是在注释一个JPA实体,它不是一个Spring bean,而是一个由您正在使用的JPA实现使用的类(例如Hibernate)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring @Value 注解设置值? - How to use Spring @Value annotation to set values? 我可以在另一个注释中使用来自属性的 Spring @Value 变量吗? - Can I use a Spring @Value variable from properties in another annotation? 如何根据 Spring Boot 中的活动配置文件设置注释属性值? - How to set annotation attribute value based on the active profile in Spring Boot? 如何从Spring Boot中的application.properties中分配注释值? - How to assign annotation value from application.properties in Spring Boot? 如何在Spring数据Elasticsearch中设置注释属性 - How to set the annotation properties in spring data Elasticsearch 如何在属性文件的@Size注释中设置最大值? - How to set max value in @Size annotation from properties file? 如何从属性文件导入值并在注释中使用它? - How to import value from properties file and use it in annotation? 如何在spring boot中从属性文件为@Order注释设置值 - how to set value to @Order annotation in spring boot from a propertyfile Spring Boot @Value 注解从 application.properties 中获取密钥,但在运行时不使用它 - Spring Boot @Value annotation picks up key from application.properties, but does not use it at run time Java:如何将默认值设置为注释,并将另一个注释作为其属性? - Java: how to set default value to annotation with another annotation as its attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM