您的位置:首页网页设计经验技巧 → Spring3注释装配的实践方案

Spring3注释装配的实践方案

时间:2011/2/11 11:46:08来源:本站原创作者:佚名我要评论(0)

5 页 Java代码

为了解决此问题,我们实现了一个后置注入的类:SqlMapClientDaoInjector用来在DAO加载到context中后注入其依赖。类代码如下:

 Java代码
/** 
 * 类说明: 向iBatis的DAO中注入依赖<br> 
 * 创建时间: 2011-1-26 上午10:51:28<br> 
 *  
 * @author seraph<br> 
 * @email: seraph115@gmail.com<br> 
 */ 
public class SqlMapClientDaoInjector implements ApplicationContextAware, InitializingBean {  
 
    private static final Logger logger = Logger.getLogger(SqlMapClientDaoInjector.class);  
      
    @Override 
    public void setApplicationContext(ApplicationContext applicationContext)  
            throws BeansException {  
        SpringContext.setApplicationContext(applicationContext);  
    }  
 
    public void afterPropertiesSet() throws Exception {  
        Assert.notNull(dataSource, "Property 'dataSource' is required.");  
        Assert.notNull(sqlMapClient, "Property 'sqlMapClient' is required.");  
        injectDependence();  
    }  
      
    private void injectDependence() {  
                // 获取Context上下文  
        ApplicationContext ctx = SpringContext.getApplicationContext();  
                // 按类型获取上下文中的对象  
                Map<String, SqlMapClientDaoSupport> map = ctx.getBeansOfType(org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.class, true, true);  
        for (Iterator<String> i = map.keySet().iterator(); i.hasNext();) {  
            try {  
                String supportName = (String) i.next();  
                SqlMapClientDaoSupport support = map.get(supportName);  
                // 后注入依赖  
                support.setSqlMapClient(sqlMapClient);  
                support.setDataSource(dataSource);  
            } catch (RuntimeException e) {  
                logger.error("SqlMapClientDaoInjector.injectDependence()", e);   
            }  
        }  
    }  
      
    public void setDataSource(DataSource dataSource) {  
        this.dataSource = dataSource;  
    }  
 
    public void setSqlMapClient(SqlMapClient sqlMapClient) {  
        this.sqlMapClient = sqlMapClient;  
    }  
      
    private DataSource dataSource;  
 
    private SqlMapClient sqlMapClient;  
 

/**
 * 类说明: 向iBatis的DAO中注入依赖<br>
 * 创建时间: 2011-1-26 上午10:51:28<br>
 *
 * @author seraph<br>
 * @email: seraph115@gmail.com<br>
 */
public class SqlMapClientDaoInjector implements ApplicationContextAware, InitializingBean {

 private static final Logger logger = Logger.getLogger(SqlMapClientDaoInjector.class);
 
 @Override
 public void setApplicationContext(ApplicationContext applicationContext)
   throws BeansException {
  SpringContext.setApplicationContext(applicationContext);
 }

 public void afterPropertiesSet() throws Exception {
  Assert.notNull(dataSource, "Property 'dataSource' is required.");
  Assert.notNull(sqlMapClient, "Property 'sqlMapClient' is required.");
  injectDependence();
 }
 
 private void injectDependence() {
                // 获取Context上下文
  ApplicationContext ctx = SpringContext.getApplicationContext();
                // 按类型获取上下文中的对象
                Map<String, SqlMapClientDaoSupport> map = ctx.getBeansOfType(org.springframework.orm.ibatis.support.SqlMapClientDaoSupport.class, true, true);
  for (Iterator<String> i = map.keySet().iterator(); i.hasNext();) {
   try {
    String supportName = (String) i.next();
    SqlMapClientDaoSupport support = map.get(supportName);
    // 后注入依赖
    support.setSqlMapClient(sqlMapClient);
    support.setDataSource(dataSource);
   } catch (RuntimeException e) {
    logger.error("SqlMapClientDaoInjector.injectDependence()", e);
     }
  }
 }
 
 public void setDataSource(DataSource dataSource) {
  this.dataSource = dataSource;
 }

 public void setSqlMapClient(SqlMapClient sqlMapClient) {
  this.sqlMapClient = sqlMapClient;
 }
 
 private DataSource dataSource;

 private SqlMapClient sqlMapClient;

}

 然后加入此类的配置即可,

相关视频

    没有数据

相关阅读 Springtomize3支持8.3吗 springtomize3 ios8.3能用吗Springtomize3教程 Springtomize3怎么用spring forword!苹果3月9日春季发布会iphone隐藏桌面分页点方法RespringCacheFix清理iphone注销后产生的垃圾文件springtomize重新自定义的系统设置及功能增强springtomize调色温的问题springtomize2一秒钟变iphone5

文章评论
发表评论

热门文章 Wordpress本地安装教程dx1.5如何设置二级域名

最新文章 hbuilder有哪些快捷键Wordpress本地安装教程 Wordpress本地安装教程expression web 4文档乱码解决方法dz 2.5“收藏本版”关闭小对话框无法关闭解在制作安装软件之前,您必须先将易语言存盘,

人气排行 如何使用multipart/form-data格式上传文件Photoshop PS图层混合模式详解(非常详细)ISAPI_Rewrite3使用教程网站里添加收藏和设为首页代码桌面快捷方式图标不见了C#获取执行程序所在的当前路径的方法详解(XMLHttpRequest)进行跨域请求方法如何用远程桌面连接进行传输文件