プロパティファイルがSpring ApplicationContextで使用されている場合、そのプロパティには次のようにアクセスできます。$ {someproperty} xml設定ファイル内。しかし、あなたはどのようにxmlを介してそれを注入せずにあなたのJavaコード内の同じプロパティにアクセスしますか?ApplicationContext経由でプロパティにアクセスするにはどうすればいいですか
ApplicationContextのコンフィグ
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="myapp.properties" />
</bean>
<bean class="my.app.MyClass">
<property name="foo" value="${someproperty}" />
</bean>
</beans>
プロパティ
someproperty=somevalue
更新1
本の実際のポイントは、アプリケーションの一意のIDである特殊なケースでありますプロパティファイル(sysadminによる編集)で設定します。アプリケーションクラスのいくつかは、コンテキストにアクセスできるようにApplicationContextAwareを実装し、ezプロパティアクセスメソッドを必要とするすべてのクラスのBeanを定義するか、各クラスの注入を防止します。この場合、Springに関する「知っている」アプリケーションは問題ではありません。
"各クラスの注入を禁止するか、すべてのクラスのBeanを定義しない"を説明できますか? – kan
注釈を使用していない場合は、プロパティ自体にアクセスする必要がある各Beanに対して「property」タグが必要です。それは、クラスからXMLに作品を移動するだけです。 –
私は実際にこのブログ記事のような設定クラスを使うことに傾いています:http://chrislovecnm.com/2010/03/08/spring-3-java-based-configuration-with-value/ –