您可以在这里快速查找:


 
您的位置: 编程学习 > java教程 > 200601
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

read appfuse-初始化

dudutu

1.  初始化配置信息:Appfuse利用监听器,在应用程序启动时,初始化配置信息

★ 应用:web.xml

   <listener>

        <listener-class>org.dudu.webapp.listener.StartupListener</listener-class>

      </listener>

★ StartupListener 监听器:继承于Spring的 ContextLoaderListener和Sevelet的

ServletContextListener,配置于Web.xml中。

 

 

 

 

★ 说明:凡是继承ServletContextListener接口的类,在web容器启动时调用其

      contextInitialized方法进行初始化,当容器关闭时调用其contextDestroyed的方

法进行销毁,我们可以利用这个特性初始化一些信息。

    ★ 程序说明:(主要代码)

        public class StartupListener extends ContextLoaderListener

        implements ServletContextListener {

            public void contextInitialized(ServletContextEvent event) {

            //容器启动时,自动调用contextInitialized函数。

            //取得全局Application Scope 环境上下文

            ServletContext context = event.getServletContext();

            Map config = (HashMap) context.getAttribute(Constants.CONFIG);

 

 

 

 

            //将DAO_TYPE_HIBERNATE = "hibernate"写入ServletContext

config.put(Constants.DAO_TYPE, daoType);

            context.setAttribute(Constants.CONFIG, config);

           

            //调用setupContext(context)函数初始化Spring

            public static void setupContext(ServletContext context) {

            //初始化Spring环境上下文,为getBean作准备,并且将ServletContext

             //交给Spring管理

            ApplicationContext ctx =

            WebApplicationContextUtils.getRequiredWebApplicationContext(context);

            // LookupManager为接口类,提供统一的调用接口 

            //通过Ico反射注入,查找lookupManager和Dao属性。(1)

           LookupManager mgr = (LookupManager) ctx.getBean("lookupManager");

            //取得角色信息,存入ServletContext scope中。

            context.setAttribute(Constants.AVAILABLE_ROLES, mgr.getAllRoles());

    }

(1)的说明:在配置文件中取值,动态加载LookupManagerImpl类

    客户面对接口LookupManager,底层实现为LookupManagerImpl,Spring的一大

                特点:自然的面向接口编程。LookupManagerImpl是POJO的管理类,真正的

    数据操作委派给它的引用LookupDAO的具体实现类LookupDAOHibernate

Spring配置文件 applicationContext-service.xml

<bean id="lookupManager" class="org.dudu.service.impl.LookupManagerImpl">

                 <property name="lookupDAO"><ref bean="lookupDAO"/></property>(2)

 </bean>

(2)的说明:通过Ico动态加载对象属性LookupDAOHibernate

       Spring配置文件:applicationContext-hibernate.xml

        <!-- LookupDAO: Hibernate implementation -->

         <bean id="lookupDAO" class="org.dudu.dao.hibernate.LookupDAOHibernate">

        <property name="sessionFactory"><ref local="sessionFactory"/></property>

 </bean>

    public class LookupManagerImpl extends BaseManager implements LookupManager {

    private LookupDAO dao;

    public void setLookupDAO(LookupDAO dao) {

//Spring动态注入dao引用,从配置文件中取得具体的lookupDAO,又一次的面向//接口编程

             this.dao = dao;

         }

    public List getAllRoles() {

        // LookupManagerImpl将真正的数据操作委派个LookupDAOHibernate(3)

             List roles = dao.getRoles();

             List list = new ArrayList();

             Role role = null;

        for (int i = 0; i < roles.size(); i++) {

                 role = (Role) roles.get(i);

                 list.add(new LabelValue(role.getName(), role.getName()));

             }

        return list;

         }

}

(3)      的说明:LookupDAOHibernate类