新建一个listener,继承自ServletContextListener
注意这个listener必须放在spring的那个ContextLoaderListener的后面,不能用servlet那个配置然后再listener中加载webApplicationContext,如下
private YourService service;
@Override
public void contextDestroyed(ServletContextEvent arg0) { } @Override
public void contextInitialized(ServletContextEvent arg0) {
ServletContext context = arg0.getServletContext();
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
this.service = (YourService)ctx.getBean("yourService");
//this.service.方法

解决方案 »

  1.   

    <?xml version="1.0" encoding="UTF-8"?><!--
    - Application context definition for JPetStore's business layer.
    - Contains bean references to the transaction manager and to the DAOs in
    - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
    -->
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
               http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
        
        <!-- 通过hibernate.cfg.xml配置SessionFactory -->   
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
          <property name="configLocation">
           <value>classpath:hibernate.cfg.xml</value>
          </property>
        </bean>
        
        <!-- 配置事务管理器 -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
           <property name="sessionFactory">
           <!-- 引用 配置SessionFactory的ID-->
             <ref bean="sessionFactory"/>
           </property>
        </bean>
       
        <!-- 配置事务的传播性 -->
        <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!-- 哪些方法有 事务的传播性-->
          <tx:attributes>
          <!-- 以add开始的方法 REQUIRED表示就用事务,没有就开启事务-->
             <tx:method name="add*" propagation="REQUIRED"/>
             <tx:method name="insert*" propagation="REQUIRED"/>
             <tx:method name="select*" propagation="REQUIRED"/>
             <tx:method name="update*" propagation="REQUIRED"/>
             <!-- 除了上面的方法外,其余的方法都是只读 -->
             <tx:method name="*" read-only="true"/>
          </tx:attributes>
        </tx:advice>
        
        <!-- 哪些类的方法参与了事务 -->
        <aop:config>
        <!-- execution(* com.east.spring.managerimpl.*.*(..))这个类的所有方法都用事务 -->
         <aop:pointcut id="allManagerMethod" expression="execution(* com.east.ssh.dao.impl.*.*(..))"/>
         <!-- 引用 pointcut 和 advice-->
            <aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
        </aop:config>
    </beans>
      

  2.   

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
      
       <bean id="userDaoImpl" class="com.east.ssh.dao.impl.UserDaoImpl">
     <property name="sessionFactory">
                 <ref bean="sessionFactory" />
             </property>
    </bean>

    <bean id="ordersDaoImpl" class="com.east.ssh.dao.impl.OrdersDaoImpl">
    <property name="sessionFactory">
       <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="userBiz" class="com.east.ssh.biz.UserBiz">
      <property name="userDao" ref="userDaoImpl"/>
    </bean>

    <bean id="ordersBiz" class="com.east.ssh.biz.OrdersBiz">
       <property name="ordersDao" ref="ordersDaoImpl"/>
    </bean>
    </beans>
    --------------------------
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
        
    <bean name="/login" class="com.east.ssh.web.action.UserMappingDispatchAction">
       <property name="userBiz" ref="userBiz"/>
    </bean>

    <bean name="/regist" class="com.east.ssh.web.action.UserMappingDispatchAction">
        <property name="userBiz" ref="userBiz"/>
    </bean>

    <bean name="/selectOrders" class="com.east.ssh.web.action.OrdersMappingDispatchAction">
      <property name="ordersBiz" ref="ordersBiz"/>
    </bean>
    </beans>
      

  3.   


     public class SerialListener extends HttpServlet implements
    ServletContextListener {
    private String date;
      public void contextInitialized(ServletContextEvent sce) {
      ServletContext context = sce.getServletContext();
            ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(context);
           DateCacheFactory  cache= (DateCacheFactory )ctx.getBean("datecachefactory");
        date=cache.getDelegate();//赋值给date
         
    }   public void contextDestroyed(ServletContextEvent sce) {
     
    }}
    //把数据放内存里面import java.util.HashMap;public final class DateCacheFactory {
    //set注入
     private xxDao dao;
           public xxDao getXxDao()
    {
        return dao;
    } public void setXxDao(xxdao dao)
    {
        this.dao=dao;
    } public static HashMap<String, String> map = new HashMap<String, String>();
     //如果你是一个集合的话你就换返回类型 HashMap 也换掉 换成HashMap<String, list>
      public synchronized String getDelegate() {
     //只会查询一次 第一次 判断 这个是 空 执行下面的查询  第2次进来的话 cache 里面有 数据了就不 需要在查询了
    // 如果 不理解的话 可以加 我 QQ26332025  
      String de = null; if (map.get("cache") != null) {
      de = map.get("cache");
      return de;
      }

     try {
     
    de=dao.select();//查询数据里面的内容
      map.put("cache", de);//放到类存里面简单的享元模式
      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      // TODO Auto-generated method stub
      return de;
      }
     
    }/***
    *spring 
    *<bean id="datecachefactory" class="DateCacheFactory>
    *<property name="dao">
    * <ref local="xxdao" />
    * </property></bean>
    *
    *<bean id="xxdao" class="xxDao">
    *<property name="这里你会写了吧">
    * <ref local="这里你会写了吧" />
    *</property></bean>
    *
    */
      

  4.   

    如果是Struts2的话,在web.xml下加上
    <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    然后在把struts.xml中的Action的类注入到spring的IOC容器中,然后struts的action在引用刚刚注入的spring的IOC中的那个就可以实现 了,
    在一个就是别忘了引入相关的jar包
      

  5.   

    使用cache做了一下感觉不错 呵呵 学到很多东西,谢谢各位~~