本帖最后由 qianqingfu 于 2010-06-02 13:56:27 编辑

解决方案 »

  1.   

    public SaleWebService(SaleService svrSale)
    {
       this.svrSale=svrSale;
    }new这个类的时候把参数传过来就行了
      

  2.   


    ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
    ctx.getBean("Bean Name");其中test.xml要放在class路径下,或者采用全路径。
    这样的代码跟tomcat就没关系了,不仅仅是web应用,即使app应用也没问题。
      

  3.   

    呵呵,谢谢楼上的回复,其实我开始是这么做的,但是,webservice客户端连接的时候,每连接一次,就会加载一次.xml文件。连接数据库的session数会一直增长,不能自行消退。
    所以我才想用tomcat启动,只加载一次Bean的方法。
      

  4.   

    你不是用Spring 了吗?? 为什么不把SaleService 托管给Spring 管理呢??
      

  5.   

    2楼的方法我也试了,但是还是报空指针。
    在ContextInit.java中加入new SaleWebService(svrSale);
    在SaleWebService.java中加入public SaleWebService(SaleService svrSale)
    {
      this.svrSale=svrSale;
    }
      

  6.   

    /**
     * 
     */
    package com.ysb.framework.listener;import javax.servlet.ServletContextEvent;import org.springframework.context.ApplicationContext;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.support.WebApplicationContextUtils; /**
     * @author allan
     * 
     */
    public class BeanUtil extends ContextLoaderListener { /**
     * spring上下文环境  
     */
    private static ApplicationContext applicationContext; public void contextInitialized(ServletContextEvent event) {
    super.contextInitialized(event);
    applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
    } public static Object getBean(String beanName) {
    return applicationContext.getBean(beanName);
    }
    }
    将spring加载配置的监听重写一下,重写内容也很简单,继承一下ContextLoaderListener ,然后创建一个spring的静态容器,需要用到spring容器里的bean时就从静态容器里面取就行了。web.xml中的spring的加载监听改为如下。
    <listener>
    <listener-class>
    com.ysb.framework.listener.BeanUtil
    </listener-class>
    </listener>
    楼主的加载方式是用的servlet,其实监听和servlet都是一样,目的就是在系统启动时执行那段加载代码。
      

  7.   

    你们用spring怎么用的这么辛苦啊,
    将SaleWebService和SaleService 配置成依赖关系
    然后由spring容器管理他们的初始化不行么?
    实在想不明白,还那么辛苦的去拿context全局对象。
    out了吧,也没什么创新之处啊,spring容器也是这么拿的
    别人弄好了的东西,你们硬是要去再写一次,累么?
      

  8.   

    不太清楚lz为什么不让Spring来管理SaleWebService???如果你实在不想在spring context中定义SaleWebService类型的bean,但又想用到spring中的SaleService bean,可以考虑使用@Configurable在Spring context中添加如下内容:<aop:spring-configured />
     
    <bean id="saleWebService" class="**.SaleWebService" abstract="true">
    <property name="svrSale" ref="SaleService" />
    </bean>修改类的定义@Configurable("saleWebService")
    public class SaleWebService {
    ... public SaleService getSvrSale() {
    return svrSale;
    } public void setSvrSale(SaleService svrSale) {
    this.svrSale = svrSale;
    }
    }
    不过,这样会用到AOP。
      

  9.   

    现在你就可以 new SaleWebService().saveSales(salesOrder);
      

  10.   

    10楼的仁兄,你说的切面技术在spring1.0中也适用么?
      

  11.   

    其实,这个贴子不是我的本意。
    当时,开发完了webservice之后,我采用static ApplicationContext basectx = new FileSystemXmlApplicationContext("file:E:/smartSaleWebservice/WebRoot/WEB-INF/applicationContext.xml");
    static SaleService svrSale=(SaleService)basectx.getBean("srvSale");
    的形式,获得Bean对象,但是webservice客户端连接之后,访问数据库完毕,数据库的session连接数降不下去。也就是说,每有一个客户端连一次,session会增长一些,慢慢Tomcat就爆掉了。
    我不知道各位有没碰到过这样的问题。
    事务的管理机制如下:<!-- 事务处理的AOP配置 -->
    <bean id="ProxyTemplate" abstract="true" lazy-init="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager"><ref local="myTransactionManager" /></property>
    <property name="transactionAttributes">
    <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="search*">PROPAGATION_REQUIRED</prop>
    <prop key="query*">PROPAGATION_REQUIRED</prop>
    <prop key="one*">PROPAGATION_REQUIRED</prop>
    <prop key="isExit*">PROPAGATION_REQUIRED</prop>
    <prop key="send*">PROPAGATION_REQUIRED</prop>
    <prop key="get*">PROPAGATION_REQUIRED</prop>
    <prop key="execute*">PROPAGATION_REQUIRED</prop>
    <prop key="add*">PROPAGATION_REQUIRED</prop>
    <prop key="del*">PROPAGATION_REQUIRED</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    <prop key="esave*">PROPAGATION_REQUIRED</prop>
    <prop key="E_*">PROPAGATION_REQUIRED</prop>
    <prop key="up*">PROPAGATION_REQUIRED</prop>
    <prop key="Eup*">PROPAGATION_REQUIRED</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="dis*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="part*">PROPAGATION_REQUIRED</prop>
    <prop key="eChange*">PROPAGATION_REQUIRED</prop>
    <prop key="eCacle*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>但是,相同的测试类在webservice服务端进行测试,访问数据库完毕,session数量会自动降下来。
    很郁闷!
      

  12.   

    强烈感谢#7楼的赐教,受益匪浅,重写这个可以在spring框架外获得spring中的实例。