- Why is the ServletContextListener called when all Servlets are instantiated?
You probably misundertood by the following webservice deployer output:
12:13:26,227 INFO [org.jboss.wsf.stack.cxf.metadata.MetadataBuilder] (MSC service thread 1-2) Add Service
id=HelloServlet
address=http://localhost:8080/helloServlet/HelloServlet
implementor=servlet.hello.helloservlet.HelloServletImpl
invoker=org.jboss.wsf.stack.cxf.InvokerJSE
serviceName={http://helloservlet.hello.servlet/}HelloServletImplService
portName={http://helloservlet.hello.servlet/}HelloServletImplPort
wsdlLocation=null
mtomEnabled=false
12:13:26,228 INFO [org.jboss.ws.common.management.DefaultEndpointRegistry] (MSC service thread 1-2) register: jboss.ws:context=helloServlet,endpoint=HelloServlet
12:13:29,986 INFO [stdout] (MSC service thread 1-2) HelloServlet is instantiated!
12:13:29,991 INFO [org.apache.cxf.service.factory.ReflectionServiceFactoryBean] (MSC service thread 1-2) Creating Service {http://helloservlet.hello.servlet/}HelloServletImplService from class servlet.hello.helloservlet.HelloServlet
12:13:30,023 INFO [org.apache.cxf.endpoint.ServerImpl] (MSC service thread 1-2) Setting the server's publish address to be http://localhost:8080/helloServlet/HelloServlet
12:13:30,024 WARN [org.jboss.wsf.stack.cxf.resolver.JBossWSResourceResolver] (MSC service thread 1-2) Cannot resolve resource: cxf
12:13:30,030 INFO [org.jboss.wsf.stack.cxf.deployment.WSDLFilePublisher] (MSC service thread 1-2) WSDL published to: file:/home/jimma/x1/code/git-client/maerqiang-work/jboss-as/build/target/jboss-as-7.1.0.Alpha1-SNAPSHOT/standalone/data/wsdl/helloServlet.war/HelloServletImplService.wsdl
12:13:31,517 INFO [org.jboss.as.webservices.service.EndpointService] (MSC service thread 1-10) Starting service jboss.ws.endpoint.helloServlet.HelloServlet
As you see the HelloServletImpl.class (configured in web.xml <servlet-class>servlet.hello.helloservlet.HelloServletImpl</servlet-class>) is not a real servlet class, the webservie deployer will modify the web.xml and start the endpoint impl before the real servlet - org.jboss.wsf.stack.cxf.CXFServletExt which servers the servlt transport initialize and start to work. These webservice deployers which output above message are executed before the web deployer to create WebContext . That's why you see it is executed after the HelloServlet is Initialized. But ServletContextListener is actually executed as expected before the real servlets are initialized. Here in your war file is after CXFServletExt and Startup are initialized.
- Why is "<load-on-startup>" ignored when the implementation differ only in the WS implementation?
It is not ignored. The <load-on-startup> configured to HelloServlet is passed to org.jboss.wsf.stack.cxf.CXFServletExt. I tried with your example it works properly.
rushead wrote:
- Last but not least: What is the best and usual way to initialize a custom environment for several servlets in a war file?
I did not get what you want to initialize from the source code(maybe I missed something), so I am not sure if do the initialize work in a real servlet and inject something with WebServiceContext can help. Here is some documentation about how to access MessageContext and/or Http request and response:http://cxf.apache.org/docs/servlet-transport.html