2016-08-04 5 views
3

私は春の技術を学んでいる間に奇妙なことを発見しました。スプリングセットjava.io.Fileプロパティjava.lang.String beanを使用

タイプjava.io.Fileのbeanプロパティーにjava.lang.Stringタイプのbeanを挿入しますが、プログラムは正常に実行されます。

私は内部で何が起こった

  1. を知りたいですか?
  2. これは有効な使用法ですか、それとも有為なのでしょうか?

ここには、スプリング構成ファイル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()); 
    } 
} 
+0

を。ありがとう。 –

答えて

1

それは、詳細はこちらをドキュメントを確認してください、あなたのケースでのPropertyEditorでFileEditor

が行われます。私はあなたのコードをテストし、それが動作http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html

+0

これはAnnotationConfigApplicationContextで動作しますか?私のコードを私の[ここに質問]で見てください(http://stackoverflow.com/q/43213385/3213514) –

関連する問題