[英]Implementing Null Object Design Pattern in Spring beans
我使用的是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
}
}
}
现在这段代码new CustomerDTO();
当我尝试访问new CustomerDTO().getCustomerName()
时,CustomerDAO中的值为null
。 我正在考虑使用默认值初始化字段。
如何使用/不使用Spring框架实现此目的?
尝试使用Spring
@Value("myValue")
String customerName;
没有弹簧,只需在声明中赋值,
private String customerName="myValue";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.