博主资料

留言 加为好友 收藏

用户名:  louis850324

个人统计

用户名: louis850324
等级: 初来乍到
威望: 255
积分: 510
在线时间: 15 小时
日志总数: 38
评论数量: 15
访问次数: 131447
建立时间: 2006-11-30
RSS订阅       手机访问

日历

2008 7.20 Sun
  12345
6789101112
13141516171819
20212223242526
2728293031  
«» 2008 - 7 «»

文章搜索

文章列表

最近访问的人:

炒股软件
2008-07-14 09:05:36
广州市易源服装专..
2008-07-02 17:33:41
风之力
2008-06-23 09:28:53
无忧在线 WuYouOn..
2008-06-23 09:18:11
Carl
2008-06-16 19:17:16
西行者~~
2008-05-11 02:16:07
MyBlog
2008-04-30 18:29:35
2008-04-25 11:47:26
Oracle数据库灾备..
2008-03-31 21:46:23
任我行不行
2008-03-08 03:17:54

最新评论

日志文章

2007年01月12日 16:30:07

Spring 依赖注射 控制反转

1 首先创建一个web项目

2 加入struts框架

3 加入spring框架

4 创建一个类:该类通过配置文件 applicationContext.xml 查找返回对应的类
public class SpringContextUtil {
     
     /**
      * constructor the bean from spring context
      * @param string
      * @return
      */
     public static Object getBean(ServletContext ctx,String beanName) {
           WebApplicationContext webCtx = WebApplicationContextUtils.getWebApplicationContext(ctx);
           return webCtx.getBean(beanName);
     }

}

5 applicationContext.xml里配置:

<beans>

     <bean id="baseManager" class="com.c3.dao.base.BaseManagerImpl">
       <property name="dao" ref="hibernateDao"/>
     </bean>

     <bean id="hibernateDao" class="com.c3.dao.impl.hibernate.HibernateDaoManager" />

</beans>

注释:baseManager 接口关键字, getBean()方法将通过baseManager找到对应的配置
    com.c3.dao.base.BaseManagerImpl 接口路径
    通过hibernateDao找到com.c3.dao.impl.hibernate.HibernateDaoManager类

6 业务类里调用:
BaseManager test = (BaseManager) SpringContextUtil.getBean(this.getServlet().getServletContext(), "baseManager");

注释:通过接口test调用到实现类

Tags: Spring   spring  

类别: Spring |  评论(0) |  浏览(2603) |  收藏
-20楼 [楼主][匿名] Says:
-21楼 [楼主][匿名] Says:
-22楼 [楼主][匿名] Says:
-23楼 [楼主][匿名] Says:
-24楼 [楼主][匿名] Says:
-25楼 [楼主][匿名] Says:
-26楼 [楼主][匿名] Says:
-27楼 [楼主][匿名] Says:
-28楼 [楼主][匿名] Says:
发表评论