spring - @Service are constructed twice -


i have problem spring application @service classes being created twice when application starts. know problem configuration, i've experienced before, doing wrong?

is there fundamentally wrong way i've laid out config, below? (i have omitted deem irrelevant)

web.xml:

<servlet>     <servlet-name>myapp</servlet-name>     <servlet-class>org.springframework.web.servlet.dispatcherservlet</servlet-class>     <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping>     <servlet-name>myapp</servlet-name>     <url-pattern>/</url-pattern> </servlet-mapping>  <context-param>     <param-name>contextconfiglocation</param-name>     <param-value>         /web-inf/myapp-config.xml         /web-inf/myapp-security.xml         /web-inf/myapp-mvc.xml     </param-value> </context-param>  <listener>     <listener-class>com.myapp.servlet.myappcontextlistener</listener-class> </listener>  <listener>     <listener-class>org.springframework.web.context.contextloaderlistener</listener-class> </listener> 

myapp-servlet.xml

<context:component-scan base-package="com.myapp" annotation-config="true" /> <mvc:annotation-driven /> 

myapp-config.xml

<context:component-scan base-package="com.myapp" annotation-config="true" /> <context:annotation-config /> 

in addition @garyf's answer, there following beautiful solution problem (used in projects generated spring roo):

myapp-config.xml

<!-- load except @controllers --> <context:component-scan base-package="com.myapp">     <context:exclude-filter expression="org.springframework.stereotype.controller"         type="annotation"/> </context:component-scan> 

myapp-servlet.xml

<!-- load @controllers --> <context:component-scan base-package="com.myapp" use-default-filters="false">     <context:include-filter expression="org.springframework.stereotype.controller"          type="annotation"/> </context:component-scan> 

edit:

removing <context:component-scan> myapp-config.xml means, autodiscovered annotated beans registered in dispatcherservlet's context (that is, context loaded myapp-servlet.xml).

however recommended approach use servlet's context presentation-specific things (such controllers), , use root context (myapp-config.xml) core services of application. solution above make approach easy.

regarding practical considerations, when core services placed in servlet's application context, can't accessed outside scope of servlet, example, servlets (you may need use servlets implement access technologies) or filters (such spring security filters). that's reason having core services in root application context.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -