Reputation: 33
I found a strange thing while learning Spring tech.
I inject a java.lang.String
type bean into a bean property which type is java.io.File
, but the program still runs normally.
I want to know
Here is the spring configuration file 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>
Here is my test classes.
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());
}
}
Upvotes: 3
Views: 755
Reputation: 835
It is done by the PropertyEditors in your case the FileEditor
Check the documentation here for more details: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/validation.html
Upvotes: 1