1. ไฟล์กำหนดค่า
การคัดลอกรหัสมีดังนี้:
<bean id = "configproperties"
-
<property name = "location" value = "classpath: jdbc.properties"/>>>
</ebean>
2. อ่านวิธีแอตทริบิวต์
การคัดลอกรหัสมีดังนี้:
ApplicationContext C = ใหม่ classpathxmlapplicationContext ("classpath: ApplicationContext-datasource.xml");
คุณสมบัติ P = (คุณสมบัติ) C.GetBean ("configProperties");
System.out.println (P.GetProperty ("JDBCorcale.DriverClassName"));
เพื่อนอีกคนหนึ่งให้วิธีการอ่านไฟล์การกำหนดค่าสปริงโปรดแชร์
วิธีการอ่านโดยตรง:
การคัดลอกรหัสมีดังนี้:
การทดสอบโมฆะสาธารณะ () โยน ioexception
-
ทรัพยากรทรัพยากร = ApplicationContextFactory.getApplicationContext (). getResource ("classpath: com/springdemo/resource/test.txt");
ไฟล์ไฟล์ = resource.getFile ();
Byte [] buffer = byte ใหม่ [(int) file.length ()];
FileInputStream คือ = ใหม่ FileInputStream (ไฟล์);
is.read (บัฟเฟอร์, 0, buffer.length);
is.close ();
string str = สตริงใหม่ (บัฟเฟอร์);
System.out.println (str);
-
อ่านผ่านการกำหนดค่าฤดูใบไม้ผลิ:
การคัดลอกรหัสมีดังนี้:
แพ็คเกจ com.springdemo.resource;
นำเข้า org.springframework.core.io.resource;
ResourceBean ชั้นเรียนสาธารณะ {
ทรัพยากรทรัพยากรส่วนตัว
ทรัพยากรสาธารณะ getResource () {
กลับทรัพยากร;
-
โมฆะสาธารณะ setResource (ทรัพยากรทรัพยากร) {
this.resource = ทรัพยากร;
-
-
การกำหนดค่าถั่วสปริง:
การคัดลอกรหัสมีดังนี้:
<!-คุณสามารถกำหนดเส้นทางไฟล์ไปยังแอตทริบิวต์ทรัพยากรของประเภททรัพยากรโดยตรง
<bean id = "ResourceBean">
<property name = "resource" value = "classpath: /com/springdemo/resource/test.txt
</ebean>