java - How can I get a bean from the servlet context instead of the root context? -
i have spring-mvc application has autowired beans use of <context:component-scan base-package="com.example.app" />
within dispatcherservlet
context configuration.
i have situation wish access service bean non bean class, requestcontextawaretag
implementation.
i can access beans registered in root context follows:
applicationcontext ctx = webapplicationcontextutils.getwebapplicationcontext( pagecontext.getsession().getservletcontext()); myservice svc = ctx.getbean(myservice.class);
if bean registered in dispatcher context nosuchbeandefinitionexception
.
if it's possible prefer @service
beans registered in root context without @controller
beans being picked up, , have @controller
beans picked in dispatcher context. problem <context:component-scan/>
picks both.
if not possible need way access dispatchers applicationcontext
retrieve service beans.
any guidance appreciated.
i have managed solve splitting 2 component-scan
configurations use of exclude-filter
, include-filter
.
root-context:
<context:component-scan base-package="com.example.app"> <context:exclude-filter expression="org.springframework.stereotype.controller" type="annotation"/> </context:component-scan>
servlet-context:
<mvc:annotation-driven/> <context:component-scan base-package="com.example.app"> <context:include-filter expression="org.springframework.stereotype.controller" type="annotation"/> </context:component-scan>
Comments
Post a Comment