I am currently working on an application, that is accessed as both a portlet and a servlet (at least some parts are). I am also using compile-time-weaving to inject dependencies into beans in the prototype scope, so for example beans that are created via "new" or more commonly via Hibernate, as described here: http://www.chrissearle.org/node/285 This is working fine so far, however I am now using a server-api that has to be called differently in servlets and portlets. So I created an interface for a service in my application, and two implementations to the interface, one for servlets and one for portlets, so each can use the server-api differently. Each implementation is configured only in the coresponding servlet/portlet-application-context. If the servlet is used first, this works fine, the service-implementation for the servlet is injected and used. However, once the portlet is used, only the service-implementation for the portlet will be injected and used, for both the portlet and the servlet.
I would have expected the containers for servlets and portlets to be separate. Is this an error or not supported by Spring, or is there a way to fix or work around this? The application is still using Spring 3.1.1, however the current version 4.1.1 is showing the same behaviour.
My configuration looks like this (massivly simplified):
Interface: MyService:
public interface MyService {
public String getText();
}
MyService-Implementation for Portlet:
public class MyPortletService implements MyService {
@Override
public String getText() {
return this.getClass().toString();
}
}
MyService-Implementation for Servlet:
public class MyServletService implements MyService {
@Override
public String getText() {
return this.getClass().toString();
}
}
Bean to be used by Servlet and Portlet:
@Configurable
public class MyBean {
@Autowired
private MyService myService;
public String getText() {
return myService.getText();
}
}
Call in Portlet- and Servlet-Controller:
logger.trace("new MyBean().getText(): " + new MyBean().getText());
servlet-application-context.xml:
<bean class="my.app.PortletController" />
<bean class="my.app.MyServletService"/>
portet-application-context.xml:
<bean class="my.app.ServletController" />
<bean class="my.app.MyPortletService"/>
relevant dependencies in pom.xml
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.1.RELEASE</version>
</dependency> -->
compile-time-weaving configuration:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<complianceLevel>1.6</complianceLevel>
<aspectLibraries>
<aspectLibrary>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</aspectLibrary>
</aspectLibraries>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
</plugin>
Log output if calling servlet, portlet, servlet in that order:
new MyBean().getText(): class my.app.MyServletService
new MyBean().getText(): class my.app.MyPortletService
new MyBean().getText(): class my.app.MyPortletService
Update 2014-11-27: I have now created a test-case outlining the problem:
https://github.com/ChrZae/servlet-portlet-spring-container-issue