Java 在Spring的getBean方法返回类实例之前,在类对象的字段中加载映射?
我有一个.properties文件,它有10个键值对,比如age=10,name=Jon等等。我在spring中配置了一个bean,其中有一个map作为成员变量 当Spring加载bean时,我调用了getBean方法,在此之前,应该使用文件中的属性加载映射。怎么做Java 在Spring的getBean方法返回类实例之前,在类对象的字段中加载映射?,java,spring,Java,Spring,我有一个.properties文件,它有10个键值对,比如age=10,name=Jon等等。我在spring中配置了一个bean,其中有一个map作为成员变量 当Spring加载bean时,我调用了getBean方法,在此之前,应该使用文件中的属性加载映射。怎么做 我知道这应该在一个生命周期方法中完成,比如使用InitializingBean或init方法配置的AfterPropertieSet。还有其他更好的方法吗?您可以在bean中启用注释: <context:annotation-
我知道这应该在一个生命周期方法中完成,比如使用InitializingBean或init方法配置的AfterPropertieSet。还有其他更好的方法吗?您可以在bean中启用注释:
<context:annotation-config />
另一个选项是直接将属性
注入bean,并提供类似于地图的API来访问它们:
<util:properties id="myProperties" location="classpath:my-props.properties">
<bean id="myBean" class="com.example.MyBean">
<property name="properties" ref="myProperties" />
</bean>
您可以使用${…}在XML spring配置文件中使用上下文属性占位符,也可以使用@value从java类配置文件中使用上下文属性占位符
<util:properties id="myProperties" location="classpath:my-props.properties">
<bean id="myBean" class="com.example.MyBean">
<property name="properties" ref="myProperties" />
</bean>