2013-02-18 3 views
1

Я пытаюсь использовать весенние бобы с @Scope("Request"). Согласно docs, если вы используете ServletDispatcher, никакая другая настройка не требуется.Bean Scope («Запрос») не работает

Несмотря на то, что я пометил некоторые из bean как @Scope("Request"), я все равно получаю для каждого запроса те же объекты bean, что означает, что я получаю поведение по умолчанию Singleton.

В приведенном ниже коде я стараюсь, чтобы statusService имел запрос области.

код контроллера:

@Controller 
public class CrawlerController { 
    private final Log log = LogFactory.getLog(getClass()); 

    @Autowired 
    private ScrappingService scrappingService; 
    @Autowired 
    private RepositoryService repositoryService; 
    @Autowired 
    private StatusService statusService; 


    @RequestMapping(value = "/test", method = RequestMethod.GET) 
    public String index(HttpServletRequest request) { 
     return "candleAndVolume"; 
    } 

    @RequestMapping(value = "/status", method = RequestMethod.GET) 
    @ResponseBody 
    public String getStatus(HttpServletRequest request) { 
     return statusService.getStatus(); 
    } 

Статус обслуживания:

@Scope (value = WebApplicationContext.SCOPE_REQUEST) 
public class StatusService { 
    String status; 

    public String getStatus(){ 
     return status; 
    } 
    public void setStatus(String status){ 
     this.status = status; 
    } 
} 

web.xml - Только в случае, если это необходимо.

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 
     xmlns="http://java.sun.com/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
     metadata-complete="false"> 

    <servlet> 
     <servlet-name>spring</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>spring</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

    <filter> 
     <filter-name>hibernateFilter</filter-name> 
     <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> 

     <init-param> 
      <param-name>sessionFactoryBeanName</param-name> 
      <param-value>sessionFactory</param-value> 
     </init-param> 
    </filter> 

    <filter-mapping> 
     <filter-name>hibernateFilter</filter-name> 
     <url-pattern>/*</url-pattern> 
    </filter-mapping> 

    <!-- listener to load the root application context --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>health</servlet-name> 
     <jsp-file>/health.jsp</jsp-file> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>health</servlet-name> 
     <url-pattern>/health</url-pattern> 
    </servlet-mapping> 

</web-app> 

Контекст Применение:

<?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:util="http://www.springframework.org/schema/util" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:p="http://www.springframework.org/schema/p" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:annotation-config/> 

    <bean name="scrapper" class="net.crawler.service.scrap.DefaultScrapperImpl"/> 

    <bean name="statusService" class="net.crawler.service.StatusService"/> 

    <bean name="scrappingService" class="net.crawler.service.scrap.ScrappingService"/> 

    <bean id="propertyConfigurer" 
      class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <list> 
       <value>/WEB-INF/jdbc.properties</value> 
       <value>/WEB-INF/app.properties</value> 
      </list> 
     </property> 
     <property name="ignoreUnresolvablePlaceholders" value="true"/> 
    </bean> 


    <bean id="dataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" 
      p:driverClassName="${jdbc.driverClassName}" 
      p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" 
      p:password="${jdbc.password}"/> 


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="configLocation"> 
      <value>classpath:hibernate.cfg.xml</value> 
     </property> 
     <property name="configurationClass"> 
      <value>org.hibernate.cfg.AnnotationConfiguration</value> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.hbm2ddl.auto">update</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" 
      class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <bean name="gmailDefaultSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> 
     <property name="host" value="${app.mail.host}"/> 
     <property name="port" value="${app.mail.port}"/> 
     <property name="username" value="${app.mail.userName}"/> 
     <property name="password" value="${app.mail.password}"/> 

     <property name="javaMailProperties"> 
      <props> 
       <prop key="mail.smtp.auth">true</prop> 
       <prop key="mail.smtp.starttls.enable">true</prop> 
      </props> 
     </property> 

    </bean> 

    <bean name="pendingPages" class="net.crawler.general.PendingPages"/> 

    <bean id="utils" class="net.crawler.general.Utils"> 
     <property name="exporterMap"> 
      <map> 
       <entry key="CrawlerExporter" value-ref="crawlerExporter"/> 
       <entry key="CsvMailExporter" value-ref="csvMailExporter"/> 
       <entry key="SendCustomMailExporter" value-ref="sendCustomMailExporter"/> 
      </map> 
     </property> 
    </bean> 

    <bean name="crawlerExporter" class="net.crawler.service.export.CrawlerExporter"/> 

    <bean name="csvMailExporter" class="net.crawler.service.export.CsvMailExporter"> 
     <property name="sendTo" value="${app.mail.recipient}"/> 
     <property name="sender" ref="gmailDefaultSender"/> 
    </bean> 

    <bean name="sendCustomMailExporter" class="net.crawler.service.export.SendCustomMailExporter"> 
     <property name="sendTo" value="${app.mail.recipient}"/> 
     <property name="sender" ref="gmailDefaultSender"/> 
    </bean> 

</beans> 
+0

Вы также можете написать 'applicationContext.xml'? –

+0

Как вы определяете 'StatusService' как bean-компонент? Использование аннотаций или XML или '@ Configuration'? – axtavt

+0

StatusService определен в applicationContext и подключен к контроллеру – special0ne

ответ

2

Вам необходимо включить

<aop:scoped-proxy/> 

так, что вы получите прокси вместо боб непосредственно. Ознакомьтесь с разделом scoped beans as dependencies в документации.

Смежные вопросы