Spring的 Resource
接口是为了提供更强的访问底层资源能力的抽象。
?
Resource
接口一些比较重要的方法如下:
-
getInputStream()
: 定位并打开资源,返回读取此资源的一个InputStream
。每次调用预期会返回一个新的InputStream
,由调用者负责关闭这个流。 -
exists()
: 返回标识这个资源在物理上是否的确存在的boolean
值。 -
isOpen()
: 返回标识这个资源是否有已打开流的处理类的boolean
值。如果为true
,则此InputStream
就不能被多次读取,而且只能被读取一次然后关闭以避免资源泄漏。除了InputStreamResource
,常见的resource实现都会返回false
。 -
getDescription()
: 返回资源的描述,一般在与此资源相关的错误输出时使用。此描述通常是完整的文件名或实际的URL地址。
把
Resource
作为属性来配置<bean id="myBean" class="...">
<property name="template" value="some/resource/path/myTemplate.txt"/>
</bean>
例子:
package com.chenhailong.resource; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.core.io.Resource; /** * * @author cnchenhl * Aug 22, 2011 */ public class SpringResource { public static void main(String[] args) throws IOException { ApplicationContext cxt = new FileSystemXmlApplicationContext(); Resource rs = cxt.getResource("1.txt"); File file = rs.getFile(); System.out.println(file.getAbsoluteFile()); BufferedReader reader = new BufferedReader(new FileReader(file)); while (reader.ready()) { System.out.println(reader.readLine()); } reader.close(); ApplicationContext cxtClass = new ClassPathXmlApplicationContext(); Resource template = cxtClass.getResource("classpath:1.txt"); InputStream inputStream = template.getInputStream(); BufferedReader readerClassStream = new BufferedReader(new InputStreamReader(inputStream)); while (readerClassStream.ready()) { System.out.println(readerClassStream.readLine()); } File fileClass = template.getFile(); System.out.println(fileClass.getAbsolutePath()); BufferedReader readerClass = new BufferedReader(new FileReader(file)); while (readerClass.ready()) { System.out.println(readerClass.readLine()); } } }
?
Validator的使用:
package com.chenhailong.validator; /** * @author cnchenhl * Aug 22, 2011 */ public class Person { private String name; private int age; /** * @return the name */ public String getName() { return name; } /** * @return the age */ public int getAge() { return age; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } }?
package com.chenhailong.validator; import org.springframework.validation.Errors; import org.springframework.validation.ValidationUtils; import org.springframework.validation.Validator; /** * @author cnchenhl * Aug 22, 2011 */ public class PersonValidator implements Validator { @Override public boolean supports(Class clazz) { return Person.class.equals(clazz); } @Override public void validate(Object obj, Errors e) { ValidationUtils.rejectIfEmpty(e, "name", "name.empty"); Person p = (Person) obj; if (p.getAge() < 0) { e.rejectValue("age", "negativevalue"); } else if (p.getAge() > 100) { e.rejectValue("age", "too.darn.old"); } } }?
package com.chenhailong.validator; import java.util.List; import org.springframework.validation.BindException; import org.springframework.validation.Errors; import org.springframework.validation.ObjectError; /** * @author cnchenhl * Aug 22, 2011 */ public class SpringValidator { /** * @param args */ public static void main(String[] args) { PersonValidator pv = new PersonValidator(); Person person = new Person(); person.setAge(-10); person.setName(""); pv.supports(Person.class); Errors e = new BindException(person, "person"); pv.validate(person, e); List list = e.getAllErrors(); for (int i = 0; i < list.size(); i++) { System.out.println(((ObjectError) list.get(i)).getCode()); } } }?