以前写的代码
mapper层
public interface PersonMapper {
void selectPersonList();
}
public class PersonMapperImpl implements PersonMapper {
@Override
public void selectPersonList() {
System.out.println("PersonMapperImpl selectPersonList");
}
}
service层
public interface PersonService {
void selectPersonList();
}
public class PersonServiceImpl implements PersonService {
private PersonMapper personMapper = new PersonMapperImpl;
@Override
public void selectPersonList() {
personMapper.selectPersonList();
}
}
controller层
private PersonService personService = new PersonServiceImpl();
public void selectPersonList(){
personService.selectPersonList();
}
可以发现的是代码耦合度非常的高,当我们修改PersonService的实现类需要修改多出代码,如何解决呢?
首先使用工厂模式创建对象
public class MyBeanFactory {
public static Object getBean(String beanName) {
if ("personService".equals(beanName)) {
return new PersonServiceImpl();
} else if ("personController".equals(beanName)) {
return new PersonController();
} else {
return new PersonMapperImpl();
}
}
}
mapper层
public interface PersonMapper {
void selectPersonList();
}
public class PersonMapperImpl implements PersonMapper {
@Override
public void selectPersonList() {
System.out.println("PersonMapperImpl selectPersonList");
}
}
service层
public interface PersonService {
void selectPersonList();
}
public class PersonServiceImpl implements PersonService {
private PersonMapper personMapper =(PersonMapper) MyBeanFactory.getBean("personMapper");
@Override
public void selectPersonList() {
personMapper.selectPersonList();
}
}
controller层
private PersonService personService = (PersonService) MyBeanFactory.getBean("personService");
public void selectPersonList(){
personService.selectPersonList();
}
测试一下是可以达到效果的,但是还是存在代码耦合度高的问题
继续优化,我们把MyBeanFactory的创建bean方法修改一下
public static Object getBean(String clazzName) {
if (clazzName != null) {
Object obj = null;
try {
obj = Class.forName(clazzName).newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
mapper层
public interface PersonMapper {
void selectPersonList();
}
public class PersonMapperImpl implements PersonMapper {
@Override
public void selectPersonList() {
System.out.println("PersonMapperImpl selectPersonList");
}
}
service层
public interface PersonService {
void selectPersonList();
}
public class PersonServiceImpl implements PersonService {
private PersonMapper personMapper =(PersonMapper) MyBeanFactory.getBean("com.qbb.spring.mapper.impl.PersonMapperImpl");
@Override
public void selectPersonList() {
personMapper.selectPersonList();
}
}
controller层
private PersonService personService = (PersonService) MyBeanFactory.getBean("com.qbb.spring.service.impl.PersonServiceImpl");
public void selectPersonList(){
personService.selectPersonList();
}
此时我们传递的是权限定类名,降低了一些代码耦合度,但是又带来了字符串硬编码问题
继续优化,修改MyBeanFactory
// 创建对象时就加载
public MyBeanFactory() {
parseXML();
}
// 定义一个Map存储bean对象
private static Map map = new HashMap<>();
// 解决字符串硬编码
public void parseXML() {
// 创建解析XML对象
SAXReader saxReader = new SAXReader();
InputStream is = MyBeanFactory.class.getClassLoader().getResourceAsStream("MyBean.xml");
try {
// 读取配置文件
Document docment = saxReader.read(is);
// 获取跟标签
Element rootElement = docment.getRootElement();
// 获取字标签
List elements = rootElement.elements();
for (Element element : elements) {
String id = element.attributeValue("id");
String clazz = element.attributeValue("class");
Object newInstance = Class.forName(clazz).newInstance();
map.put(id, newInstance);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 获取bean
public static Object getBean(String id) {
return map.get(id);
}