hereとhereで説明されているように、それを行う方法は非常に明確ですが、それでも機能しないようです。Spring Bean にプロパティを注入するために @Value アノテーションを使用したいだけです。1 つのコントローラーと 1 つの Bean を使用して、基本的な Spring MVC プロジェクトを作成しました。
ここに私のアプリケーションコンテキストがあります:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util" xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<context:component-scan base-package="me.co.fatsecret" />
<!-- Properties -->
<bean id="props"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:fatProperties.properties" />
</bean>
</beans>
Configuration と呼ばれる 1 つの Bean があります。
package me.co.fatsecret;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Configuration {
/*--- Members ---*/
@Value("${api_key}")
protected String API_KEY;
@Value("${api_secret}")
protected String API_SECRET;
@Value("${api_url}")
protected String API_URL;
/*--- Constructors ---*/
public Configuration() {
}
/*--- Getters & Setters ---*/
public String getAPI_KEY() {
return API_KEY;
}
public void setAPI_KEY(String aPI_KEY) {
API_KEY = aPI_KEY;
}
public String getAPI_SECRET() {
return API_SECRET;
}
public void setAPI_SECRET(String aPI_SECRET) {
API_SECRET = aPI_SECRET;
}
public String getAPI_URL() {
return API_URL;
}
public void setAPI_URL(String aPI_URL) {
API_URL = aPI_URL;
}
}
これで、この構成クラスが挿入されたコントローラーが 1 つだけになりました。このコントローラーを呼び出すと、構成クラスの値が正しく設定されていないことがわかります。
プロパティ ファイルはリソース フォルダー (src/main/resources) の下にあり、クラスパスの一部です (これは Maven プロジェクトであるため、デフォルトで行われます)。ここにあります:
api_url=http://platform.fatsecret.com/js?
api_key=SomeKey
api_secret=SomeSecret
ファイル名は、fatProperties.properties です。コントローラーを呼び出すときにサーバーをデバッグすると、構成クラスの内容が次のようになることがわかります。
${api_key}
${api_secret}
${api_url}
これは文字列の実際の値です。これは、プロパティ ファイルの値が何らかの理由で挿入されていないことを意味します。
ここで何か不足していますか?
UPDATE1: PropertyPlaceholderConfigurer Bean を次のものに置き換えました。
<context:property-placeholder location="classpath:fatProperties.properties"/>
同じ結果を得る