1.SPRING構成ファイル
コードコピーは次のとおりです。
<bean id = "configproperties"
>
<プロパティ名= "location" value = "classpath:jdbc.properties"/>
</bean>
2。属性メソッドを読み取ります
コードコピーは次のとおりです。
ApplicationContext C = new ClassPathxMLApplicationContext( "classPath:ApplicationContext-DataSource.xml");
プロパティp =(プロパティ)c.getbean( "configproperties");
System.out.println(p.getProperty( "jdbcorcale.driverclassname"));
別の友人は、Spring構成ファイルを読み取る方法を提供します。共有してください。
直接読書方法:
コードコピーは次のとおりです。
public void test()はioExceptionをスローします
{
リソースリソース= applicationContextFactory.getApplicationContext()。getResource( "classpath:com/springdemo/resource/test.txt");
file file = resource.getFile();
byte [] buffer = new byte [(int)file.length()];
fileInputStream is = new FileInputStream(file);
is.read(buffer、0、buffer.length);
is.close();
string str = new String(バッファー);
System.out.println(str);
}
Spring構成を読む:
コードコピーは次のとおりです。
パッケージcom.springdemo.resource;
Import org.springframework.core.io.resource;
パブリッククラスのResourceBean {
プライベートリソースリソース。
Public Resource getResource(){
返品リソース。
}
public void setResource(リソースリソース){
this.resource = resource;
}
}
Spring Bean構成:
コードコピーは次のとおりです。
<! - リソースタイプのリソース属性に直接ファイルパスを割り当てることができます。
<bean id = "resourcebean">
<プロパティ名= "resource" value = "classpath:/com/springdemo/resource/test.txt"> </property>
</bean>