Announcement Announcement Module
Collapse
No announcement yet.
Self-referencing components Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Self-referencing components

    Hi guys,

    I'm sure this has been discussed before, but I can't seem to find anything on the subject in the forums.

    I have a Spring bean that needs to be wired to its proxied self so that it can call 2 proxied methods on itself, one that is transactional and one that isn't. These internal methods should not be declared in the interface. Basically:

    Code:
    public interface Service {
     void call2Methods();
    }
    
    @Component
    public class ServiceImpl implements Service {
      @Autowired
      private ServiceImpl self;
    
      public void call2Methods() {
        self.callTxMethod();
        self.callNotTxMethod();
      }
    
      @Transactional
      public void callTxMethod() { ... }
    
      private void callNotTxMethod() { ... }
    }
    Can this be done without resorting to compile-time AOP?

    Thanks,
    Spiff

  • #2
    6.6.1. Understanding AOP proxies:
    ...
    Okay, so what is to be done about this? The best approach (the term best is used loosely here) is to refactor your code such that the self-invocation does not happen. For sure, this does entail some work on your part, but it is the best, least-invasive approach. The next approach is absolutely horrendous, and I am almost reticent to point it out precisely because it is so horrendous. You can (choke!) totally tie the logic within your class to Spring AOP by doing this:
    Code:
    public class SimplePojo implements Pojo {
    
       public void foo() {
          // this works, but... gah!
          ((Pojo) AopContext.currentProxy()).bar();
       }
       
       public void bar() {
          // some logic...
       }
    }
    This totally couples your code to Spring AOP, and it makes the class itself aware of the fact that it is being used in an AOP context, which flies in the face of AOP.
    You can use load time aspectj weaving to achieve the necessary behavior as well.

    Comment


    • #3
      I would've liked to avoid declaring callTxMethod in the interface, but it seems I won't have any choice.

      Instead of the AopContext.currentProxy() method, I've made the component ApplicationContextAware and had it retrieve a reference to itself. A little bit more code I suppose, but it works equally well.

      Spasibo!

      Comment


      • #4
        Originally posted by spiff View Post
        I would've liked to avoid declaring callTxMethod in the interface, but it seems I won't have any choice.

        Instead of the AopContext.currentProxy() method, I've made the component ApplicationContextAware and had it retrieve a reference to itself. A little bit more code I suppose, but it works equally well.

        Spasibo!
        Neither putting method to the interface interface nor spring coupling is not necessary if you use aspectj weaving with spring.

        Comment

        Working...
        X