Announcement Announcement Module
No announcement yet.
Advising components not managed by spring Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Advising components not managed by spring

    I have component which is not managed by Spring Container. It is created using new from third party library. I want intercept some of these component methods. I have read the documentation for LTW and it says to annotate the component with @Configurable and then this will eligible for Spring driven configuration . But I have no control over this component to annotate it.
    How can I intercept such component methods?

  • #2
    Use loadtimeweaving @Configurable isn't needed for using loadtimeweaving.

    I advice you to read about the different weaving options (runtime, loadtime, compile) then you notice you don't need @Configurable.


    • #3
      Thx for the response. I was going thru the document, but not successful in finding what I need. Can you point me the link?

      For instance, let's say I'm using a third party library and one of their classes uses javax.jms.TopicSession. Now I want to intercept these session methods and my provide my own behavior in my interceptor class (SessionAOPInterceptor).

      This is my sample config file.

      	<aop:aspect id="myAspect" ref="SessionInterceptordvice">
      	<aop:pointcut id="myPointCut" expression="execution(* javax.jms.Session.*(..))"/>
      	<aop:around pointcut-ref="myPointCut" method="createCustomTopic"/>
      <bean name="SessionInterceptordvice" class="com.gtech.aop.SessionAOPInterceptor"> </bean>


      • #4
        As stated before use 'loadtimeweaving' chapter 6 of the reference guide explains quite clearly on how to set it up.

        You are currently not using loadtimeweaving but a proxy based approach.


        • #5
          Thx for pointing me to the correct location in spring ref document. It works now with loadtimeweaving.

          Is there a way where I can set some custom properties in the aspect class when using LTW?

          For instance, using proxy approach I was setting some of the required properties on the Aspect as below

          <bean name="SessionInterceptordvice" class="com.gtech.aop.SessionAOPInterceptor"> 
            <property name="requiredProperty" value="testValue"/>
          Now my aspect looks like below when using LTW. How can I invoke setRequiredProperty() method? I need this property to be set before customeCreateDestination() is invoked? Any ideas?

          public class SessionAOPIntercepto {
          	private String requiredProperty;
          	public Object customCreateDestination(ProceedingJoinPoint call) throws Throwable { 
                   //method implementation here
                  public void setRequiredProperty(String requiredProperty) {
          		this.requiredProperty = requiredProperty;
                 @Pointcut("execution(public * javax.jms.Session.create*(..))")
          	public void methodsToBeProfiled(){}


          • #6
            Like anyother spring bean.... Simply configure it.


            • #7
              I'm able to inject the dependencies into my aspect using aspectOf. I hope this is the correct way to inject dependencies into aspects

              <bean id="SessionInterceptordvice" class="com.gtech.aop.SessionAOPInterceptor" factory-method="aspectOf"> 
              <property name="requiredProperty" value="somevalue"/>
              Without aspectOf factory-method, there were 2 instance of SessionAOPInterceptor, one created by Spring (since it is defined as bean in spring config xml) which has correct injected value for requiredProperty and the other instance (create by AspectJ ???? since this aspect is defined aop.xml file) which didn't set the requiredProperty value.


              • #8
                My answer was a bit short, my mistake. It actually depends on how you have setup loadtime weaving.

                But the way you defined above is a good one.