IOC(Inversion of Control):控制反转
在软件系统中,层与层之间是存在依赖的。我们也称之为耦合。
我们系统架构或者是设计的一个原则是: 高内聚低耦合。
层内部的组成应该是高度聚合的,而层与层之间的关系应该是低耦合的,最理想的情况0耦合(就是没有耦合)
2) IOC - 控制反转 / DI - 依赖注入
BeanFactory 接口
public interface BeanFactory {
Object getBean(String id);
ClassPathXmlApplicationContext 类
public class ClassPathXmlApplicationContext implements BeanFactory {
private Map beanMap = new HashMap<>();
public ClassPathXmlApplicationContext() {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("applicationContext.xml");
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(inputStream);
NodeList beanNodeLists = document.getElementsByTagName("bean");
for (int i = 0; i < beanNodeLists.getLength(); i++) {
Node beanNode = beanNodeLists.item(i);
if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
Element beanElement = (Element) beanNode;
String beanId = beanElement.getAttribute("id");
String className = beanElement.getAttribute("class");
Class beanClass = Class.forName(className);
Object beanObj = beanClass.newInstance();
beanMap.put(beanId, beanObj);
for (int i = 0; i < beanNodeLists.getLength(); i++) {
Node beanNode = beanNodeLists.item(i);
if (beanNode.getNodeType() == Node.ELEMENT_NODE) {
Element beanElement = (Element) beanNode;
String beanId = beanElement.getAttribute("id");
NodeList beanChildNodeList = beanElement.getChildNodes();
for (int j = 0; j < beanChildNodeList.getLength(); j++) {
Node beanChildNode = beanChildNodeList.item(j);
if (beanChildNode.getNodeType() == Node.ELEMENT_NODE && "property".equals(beanChildNode.getNodeName())) {
Element propertyElement = (Element) beanChildNode;
String propertyName = propertyElement.getAttribute("name");
String propertyRef = propertyElement.getAttribute("ref");
Object refObj = beanMap.get(propertyRef);
Object beanObj = beanMap.get(beanId);
Class beanClass = beanObj.getClass();
Field propertyField = beanClass.getDeclaredField(propertyName);
propertyField.setAccessible(true);
propertyField.set(beanObj, refObj);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
} catch (IllegalAccessException e) {
} catch (InstantiationException e) {
} catch (ClassNotFoundException e) {
} catch (NoSuchFieldException e) {
public Object getBean(String id) {

FruitController 类
public class FruitController{
private FruitService fruitService = null;
private String update(Integer fid,String fname,Integer price,Integer fcount,String remark){
fruitService.updateFruit(new Fruit(fid, fname, price, fcount, remark));
return "redirect:fruit.do";
private String edit(Integer fid,HttpServletRequest request){
Fruit fruit = fruitService.getFruitByFid(fid);
request.setAttribute("fruit", fruit);
private String del(Integer fid){
fruitService.delFruit(fid);
return "redirect:fruit.do";
private String add(String fname,Integer price,Integer fcount,String remark){
Fruit fruit = new Fruit(0, fname, price, fcount, remark);
fruitService.addFruit(fruit);
return "redirect:fruit.do";
private String index(String oper,String keyword,Integer pageNo,HttpServletRequest request){
HttpSession session = request.getSession();
if (StringUtil.isNotEmpty(oper) && "search".equals(oper)) {
if (StringUtil.isEmpty(keyword)) {
session.setAttribute("keyword", keyword);
Object keywordobj = session.getAttribute("keyword");
if (keywordobj != null) {
keyword = (String) keywordobj;
session.setAttribute("pageNo", pageNo);
List fruitList = fruitService.getFruitList(keyword, pageNo);
session.setAttribute("fruitList", fruitList);
int pageCount = fruitService.getPageCount(keyword);
session.setAttribute("pageCount", pageCount);

FruitService 接口
public interface FruitService {
List getFruitList(String keyword, Integer pageNo);
void addFruit(Fruit fruit);
Fruit getFruitByFid(Integer fid);
void delFruit(Integer fid);
void updateFruit(Fruit fruit);
Integer getPageCount(String keyword);
FruitServiceImpl 类
public class FruitServiceImpl implements FruitService{
private FruitDAO fruitDAO = null;
public List getFruitList(String keyword, Integer pageNo) {
return fruitDAO.getFruitList(keyword,pageNo);
public void addFruit(Fruit fruit) {
fruitDAO.addFruit(fruit);
public Fruit getFruitByFid(Integer fid) {
return fruitDAO.getFruitByFid(fid);
public void delFruit(Integer fid) {
public void updateFruit(Fruit fruit) {
fruitDAO.updateFruit(fruit);
public Integer getPageCount(String keyword) {
int count = fruitDAO.getFruitCount(keyword);
int pageCount = (count + 5 - 1) / 5;
applicationContext.xml
<bean id="fruitDAO" class="com.atguigu.fruit.dao.impl.FruitDAOImpl"/>
<bean id="fruitService" class="com.atguigu.fruit.service.impl.FruitServiceImpl">
<property name="fruitDAO" ref="fruitDAO"/>
<bean id="fruit" class="com.atguigu.fruit.controllers.FruitController">
<property name="fruitService" ref="fruitService"/>
