我正在尝试将 Spring Scoped Beans 与@Scope("Request")
. 根据文档,如果使用 ServletDispatcher,则不需要其他设置。
即使我标记了一些 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>