Announcement Announcement Module
No announcement yet.
Limit interfaces implemented by proxy Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Limit interfaces implemented by proxy

    I use aop to add behaviour to my beans e.g. like adding logging.

    I noticed that spring 2.5 will try to create a proxy-object that implements ALL interfaces that my bean class implements. However I know in my configuration that I advice only methods from a single interface. So there is no need for my proxy-objects to implement all the interfaces, but just he one I need.

    I can achieve this if I declare my bean as a ProxyFactoryBean and use the interfaces property.

    However that makes me write my aspects that must implement some Advice interface. I prefer the way I can use simple POJOs as aspects in spring2 aop.

    Is there any way to achieve this? If not, are there any plans in future versions to allow this?

    I also think of OSGi services where all interfaces implemented by a service object are not necessarily visible by the bundle that instantiates the service proxy. Only the service's interface should be implemented by the proxy-object.

  • #2
    Here some extra information on what I try to achieve.

    Suppose my code makes use of objects that implement InterfaceA. In my application I am only interested in that interface. In Spring I wire some beans, one of them being a bean of class SomeImplementation that implements InterfaceA.

    But besides that InterfaceA, the programmer that wrote SomeImplementation made it also implement InterfaceB and InterfaceC. He has his own reasons for doing so. However from my point of view, I am only interested in InterfaceA, as that's the one I need to wire in my beans.

    So when I set up my Spring config and provide some aop for my bean, implemented by SomeImplementation, there is no need for me to receive a proxy object that implements all these interfaces. Just InterfaceA is more than enough for me. My code will never call any other method as the ones defined in InterfaceA.


    • #3
      Just use spring 2 configuration and define your pointcut not towards the implementation class but to the interface you want to apply the aop beaviour


      • #4
        The fact that your code doesn't call it doesn't mean that there is no code calling it. So it is dangerous to limit the interfaces. Next to that the only way to achieve it is to use a ProxyFactoryBean instead of the config name space and declare all the interfaces you want to be proxied.


        • #5
          You mean that when using the aop namespace, even if you define a pointcut targeting only one interface the proxy will implement all of them?
          I had the impression that under the hoods it picked the interfaces from the pointcuts but I might unerstood it wrong..


          • #6
            The advice is applied based on the pointcut but a proxy will be created using all the interfaces.

            If you have a service SomeServiceImpl which is SomeService and UserDetailsService and you define a pointcut for the UserDetails for logging does that mean that the proxy isn't a SomeService anymore. That would lead you to all kinds of troubles if you try to inject it into different beans (once as a UserDetails and once as a SomeService).

            The PFB worked similair if you didn't specify the interfaces explicitly it would detect them and create a proxy implementing all of them.


            • #7
              Hmm yes it is definitely more wise this way.
              Now I wonder what really was the real benefit of old version's proxyInterfaces property.


              • #8
                We use it quite heavily internally, the EJB 2 stuff in spring for example. YOu can speficy a business interface but your EJB doesn't really have to implement that interface.


                • #9
                  I understand that the default behaviour is for the proxy to implement all interfaces. As you say, there is no way for Spring to know which of thes interfaces are needed.

                  However as a developer there can be cases where I know exactly which interfaces I do need. Limiting the proxy to these interfaces would make sense in that case.

                  I also used the ProxyFactoryBean to do this already.

                  What I would like to know is if it is possible to provide PFB with an interceptor that uses a POJO as advice, instead of a class that needs to implement one of MethodBeforeAdvice, AfterReturningAdvice or ThrowsAdvice. Using the aop schema to define advice this is easy. But can it be done with the PFB also?