Spring之IOC

来源:互联网 发布:金山打字通mac版官网 编辑:程序博客网 时间:2024/05/23 01:26

我们在编写程序的时候经常会用到new关键字,尤其是在为了实现高内聚低耦合我们一般在编程程序的时候分为三层,dao层主要是实体与数据库的表进行对应而service业务逻辑层主要是调用的dao层的实体完成业务逻辑而view表示层主要是为了展现service产生的结果。分层之后就会出现我们需要很多的new,层与层之间的相互调用就很多,有时候我们会改变两个类的调用关系,Spring中的IOC就可以让我们在配置文件中来实现调用关系(依赖关系的配置),让我们无需用new就可以调用一个类的实体。
IOC的实验代码如下:
dao层的接口如下:

package org.liky.ssh.dao;import java.util.List;public interface IDAO<K, V> {    public void doCreate(V vo) throws Exception;    public void doUpdate(V vo) throws Exception;    public void doRemove(K id) throws Exception;    public List<V> findAll() throws Exception;    public V findById(K id) throws Exception;    public List<V> findAll(int pageNo, int pageSize, String keyword,            String column) throws Exception;    public int getAllCount(String keyword, String column) throws Exception;}
package org.liky.ssh.dao;import org.liky.ssh.pojo.NewsType;public interface INewsTypeDAO extends IDAO<Integer, NewsType> {}
package org.liky.ssh.dao;import org.liky.ssh.pojo.News;public interface INewsDAO extends IDAO<Integer, News> {}

接口的实现类如下:

