私は春の技術を学んでいる間に奇妙なことを発見しました。スプリングセットjava.io.Fileプロパティjava.lang.String beanを使用
タイプjava.io.File
のbeanプロパティーにjava.lang.String
タイプのbeanを挿入しますが、プログラムは正常に実行されます。
私は内部で何が起こった
- を知りたいですか?
- これは有効な使用法ですか、それとも有為なのでしょうか?
ここには、スプリング構成ファイルstringtofile.xml
があります。
<?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:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"
default-lazy-init="true">
<bean id="file_str"
class="java.lang.String"
c:_="C:\tmp\test.hi"/>
<bean id="file"
class="stringtofile.FileWrapper"
p:file-ref="file_str"/>
</beans>
ここは私のテストクラスです。
package stringtofile;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.File;
public class FileWrapper {
File file;
public File getFile() {
return file;
}
public FileWrapper setFile(File file) {
this.file = file;
return this;
}
public static void main(String[] args) {
ApplicationContext ctx =
new ClassPathXmlApplicationContext("stringtofile.xml");
FileWrapper fileWrapper =
(FileWrapper) ctx.getBean("file");
System.out.println(fileWrapper.getFile());
}
}
を。ありがとう。 –