I've had a similar problem. Starting my Spring application without Jetty:
Code:
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-configuration/demoQuartz-applicationContext.xml");
works fine. With VM argument given as follows:
Code:
-javaagent:~/tools/spring-framework-2.5/dist/weaving/spring-agent.jar
Starting the same application inside Jetty
Code:
public static void main(String[] args) {
// start spring container
String[] configLocations = new String[] { "classpath:spring-configuration/demoQuartz-applicationContext-jetty.xml" };
ApplicationContext context = new FileSystemXmlApplicationContext(configLocations);
Server server = (Server) context.getBean("jettyServer");
// get host
String host = server.getConnectors()[0].getHost();
if (host == null) {
host = "localhost";
}
// get port
int port = server.getConnectors()[0].getPort();
// get context path
Handler[] handlers = ((ContextHandlerCollection) server.getHandlers()[0]).getHandlers();
String contextPath = ((WebAppContext) handlers[0]).getContextPath();
if (LOG.isInfoEnabled()) {
LOG.info("server started - 'http://" + host + ":" + port + contextPath + "'");
}
}
fails:
Code:
java.lang.IllegalStateException: ClassLoader [org.mortbay.jetty.webapp.WebAppClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method.
at org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver.<init>(ReflectiveLoadTimeWeaver.java:103)
at org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver.<init>(ReflectiveLoadTimeWeaver.java:86)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
I started Jetty with the following Spring configuration:
Code:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:/spring-configuration/jetty.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
<property name="propertiesArray">
<props>
<prop key="jetty.port">8080</prop>
<prop key="jetty.contextPath">/</prop>
<prop key="jetty.war">src/main/webapp</prop>
</props>
</property>
</bean>
<bean id="jettyServer" class="org.mortbay.jetty.Server" init-method="start" destroy-method="stop">
<property name="connectors">
<list>
<bean id="Connector" class="org.mortbay.jetty.nio.SelectChannelConnector">
<property name="port" value="${jetty.port}"/>
</bean>
</list>
</property>
<property name="handler">
<bean id="handlers" class="org.mortbay.jetty.handler.HandlerCollection">
<property name="handlers">
<list>
<bean id="contexts" class="org.mortbay.jetty.handler.ContextHandlerCollection">
<property name="handlers">
<list>
<bean class="org.mortbay.jetty.webapp.WebAppContext">
<property name="contextPath" value="${jetty.contextPath}"/>
<property name="war" value="${jetty.war}">
</property>
</bean>
</list>
</property>
</bean>
</list>
</property>
</bean>
</property>
</bean>
The problem was one missing line of configuration in the jetty-XML above. After i added the load-time-weaver configuration to the jetty running the Spring application everything runs as smoothly as outside the container.
Code:
<context:load-time-weaver />
Now all my @Configurable beans get @Autowired inside Jetty, too.
Spring is