JavaWeb_MVC案例之通过配置切换底层存储源
MVC案例之通过配置切换底层存储源
- 1.深入理解面向接口编程:在类中调用接口的方法,而不必关心具体的实现。这将有利于代码的解耦。使程序有更好的可移植性和可扩展性。
- 2.通过配置文件实现动态修改Customer的存储方式。
1)在src目录下添加switch.properties配置文件。
#type=xml
type=jdbc
.
2)CustomerServlet 中不能在通过 private CustomerDAO customerDAO = new CustomerDAOXMLImpl(); 的方式来写死实现类。
3)需要通过一个类的一个方法来获取具体的实现类的对象。
先新建一个CustomerDAOXMLImpl实现类(没有具体实现,只写了输出的方法,以供测试)。
/**
* All rights Reserved,Designed By XS
* @Title: CustomderDAOXMLImpl.java
* @Package com.xs.mvcapp.dao.impl
* @Description: TODO
* @author: XS
* @date: 2019年3月7日 下午9:03:36
* @version V1.0
*/
package com.xs.mvcapp.dao.impl;
import java.math.BigDecimal;
import java.util.List;
import com.xs.mvcapp.dao.CriteriaCustomer;
import com.xs.mvcapp.dao.CustomerDAO;
import com.xs.mvcapp.domain.Customer;
/**
* @ClassName: CustomderDAOXMLImpl
* @Description: TODO
* @author: XS
* @date: 2019年3月7日 下午9:03:36
* @version V1.0
*/
public class CustomerDAOXMLImpl implements CustomerDAO {
/**
* <p>Title: getForListWitCriteriaCustomer</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#getForListWitCriteriaCustomer(com.xs.mvcapp.dao.CriteriaCustomer)
* @param cc
* @return
*/
@Override
public List<Customer> getForListWitCriteriaCustomer(CriteriaCustomer cc) {
System.out.println("getForListWitCriteriaCustomer");
return null;
}
/**
* <p>Title: getAll</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#getAll()
* @return
*/
@Override
public List<Customer> getAll() {
System.out.println("getAll");
return null;
}
/**
* <p>Title: save</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#save(com.xs.mvcapp.domain.Customer)
* @param customer
*/
@Override
public void save(Customer customer) {
System.out.println("save");
}
/**
* <p>Title: get</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#get(int)
* @param id
* @return
*/
@Override
public Customer get(int id) {
System.out.println("get");
return null;
}
/**
* <p>Title: delete</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#delete(int)
* @param id
*/
@Override
public void delete(int id) {
System.out.println("id");
}
/**
* <p>Title: getCountWithName</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#getCountWithName(java.lang.String)
* @param name
* @return
*/
@Override
public BigDecimal getCountWithName(String name) {
System.out.println("getCountWithName");
return null;
}
/**
* <p>Title: update</p>
* <p>Description: </p>
* @see com.xs.mvcapp.dao.CustomerDAO#update(com.xs.mvcapp.domain.Customer)
* @param customer
*/
@Override
public void update(Customer customer) {
System.out.println("update");
}
}
然后新建一个CustomerDAOFactory类。
/**
* All rights Reserved,Designed By XS
* @Title: CustomerDAOFactory.java
* @Package com.xs.mvcapp.dao
* @Description: TODO
* @author: XS
* @date: 2019年3月7日 下午9:16:24
* @version V1.0
*/
package com.xs.mvcapp.dao;
import java.util.HashMap;
import java.util.Map;
import com.xs.mvcapp.dao.impl.CustomerDAOJdbcImpl;
import com.xs.mvcapp.dao.impl.CustomerDAOXMLImpl;
/**
* @ClassName: CustomerDAOFactory
* @Description: TODO
* @author: XS
* @date: 2019年3月7日 下午9:16:24
* @version V1.0
*/
public class CustomerDAOFactory {
private Map<String, CustomerDAO> daos = new HashMap<String, CustomerDAO>();
private CustomerDAOFactory() {
daos.put("jdbc", new CustomerDAOJdbcImpl());
daos.put("xml", new CustomerDAOXMLImpl());
}
private static CustomerDAOFactory instance = new CustomerDAOFactory();
public static CustomerDAOFactory getInstance() {
return instance;
}
private String type = null;
public void setType(String type) {
this.type = type;
}
public CustomerDAO getCustomerDAO() {
return daos.get(type);
}
}
在Web应用启动时,为了初始化type,新建一个InitServlet。
package com.xs.mvcapp.servlet;
import java.io.InputStream;
import java.util.Properties;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import com.xs.mvcapp.dao.CustomerDAOFactory;
/**
* Servlet implementation class InitServlet
*/
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see Servlet#init(ServletConfig)
*/
@Override
public void init() throws ServletException {
InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/switch.properties");
Properties properties = new Properties();
try {
properties.load(in);
String type = properties.getProperty("type");
CustomerDAOFactory.getInstance().setType(type);
} catch (Exception e) {
e.printStackTrace();
}
}
}
修改web.xml文件,使InitServlet在Web应用被启动是加载。
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.xs.mvcapp.servlet.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
当前 WEB 应用才启动的时候,InitServlet 被创建,并由 Servlet 容器调用其 init() 方法:
1.读取类路径下的 switch.properties 文件。
2.获取 switch.properties 的 type 属性值。
3.赋给了 CustomerDAOFactory 的 type 属性值。
将CustomerServlet中的
private CustomerDao customerDAO = new CustomerDAOJdbcImpl();
修改为
private CustomerDAO customerDAO = CustomerDAOFactory.getInstance().getCustomerDAO();
创建 CustomerServlet 时,为 customerDAO 属性赋值是通过 CustomerDAOFactory 的 getCustomerDAO() 方法完成的 。此时的 type 已经在 InitServlet 中被赋值了。