从实体访问属性的春季方式是什么? [英] What's the Spring way of accessing properties from an entity?
问题描述
我是Spring的新手,我正在构建一个应用程序,其中某些实体(JPA/Hibernate)需要从application.properties访问属性.我确实有一个简单的配置类:
I'm new to Spring and I'm building an application where some entities (JPA/Hibernate) need access to a property from application.properties. I do have a configuration class in which this is trivial:
@Configuration
public class FactoryBeanAppConfig {
@Value("${aws.accessKeyId}")
private String awsAccessKeyId;
@Value("${aws.secretKey}")
private String awsSecretKey;
}
但是由于实体没有并且我认为它们不应该具有诸如@Configuration
或@Component
之类的注释,因此Spring访问该属性的方式是什么?
but since entities do not have and I think they should not have the annotations such as @Configuration
or @Component
, what's the Spring way for them to access the property?
现在,我知道我可以创建自己的类,自己的bean,并将其作为属性的简单包装.但这是Spring的方法吗?还是还有另一种方法?
Now, I know I can create my own class, my own bean, and make it as a simple wrapper around the properties; but is that the Spring way to do it or is there another way?
推荐答案
使用@PropertySource
指定属性文件的位置
如下
specify Property file location using @PropertySource
Something like below
@PropertySource("classpath:/application.proerties")
您还需要在配置中添加以下bean
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigIn() {
return new PropertySourcesPlaceholderConfigurer();
}
这篇关于从实体访问属性的春季方式是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!