1
我試圖用@Scope("Request")
來使用Spring Scoped Bean。根據docs,如果使用ServletDispatcher,則不需要其他設置。Bean Scope(「Request」)不起作用
儘管我將一些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>
你可以發佈'applicationContext.xml'嗎? – 2013-02-18 15:44:44
如何將'StatusService'定義爲一個bean?使用註釋或XML或'@ Configuration'? – axtavt 2013-02-18 16:01:02
StatusService在applicationContext中定義並連接到控制器 – special0ne 2013-02-18 17:03:56