[英]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.