3

I’m currently facing a Problem using a combination of JSF 2 and Spring 3 AOP.

The Application is designed as a classic three Layer archiecture (Web, Service, Database). So the Spring managed JSF-Beans call Service Beans which represent the Business logic.

The Managed Beans look like this:

@Named("startPage")
@Scope("view")
public class ManagedBean implements Serializable {

    @Inject
    private  ServiceBeanA serviceBeanA;

    public void someMethod() {
        serviceBean.doSomeBusinessLogic();
}

As this Bean is view scoped it implements Serializeable and so do the instance Variables (in this case ServiceBeanA) as described here: JSF backing bean should be serializable?

So far everything works as expected.

Now I’d like to Intercept the call of the business Methods declared in the Business Services. So I created an Aspect that does some basic logging and decared it in the spring config:

<bean id="aspectLogging" class="my.package.AspectLogging" />

<aop:config>
    <aop:aspect ref="aspectLogging">
        <aop:pointcut id="serviceMethodInvocation" expression="execution(*  my.services.ServiceBeanA.doSomeBusinessLogic())" />
        <aop:around pointcut-ref="serviceMethodInvocation" method="performLogging" />
    </aop:aspect>
</aop:config>

The Aspect:

public class AspectLogging {

    public Object performLogging(ProceedingJoinPoint pjp) throws Throwable {

        Object toReturn = null;        
        System.out.println(">>>>"+ pjp.getSignature().toLongString());
        toReturn = pjp.proceed();
        return toReturn;
    }
}

And now the Problem occures:

SCHWERWIEGEND: Exiting serializeView - Could not serialize state:        org.springframework.aop.aspectj.AspectJPointcutAdvisor
java.io.NotSerializableException: org.springframework.aop.aspectj.AspectJPointcutAdvisor

I found two identical problem descriptions at Springsource (unfortunately without beeing solved)

http://forum.springsource.org/showthread.php?87428-AspectJPointcutAdvisor-not-Serializable http://forum.springsource.org/archive/index.php/t-57602.html

Of course I could simply not use Aspect Orientation but the same problem can occur with any other third Library party.

What I did was marking the instance variable holding the business Service as transient.

public class ManagedBean implements Serializable {

    @Inject
    private  transient ServiceBeanA serviceBeanA;

    public void someMethod() {
     serviceBean.doSomeBusinessLogic();
    }

This works fine until the session is restored (deserialized). Of course the variable (serviceBeanA) is null after deserialization. I could configure the Web Container (e.g Tomcat) not to serialize sessions but this application runs in the cloud so I’m not in charge of configuring the Servers.

The only idea I had was using basic Java Serialization features by providing the

private synchronized void readObject(ObjectInputStream s) throws IOException,    ClassNotFoundException 

and

private synchronized void writeObject(ObjectOutputStream s) throws IOException

methods in the managed Bean.

private synchronized void readObject(ObjectInputStream s) throws IOException,    ClassNotFoundException {
    s.defaultReadObject();
    ApplicationContext context = new ClassPathXmlApplicationContext("services.spring-config.xml", "database.spring-config.xml");
    this.ServiceBeanA = (ServiceBeanA) context.getBean("serviceBeanA");
}

private synchronized void writeObject(ObjectOutputStream s) throws IOException {
    s.defaultWriteObject();
}

This solutions works, but it seems a very clumsy an unelegant solution. I have to repeat nearly the same code in every Managed Bean that uses Service Beans. Furthermore the Managed beans are tightly coupled to Spring in this way as they import Springs Application Context to restore the Service variables.

Can somebody think of a better architectural approach? Thanks in advance!

Community
  • 1
  • 1
Compito
  • 851
  • 5
  • 6
  • possible duplicate of [Spring session-scoped beans (controllers) and references to services, in terms of serialization](http://stackoverflow.com/questions/3180963/spring-session-scoped-beans-controllers-and-references-to-services-in-terms-o) – meriton Jan 23 '13 at 23:11
  • @meriton: Thanks a lot for the hint. This was indeed a duplicate – Compito Jan 25 '13 at 21:19

0 Answers0