Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Java 在Spring的getBean方法返回类实例之前,在类对象的字段中加载映射?_Java_Spring - Fatal编程技术网

Java 在Spring的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-

我有一个.properties文件,它有10个键值对,比如age=10,name=Jon等等。我在spring中配置了一个bean,其中有一个map作为成员变量

当Spring加载bean时,我调用了getBean方法,在此之前,应该使用文件中的属性加载映射。怎么做


我知道这应该在一个生命周期方法中完成,比如使用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>