Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring中的属性,在Java应用程序中访问_Java_Spring_Properties - Fatal编程技术网

Spring中的属性,在Java应用程序中访问

Spring中的属性,在Java应用程序中访问,java,spring,properties,Java,Spring,Properties,我有这样一个属性集: <context:property-placeholder location="file:${catalina.home}/conf/my.properties" ignore-unresolvable="true" /> <bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage"> <property nam

我有这样一个属性集:

<context:property-placeholder
        location="file:${catalina.home}/conf/my.properties"
        ignore-unresolvable="true" />
<bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">
  <property name="to">
    <value>${app.email}</value>
  </property>
</bean>

我想在其他地方使用属性设置“app.email”的值,除了alertMailMessage,最好的方法是什么?(顺便说一句,alertMailMessage工作正常)

您不能在hibernate实体中设置它,因为hibernate实体不是由spring管理的

在创建hibernate实体的spring服务中使用
@Value
注释,并在需要时手动设置它。但是在数据库中存储默认值看起来很奇怪,所以请重新考虑


附带说明:如果使用aspectJ和
@Configurable
,您可以让spring管理hibernate实体,但这可能会使事情变得不必要的复杂。

您如何创建包含
defaultEmailAddress
的bean?如果它是使用
new
手动创建的,那么spring无法截取它并应用
@Value
注释。@beny23所以我需要创建一个包含我想要访问的所有属性的Springbean,然后在我的非Springbean中引用它?或者让我的hibernate实体成为SpringBean——这听起来很痛苦?我不认为让你的hibernate实体成为SpringBean会是答案,因为在查找时实例化实体的是hibernate,所以如果没有hibernate的一些定制,spring将无法控制hibernate实例化的实体。但是,我不知道为什么您希望实体中有“defaultEmailAddress”(系统范围的设置)?为什么ut很奇怪?如果业务没有与字段关联的数据,则该字段应具有默认值,该默认值取决于上下文/部署。。。我将创建一个Springbean来包含它,然后引用它。更合理?是的,你只能在春豆里吃。我说这很奇怪,因为如果它是默认值,那么所有记录都是一样的。您已经将其作为配置属性。但如果你的情况没有问题,那就开始吧。也许这是一个奇怪的要求,但除非业务覆盖了它,否则所有记录都应该是相同的。谢谢
@Value("${app.email}")
private String defaultEmailAddress;