Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Serialization and Annotations question

I'm trying to write an aspect that post-configures objects that are
being deserialized. This is from a Spring Framework point of view
where I want deserialized objects to be configured by the IoC
container.

This is also really the first time I actually use AspectJ so please
forgive me if I am asking stupid things.

So far I have this:

public aspect ReadObjectAspect extends BeanConfigurerSupport
{
   public ReadObjectAspect()
   {
       setBeanWiringInfoResolver(new AnnotationBeanWiringInfoResolver());
   }

	pointcut readObject() : call(Object java.io.ObjectInput+.readObject());

	after() returning(Object o) : readObject()
	{
		if (o != null) {
			Class clazz = o.getClass();
			if (clazz.getAnnotation(Configurable.class) != null) {
				configureBean(o);
			}
		}
	}
}

The above aspect runs after ObjectInput.readObject has created a new
object. It then asks Spring to configure the object.

A configurable object looks like this for example:

@Configurable("helloServiceClient")
public class HelloServiceClient implements Serializable
{
	private static final long serialVersionUID = 5450664838818451206L;
	
	private transient HelloService helloService;

   public HelloService getHelloService()
   {
       return helloService;
   }

   public void setHelloService(HelloService helloService)
   {
       this.helloService = helloService;
   }

   private String name;

   public String getName()
   {
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public void executeHelloService()
   {
       helloService.sayHello(name);
   }

	public HelloServiceClient(String name)
	{
		super();
		this.name = name;
	}
	
	public HelloServiceClient()
	{		
	}
}

When this object is deserialized, Spring will make sure helloService
is wired up to the bean with the same name from the bean context.

The above works, but I have some issues:

First of all, I'm not sure if attaching to ObjectInput.readObject is
the best way. I'm doing that because during deserialization the
constructors are not called so I'm not sure what pointcut to define
otherwise.

Second, I would prefer to get rid of the check to see if the
@Configurable annotation is present. It would be nicer if I could do
something like this:

	pointcut readObject() : call((@Configurable *)
java.io.ObjectInput+.readObject());

So let AspectJ only run this aspect when readObject returns an object
that is annotated with @Configurable. Is something like that possible,
or does AspectJ not work at runtime, which would be required I guess.

S.


Back to the top