¼ÓÈëÊÕ²Ø | ÉèΪÊ×Ò³ | »áÔ±ÖÐÐÄ | ÎÒҪͶ¸å Àî´óͬ £¨https://www.lidatong.com.cn/£©- ¿Æ¼¼¡¢½¨Õ¾¡¢¾­Ñé¡¢ÔƼÆËã¡¢5G¡¢´óÊý¾Ý,Õ¾³¤Íø!
µ±Ç°Î»Ö㺠Ê×Ò³ > ±à³Ì¿ª·¢ > Java > ÕýÎÄ

dz̸Springѧϰ֮request,sessionÓëglobalSession×÷ÓÃÓò

·¢²¼Ê±¼ä£º2020-12-14 22:28:55 ËùÊôÀ¸Ä¿£ºJava À´Ô´£ºÍøÂçÕûÀí
µ¼¶Á£ºÓëwebÈÝÆ÷ÓйصÄ×÷ÓÃÓò£¬Ê×ÏÈÒªÔÚWebÈÝÆ÷Àï½øÐÐһЩÅäÖᣠweb-app ... listener listener-class org.springframework.web.context.request.RequestContextListener /listener-class /listener .../web-app Request×÷ÓÃÓò ¿¼ÂÇÏÂÃæbean¶¨Ò壺 bean id="login

ÓëwebÈÝÆ÷ÓйصÄ×÷ÓÃÓò£¬Ê×ÏÈÒªÔÚWebÈÝÆ÷Àï½øÐÐһЩÅäÖá£

<web-app>
  ...
  <listener>
    <listener-class>
      org.springframework.web.context.request.RequestContextListener
    </listener-class>
  </listener>
  ...
</web-app>

Request×÷ÓÃÓò

¿¼ÂÇÏÂÃæbean¶¨Ò壺

<bean id="loginAction" class="com.foo.LoginAction" scope="request"/>

Õë¶Ôÿ´ÎHTTPÇëÇó£¬SpringÈÝÆ÷»á¸ù¾ÝloginAction bean¶¨Òå´´½¨Ò»¸öȫеÄLoginAction beanʵÀý£¬ ÇÒ¸ÃloginAction beanʵÀý½öÔÚµ±Ç°HTTP requestÄÚÓÐЧ£¬Òò´Ë¿ÉÒÔ¸ù¾ÝÐèÒª·ÅÐĵĸü¸ÄËù½¨ÊµÀýµÄÄÚ²¿×´Ì¬£¬ ¶øÆäËûÇëÇóÖиù¾ÝloginAction bean¶¨Òå´´½¨µÄʵÀý£¬½«²»»á¿´µ½ÕâЩÌض¨ÓÚij¸öÇëÇóµÄ״̬±ä»¯¡£ µ±´¦ÀíÇëÇó½áÊø£¬request×÷ÓÃÓòµÄbeanʵÀý½«±»Ïú»Ù¡£

Session×÷ÓÃÓò

¿¼ÂÇÏÂÃæbean¶¨Ò壺

<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

Õë¶Ôij¸öHTTP Session£¬SpringÈÝÆ÷»á¸ù¾ÝuserPreferences bean¶¨Òå´´½¨Ò»¸öȫеÄuserPreferences beanʵÀý£¬ ÇÒ¸ÃuserPreferences bean½öÔÚµ±Ç°HTTP SessionÄÚÓÐЧ¡£ Óërequest×÷ÓÃÓòÒ»Ñù£¬Äã¿ÉÒÔ¸ù¾ÝÐèÒª·ÅÐĵĸü¸ÄËù´´½¨ÊµÀýµÄÄÚ²¿×´Ì¬£¬¶ø±ðµÄHTTP SessionÖиù¾ÝuserPreferences´´½¨µÄʵÀý£¬ ½«²»»á¿´µ½ÕâЩÌض¨ÓÚij¸öHTTP SessionµÄ״̬±ä»¯¡£ µ±HTTP Session×îÖÕ±»·ÏÆúµÄʱºò£¬ÔÚ¸ÃHTTP Session×÷ÓÃÓòÄÚµÄbeanÒ²»á±»·ÏÆúµô¡£

