0

これは、プロパティファイルからコントローラにプロパティを挿入する最も簡単な方法ですか?次に、いくつかのプロパティを必要とする各コントローラーにプロパティファイルをインポートする必要があります。私のようなプロジェクトでは、約30のコントローラーがあり、そのうちの10がこの国のプロパティを必要としているので、混乱しているように見えます。使用法を@Value正しく理解しましたか?

@Controller
@RequestMapping(value = "/simple")
@ImportResource("classpath:/META-INF/properties-config.xml")
public class SimpleController {

    private @Value("#{exampleProperties['simple.country']}") String country;

}

property-config.xml (xmlとスキーマのものをスキップしました)

<beans>
    <util:properties id="exampleProperties" location="classpath:/simple.properties" />
</beans>

また、properties-config.xmlリソースを複数のコントローラーにインポートしようとすると、そのようなメッセージが表示されます。それは正しい方法ではないようですが、私はより良い方法を見つけることができません。

01 Apr 2011 04:52:29,859 INFO  org.springframework.beans.factory.support.DefaultListableBeanFactory []: Overriding bean definition for bean 'exampleProperties': replacing [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Generic bean: class [org.springframework.beans.factory.config.PropertiesFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
4

1 に答える 1

8

この場合、あなたのアプローチは複雑すぎると思います。典型的なアプローチはを使用すること<context:property-placeholder>です。あなたは宣言します

<context:property-placeholder location = "classpath:/simple.properties" />

単一の場所で、コントローラーでそのプロパティを次のように使用します

private @Value("${simple.country}") String country;

また、この方法を使用するのは良い考えではないと思います。@ImportResource依存性注入の原則に違反します。これらのプロパティは、コントローラーが機能しているコンテキストの一部であるため、コントローラーはそれらがどのようにロードされるかを知る必要がありません。

于 2011-04-01T07:34:26.887 に答える