public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
CustomerDAOFactory.getInstance().setType("xml");
InputStream in =
getServletContext().getResourceAsStream("/WEB-INF/classes/switch.properties");
Properties pros = new Properties();
try {
pros.load(in);
String type = pros.getProperty("type");
CustomerDAOFactory.getInstance().setType(type);
} catch (Exception e) {
e.printStackTrace();
}
}
上面的servlet设置了1
public class CustomerDAOFactory {
private static String type = null;
private static CustomerDAOFactory instance = new CustomerDAOFactory();
private Map<String,CustomerDAO> docs = new HashMap<String,CustomerDAO>();
private CustomerDAOFactory() {
docs.put("jdbc", new CustomerDAOImpl());
docs.put("xml", new CustomerDAOXMLImpl());
}
public static CustomerDAOFactory getInstance() {
return instance;
}
public void setType(String type) {
this.type = type;
}
public CustomerDAO getCustomerDAO() {
return docs.get(type);
}
}
为什么加上static 写成private static Map<String,CustomerDAO> docs
就会在第一段代码的这个地方:CustomerDAOFactory.getInstance().setType("xml");报错