global session×÷ÓÃÓò

¿¼ÂÇÏÂÃæbean¶¨Ò壺

<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>

global session×÷ÓÃÓòÀàËÆÓÚ±ê×¼µÄHTTP Session×÷ÓÃÓò£¬²»¹ýËü½ö½öÔÚ»ùÓÚportletµÄwebÓ¦ÓÃÖвÅÓÐÒâÒå¡£Portlet¹æ·¶¶¨ÒåÁËÈ«¾ÖSessionµÄ¸ÅÄËü±»ËùÓй¹³Éij¸öportlet webÓ¦Óõĸ÷ÖÖ²»Í¬µÄportletËù¹²Ïí¡£ÔÚglobal session×÷ÓÃÓòÖж¨ÒåµÄbean±»ÏÞ¶¨ÓÚÈ«¾Öportlet SessionµÄÉúÃüÖÜÆÚ·¶Î§ÄÚ¡£

Çë×¢Ò⣬¼ÙÈçÄãÔÚ±àдһ¸ö±ê×¼µÄ»ùÓÚServletµÄwebÓ¦Ó㬲¢ÇÒ¶¨ÒåÁËÒ»¸ö»ò¶à¸ö¾ßÓÐglobal session×÷ÓÃÓòµÄbean£¬ÏµÍ³»áʹÓñê×¼µÄHTTP Session×÷ÓÃÓò£¬²¢ÇÒ²»»áÒýÆðÈκδíÎó¡£

×÷ÓÃÓòÒÀÀµÎÊÌâ

If you want to inject (for example) an HTTP request scoped bean into another bean of a longer-lived scope,you may choose to inject an AOP proxy in place of the scoped bean. That is,you need to inject a proxy object that exposes the same public interface as the scoped object but that can also retrieve the real target object from the relevant scope (such as an HTTP request) and delegate method calls onto the real object.

<?xml version="1.0" encoding="UTF-8"?>
<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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">

  <!-- an HTTP Session-scoped bean exposed as a proxy -->
  <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
    <!-- instructs the container to proxy the surrounding bean -->
    <aop:scoped-proxy/>
  </bean>

  <!-- a singleton-scoped bean injected with a proxy to the above bean -->
  <bean id="userService" class="com.foo.SimpleUserService">
    <!-- a reference to the proxied userPreferences bean -->
    <property name="userPreferences" ref="userPreferences"/>
  </bean>
</beans>

×ܽá

ÒÔÉϾÍÊDZ¾ÎĹØÓÚSpringѧϰ֮request,sessionÓëglobalSession×÷ÓÃÓòµÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒÓÐËù°ïÖú¡£¸ü¶àÄÚÈÝÇë²Î¿¼£ºSpring spel±í´ïʽʹÓ÷½·¨Ê¾Àý¡¢JavaÖ®Spring×¢½âÅäÖÃbeanʵÀý´úÂë½âÎö¡¢javaÖÐjavaBeanÓëBeanµÄÉîÈëÀí½âÒÔ¼°±¾Õ¾ÆäËûÀ¸Ä¿£¬¸Ðл´ó¼Ò¶Ô±¾Õ¾µÄÖ§³Ö£¡

£¨±à¼­£ºÀî´óͬ£©

¡¾ÉùÃ÷¡¿±¾Õ¾ÄÚÈݾùÀ´×ÔÍøÂ磬ÆäÏà¹ØÑÔÂÛ½ö´ú±í×÷Õ߸öÈ˹۵㣬²»´ú±í±¾Õ¾Á¢³¡¡£ÈôÎÞÒâÇÖ·¸µ½ÄúµÄȨÀû£¬Ç뼰ʱÓëÁªÏµÕ¾³¤É¾³ýÏà¹ØÄÚÈÝ!

    ÍƼöÎÄÕÂ
      ÈȵãÔĶÁ