Spring 2.X 深入了解 ------->Resource操作和Validator

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());
        }
    }

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值