Springアノテーションを使用してプロパティファイルのコンテンツを挿入します:PropertiesFactoryBean



Inject Properties File Content Using Spring Annotations



[url] http://my.oschina.net/simpleton/blog/489129 [/ url]
最初に作業ディレクトリを見て、次に説明します
[img] http://static.oschina.net/uploads/space/2015/0807/152734_RcsT_1158633.jpg [/ img]


1、config.propertiesを作成します。私のconfig.propertiesの内容は次のとおりです。
[color = darkblue] author_name = luolin
Project_info =このプロジェクトは主にデモを書くために使用されます[/ color]


2. Spring構成ファイルを構成し、プロパティファイルを読み取り、エンコード形式を設定します。私のプロジェクト構造図から、2つのSpring構成ファイルを使用したことが誰にでもわかります。実際、spring-context.xmlで構成されている他のコンテンツはありません。パッケージcom.eya.propertyをスキャンするように構成しました。質問があるかもしれません。 com.eyaをスキャンするようにパッケージスキャンがspring-mvc.xmlで直接構成されていない理由。実際、これは習慣の問題であり、さまざまなことがさまざまなことを行います。spring-mvc.xmlでは、com.eya.controllerパッケージをスキャンするように構成しただけです。

class='org.springframework.beans.factory.config.PropertiesFactoryBean'>


classpath:config.properties







3.config.propertiesファイルの値に対応するConfigProperty.javaファイルを書き込みます。アノテーション@Comopnent( 'configProperty')をSpringコンテナマネージャーに追加し、コンポーネント名をconfigPropertyとして指定します。

ここで、@ Valueアノテーションを使用する場合、内部形式は#{beanID [propertyKey]}です。ここで、beanIDは2番目のステップでPropertiesFactoryBeanを構成するときに指定されたID値であり、propertyKeyはconfig.propertiesにあります。キーは対応します。
/**
*
*/
package com.eya.property

import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component

/**
* config.properties file mapping class
* @author luolin
*
* @version $id:ConfigProperty.java,v 0.1 August 7, 2015 2:10:44 PM luolin Exp $
*/
@Component('configProperty')
public class ConfigProperty {

/** Author's name */
@Value('#{setting[author_name]}')
private String authorName
/** Project Information */
@Value('#{setting[project_info]}')
private String projectInfo

/**
* @return the authorName
*/
public String getAuthorName() {
return authorName
}

/**
* @param authorName the authorName to set
*/
public void setAuthorName(String authorName) {
this.authorName = authorName
}

/**
* @return the projectInfo
*/
public String getProjectInfo() {
return projectInfo
}

/**
* @param projectInfo the projectInfo to set
*/
public void setProjectInfo(String projectInfo) {
this.projectInfo = projectInfo
}

}


4.ユニットテストを作成して、注入が成功したかどうかをテストします。これが、演習としてのJunit4 + Springアノテーションの方法です。
/**
*
*/
package com.eya.property


import javax.annotation.Resource

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner

/**
* JUnit4 + Spring annotations for unit testing, testing the values ​​of the Properties file obtained via Spring annotations
* @author luolin
*
* @version $id:ConfigPropertyTest.java,v 0.1 August 7, 2015 2:21:26 PM luolin Exp $
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={'classpath:spring-mvc.xml','classpath:spring-context.xml'})
public class ConfigPropertyTest {

@Resource(name = 'configProperty')
private ConfigProperty configProperty

/**
* Test the Spring annotation to get the value of the properties file
*/
@Test
public void test() {
System.out.println(configProperty.getAuthorName())
System.out.println(configProperty.getProjectInfo())
}

}


結果は次のとおりです。
[img] http://static.oschina.net/uploads/space/2015/0807/153644_7O5a_1158633.jpg [/ img]


期間中に文字化けした問題がありました。 config.propertiesのエンコーディングをUTF-8に変更したため、Springファイルを構成するときにエンコーディングを指定しなかったため、文字化けしていました。後で、その中のPropertiesFactoryBeanのソースコードを調べました。エンコーディングを設定するプロパティは親クラスにあり、対応するエンコーディングを設定できます。