0

我正在使用 CustomerDTO:

public class CustomerDTO {

    private int customerId;
    private String customerName;
    private String customerAddress;
    public int getCustomerId() {
        return customerId;
    }
    public void setCustomerId(int customerId) {
        this.customerId = customerId;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
    public String getCustomerAddress() {
        return customerAddress;
    }
    public void setCustomerAddress(String customerAddress) {
        this.customerAddress = customerAddress;
    }

}

然后我使用 CustomerDAO 我根据 customerId 获取客户

public class CustomerDAO {
    private CustomerDTO customer;

    public void setCustomer(CustomerDTO customer) {
        this.customer = customer;
    }

    public CustomerDTO getCustomer(int customerId){
        try{
            if("you get customer based on customer id") then "return the customer";
        }catch(Exception ex){
            return new CustomerDTO();
              //some other work
        }
    }
}

现在,当我尝试访问时,CustomerDAO 中的这段代码new CustomerDTO();会给出。我正在考虑使用默认值初始化该字段。nullnew CustomerDTO().getCustomerName()

使用/不使用 Spring 框架如何实现这一点?

4

1 回答 1

2

试试春天

@Value("myValue")
String customerName;

没有spring,只需在声明本身中赋值,

 private String customerName="myValue";
于 2013-07-08T09:23:00.853 回答