Spring (一) IOC ( Inversion Of Control )
package com.bjpowernode.drp.service;import com.bjpowernode.drp.AppException;import com.bjpowernode.drp.BeanFactory;import com.bjpowernode.drp.DBUtil;import com.bjpowernode.drp.PageModel;import com.bjpowernode.drp.dao.ItemDao;import com.bjpowernode.drp.domain.Item;/** * IOC * @author Administrator * */public class ItemServiceImpl implements ItemService {public void addItem(Item item) {try {itemDao.addItem(item);} catch (Exception e) {e.printStackTrace();throw new AppException("添加物料失败!");}}private ItemDao getItemDao() {ItemDao itemDao = (ItemDao)BeanFactory.getInstance().getBean(ItemDao.class);return itemDao;}}
package com.bjpowernode.drp.service;import com.bjpowernode.drp.AppException;import com.bjpowernode.drp.PageModel;import com.bjpowernode.drp.dao.ItemDao;import com.bjpowernode.drp.domain.Item;/** * IOC * @author Administrator * */public class ItemServiceImpl implements ItemService {private ItemDao itemDao;public void setItemDao(ItemDao itemDao) {this.itemDao = itemDao;}public void addItem(Item item) {try {itemDao.addItem(item);} catch (Exception e) {e.printStackTrace();throw new AppException("添加物料失败!");}}}
<?xml version="1.0" encoding="UTF-8"?><!-- - Application context definition for JPetStore's business layer. - Contains bean references to the transaction manager and to the DAOs in - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation"). --><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"><!-- 配置Service里面的itemDao,dataDictService --><bean id="itemService" class="com.bjpowernode.drp.service.ItemServiceImpl"><property name="itemDao" ref="itemDao"></property></bean><bean id="dataDictService" class="com.bjpowernode.drp.service.DataDictServiceImpl"><property name="dataDictDao" ref="dataDictDao"></property></bean></beans>