package org.liky.ssh.dao.impl;import java.util.List;import org.hibernate.criterion.DetachedCriteria;import org.hibernate.criterion.Restrictions;import org.liky.ssh.dao.INewsDAO;import org.liky.ssh.pojo.News;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class NewsDAOImpl extends HibernateDaoSupport implements INewsDAO {    public void doCreate(News vo) throws Exception {        super.getHibernateTemplate().save(vo);    }    public void doRemove(Integer id) throws Exception {        super.getHibernateTemplate().delete(findById(id));    }    public void doUpdate(News vo) throws Exception {        super.getHibernateTemplate().update(vo);    }    public List<News> findAll() throws Exception {        return getHibernateTemplate().loadAll(News.class);    }    public List<News> findAll(final int pageNo, final int pageSize,            final String keyword, final String column) throws Exception {        // Spring没有提供针对HQL方式的分页查询方法        // 1、使用Criteria来完成分页查询        DetachedCriteria c = DetachedCriteria.forClass(News.class);        // 加入条件        c.add(Restrictions.like(column, "%" + keyword + "%"));        List all = super.getHibernateTemplate().findByCriteria(c,                (pageNo - 1) * pageSize, pageSize);        // 2、自行扩展Spring功能,添加分页查询方法,使用的方式为匿名内部类        // List all = super.getHibernateTemplate().executeFind(        // new HibernateCallback() {        // public Object doInHibernate(Session session)        // throws HibernateException, SQLException {        // String hql = "FROM News AS n WHERE n." + column        // + " LIKE ?";        // Query query = session.createQuery(hql);        // query.setString(0, "%" + keyword + "%");        // query.setFirstResult((pageNo - 1) * pageSize);        // query.setMaxResults(pageSize);        //        // return query.list();        // }        // });        return all;    }    public News findById(Integer id) throws Exception {        return super.getHibernateTemplate().get(News.class, id);    }    public int getAllCount(String keyword, String column) throws Exception {        String hql = "SELECT COUNT(n) FROM News AS n WHERE n." + column                + " LIKE ?";        List all = super.getHibernateTemplate().find(hql, "%" + keyword + "%");        return ((Long) all.get(0)).intValue();    }}
package org.liky.ssh.dao.impl;import java.util.List;import org.liky.ssh.dao.INewsTypeDAO;import org.liky.ssh.pojo.NewsType;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;public class NewsTypeDAOImpl extends HibernateDaoSupport implements        INewsTypeDAO {    public void doCreate(NewsType vo) throws Exception {    }    public void doRemove(Integer id) throws Exception {    }    public void doUpdate(NewsType vo) throws Exception {    }    public List<NewsType> findAll() throws Exception {        return super.getHibernateTemplate().loadAll(NewsType.class);    }    public List<NewsType> findAll(int pageNo, int pageSize, String keyword,            String column) throws Exception {        // TODO Auto-generated method stub        return null;    }    public NewsType findById(Integer id) throws Exception {        // TODO Auto-generated method stub        return null;    }    public int getAllCount(String keyword, String column) throws Exception {        // TODO Auto-generated method stub        return 0;    }}

业务逻辑层是怎样调用dao层的实现类?
业务逻辑层接口代码如下:

package org.liky.ssh.back.service;import java.util.List;import java.util.Map;import org.liky.ssh.pojo.News;import org.liky.ssh.pojo.NewsType;public interface INewsService {    public List<NewsType> insertPre() throws Exception;    public void insert(News news) throws Exception;    public void update(News news) throws Exception;    public void delete(int id) throws Exception;    public Map<String, Object> updatePre(int id) throws Exception;    public Map<String, Object> list(int pageNo, int pageSize, String column,            String keyword) throws Exception;}

业务逻辑层接口的实现代码如下:

package org.liky.ssh.back.service.impl;import java.util.HashMap;import java.util.List;import java.util.Map;import org.liky.ssh.back.service.INewsService;import org.liky.ssh.dao.INewsDAO;import org.liky.ssh.dao.INewsTypeDAO;import org.liky.ssh.pojo.News;import org.liky.ssh.pojo.NewsType;public class NewsServiceImpl implements INewsService {    private INewsDAO newsdao;    private INewsTypeDAO typedao;    public void delete(int id) throws Exception {        newsdao.doRemove(id);    }    public Map<String, Object> updatePre(int id) throws Exception {        Map<String, Object> map = new HashMap<String, Object>();        map.put("allType", typedao.findAll());        map.put("news", newsdao.findById(id));        return map;    }    public void insert(News news) throws Exception {        newsdao.doCreate(news);    }    public void update(News news) throws Exception {        newsdao.doUpdate(news);    }    public Map<String, Object> list(int pageNo, int pageSize, String column,            String keyword) throws Exception {        Map<String, Object> map = new HashMap<String, Object>();        map.put("allNews", newsdao.findAll(pageNo, pageSize, keyword, column));        map.put("allCount", newsdao.getAllCount(keyword, column));        return map;    }    public void setNewsdao(INewsDAO newsdao) {        this.newsdao = newsdao;    }    public List<NewsType> insertPre() throws Exception {        return typedao.findAll();    }    public void setTypedao(INewsTypeDAO typedao) {        this.typedao = typedao;    }}
private INewsDAO newsdao;private INewsTypeDAO typedao;

上面的两行代码就是业务逻辑层调用dao层的类,在配置文件中的配置如下:
两个dao层的实现类的配置

<bean id="newsDAOImpl" class="org.liky.ssh.dao.impl.NewsDAOImpl">        <property name="hibernateTemplate">            <ref bean="hibernateTemplate" />        </property>    </bean>    <bean id="newsTypeDAOImpl" class="org.liky.ssh.dao.impl.NewsTypeDAOImpl">        <property name="hibernateTemplate">            <ref bean="hibernateTemplate" />        </property>    </bean>

service中的调用dao层的两个类的配置如下:

    <bean id="newsServiceImpl" class="org.liky.ssh.back.service.impl.NewsServiceImpl">        <property name="newsdao">            <ref bean="newsDAOImpl" />        </property>        <property name="typedao">            <ref bean="newsTypeDAOImpl" />        </property>    </bean>

通过以上配置我们就使用了Spring的ioc。

0 0
原创粉丝点击