Announcement Announcement Module
Collapse
No announcement yet.
Method interceptor in java classes not from beanfactory Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Method interceptor in java classes not from beanfactory

    Hi,

    I'm trying to use spring AOP in my project on classes that are not declared as beans in the bean factory.

    I tried the NameMatchMethodPointcutAdvisor, but I couldn't manage to get it working. I also tried out the AutoProxyCreator classes, but they only seem applicable for bean in the bean factory.

    So, is it possible to add a method interceptor in classes that are not defined in the bean factory? Any leads?

    Thanks a lot!
    Kris

  • #2
    So, is it possible to add a method interceptor in classes that are not defined in the bean factory?
    NAFAIK
    Any leads?
    AspectJ

    Comment


    • #3
      You could also use Spring AOP programmatically.

      Comment


      • #4
        Ah, so it's possible to add a method interceptor at runtime?? Do you have an example of this? I couldn't find munh info about this in the manual.

        Comment


        • #5
          Kris,

          I think this is what you are looking for: http://www.springframework.org/docs/....html#aop-prog.

          Comment


          • #6
            I do it a bit differently, first, I write a config xml like this (this code shows introduction advice):

            Code:
              <bean name="beanAdvisor" class="spring.ExtensionAdvisor" singleton="false">
                <constructor-arg index="0">
            			<bean class="Application.BeanExtensionImpl" singleton="false" />
            		</constructor-arg>
                <constructor-arg index="1">
            			<value>Application.BeanExtension</value>
            		</constructor-arg>
              </bean>
              <bean name="bean" class="org.springframework.aop.framework.ProxyFactoryBean">
                <property name="singleton"><value>false</value></property>
                <property name="proxyInterfaces">
                  <value>Application.BeanView</value>
                </property>
                <property name="interceptorNames">
                  <list>
                    <value>beanAdvisor</value>
                  </list>
                </property>
              </bean>
            And then use code like this to "wire" up the instance:

            Code:
               BeanView target = null;
               if&#40; context.containsBean&#40;"bean"&#41; &#41; &#123;
                  target = &#40;BeanView&#41; context.getBean&#40;"bean"&#41;;
                  &#40;&#40;Advised&#41; target&#41;.setTargetSource&#40;beanInstance&#41;;
               &#125;
            This allows me the flexibility of defining all the advice in a config file.

            Cheers

            Comment

            Working...
            X