Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Problem getting started

  1. #1
    Join Date
    Aug 2004
    Posts
    6

    Default Problem getting started

    I am having trouble getting a aop demo working in SPRING.

    My test program starts a ClassPathXmlApplicationContext which loads the folling applicationContext;

    <bean id="beforeAdvice" class="MyBeforeAdvice"/>

    <bean id="beforeAdvisor"
    class="org.springframework.aop.support.RegexpMetho dPointcutAdvisor">
    <property name="advice"><ref local="beforeAdvice"/></property>
    <property name="pattern"><value>.*</value></property>
    </bean>

    <bean id="test1" class="org.springframework.aop.framework.ProxyFact oryBean">
    <property name="proxyInterfaces"><value>TestBean</value></property>
    <property name="target"><bean class="TestBeanImpl"/></property>
    <property name="interceptorNames">
    <list>
    <value>beforeAdvisor</value>
    </list>
    </property>
    </bean>

    beforeAdvice just prints a message so I know it is called.

    The first time I run test app to get the 'test1' bean and call its methods I got

    NoClassDefFoundError: net/sf/cglib/proxy/MethodInterceptor

    I expected it to proxy the TestBean interface supplied to proxyInterfaces and not use cglib to proxy the TestBeanImpl class, but I go along with this anyway and put cglib on the classpath.

    Now I have the following exception, any help with this would be greatly appreciated.

    - Jeff


    org.springframework.aop.framework.AopConfigExcepti on: Cannot create AopProxy with no advisors and no target source
    at org.springframework.aop.framework.Cglib2AopProxy.< init>(Cglib2AopProxy.java:77)
    at org.springframework.aop.framework.DefaultAopProxyF actory$CglibProxyFactory.createCglibProxy(DefaultA opProxyFactory.java:47)
    at org.springframework.aop.framework.DefaultAopProxyF actory$CglibProxyFactory.access$000(DefaultAopProx yFactory.java:44)
    at org.springframework.aop.framework.DefaultAopProxyF actory.createAopProxy(DefaultAopProxyFactory.java: 32)
    at org.springframework.aop.framework.AdvisedSupport.c reateAopProxy(AdvisedSupport.java:454)
    at org.springframework.aop.framework.ProxyFactoryBean .getSingletonInstance(ProxyFactoryBean.java:210)
    at org.springframework.aop.framework.ProxyFactoryBean .getObject(ProxyFactoryBean.java:194)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getObjectForSharedInstance(AbstractBea nFactory.java:469)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:137)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeansOfType(DefaultListableB eanFactory.java:144)
    at org.springframework.beans.factory.BeanFactoryUtils .beansOfTypeIncludingAncestors(BeanFactoryUtils.ja va:109)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.findMatchingBeans(DefaultListab leBeanFactory.java:268)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireByType(Abstract AutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:460)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:232)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:177)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:159)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:177)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:268)
    at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:58)
    at RunTest.main(RunTest.java:14)
    Ex

  2. #2

    Default

    If you want to make sure the JDK proxy are used, you have to specify the interfaces you want to be implemented by the proxy. I don't know why you got that error with CGLIB though.

    Guillaume

  3. #3
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Jeff Gager,

    Could you provide the source code for your classes?
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  4. #4
    Join Date
    Aug 2004
    Posts
    6

    Default

    OK this is my advisor

    public class MyAdvisor implements PointcutAdvisor {

    private Advice advice;
    private Pointcut pointcut;

    /*
    * @see org.springframework.aop.PointcutAdvisor#getPointcu t()
    */
    public Pointcut getPointcut() {
    if (pointcut == null) {
    pointcut = new Pointcut() {
    public ClassFilter getClassFilter() {
    return new ClassFilter() {
    public boolean matches(Class arg0) {
    return true;
    }
    };
    }
    public MethodMatcher getMethodMatcher() {
    return new MethodMatcher() {
    public boolean matches(Method arg0, Class arg1) {
    if (arg0.getName().indexOf("meth") == 0)
    return true;
    return false;
    }
    public boolean isRuntime() {
    return false;
    }
    public boolean matches(Method arg0, Class arg1, Object[] arg2) {
    return false;
    }
    };
    }
    };
    }
    return pointcut;
    }

    /*
    * @see org.springframework.aop.Advisor#getAdvice()
    */
    public Advice getAdvice() {
    if (advice == null) {

    advice = new MethodBeforeAdvice() {
    private boolean initialised = false;
    public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
    Object o = arg2;
    if (o instanceof Initializable && !initialised) {
    Initializable i = (Initializable)o;
    i.init();
    initialised = true;
    }
    }
    };
    }
    return advice;
    }

    /*
    * @see org.springframework.aop.Advisor#isPerInstance()
    */
    public boolean isPerInstance() {
    return true;
    }

    }

  5. #5
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    Jeff,

    I do not know where interface Initializable comes from, so I created a simple one for testing:
    Code:
      public interface Initializable &#123;
        void init&#40;&#41;;
      &#125;
    TestBean interface:
    Code:
      public interface TestBean &#123;
        int methodSum&#40;int a, int b&#41;;
      &#125;
    TestBeanImpl class:
    Code:
      public class TestBeanImpl implements TestBean, Initializable &#123;
        public void init &#40;&#41; &#123;
          System.out.println &#40;"initializing..."&#41;;
        &#125;
    
        public int methodSum&#40;int a, int b&#41; &#123;
          return a + b;
        &#125;
      &#125;
    and my applicationContext.xml
    Code:
    <beans>
      <bean id="beforeAdvice" class="MyAdvisor"/>
    
      <bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces"><value>TestBean</value></property>
        <property name="target"><bean class="TestBeanImpl"/></property>
        <property name="interceptorNames">
         <list>
          <value>beforeAdvice</value>
         </list>
        </property>
      </bean>
    </beans>
    This sample runs using JDK1.4, Jakarta commons logging, aopalliance.jar and of course spring.jar (no CGLIB).
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  6. #6

    Default

    Jeff,

    Looking at your code, there's some redundant stuff in there. First, when you provide an interceptor/advice that you want to apply to all methods of the instance, there's no need to wrap it in a RegexMethodPointcutAdvisor like you did. ProxyFactoryBean accept interceptors/advices, and providing one directly avoid the regexp layer which wouls make things slower.

    Also, instead of implementing PointcutAdvisor, you can simply implement MethodBeforeAdvice, and pass that bean's name to the ProxyFactoryBean.

    From your code, I don't see that any of it should fail, but eliminating the redundancy should probably make things easier to debug.

    Guillaume

  7. #7
    Join Date
    Aug 2004
    Location
    India
    Posts
    16

    Default

    Quote Originally Posted by gpoirier
    Also, instead of implementing PointcutAdvisor, you can simply implement MethodBeforeAdvice, and pass that bean's name to the ProxyFactoryBean.
    I have a different opinion here . I have noticed the flow as
    ProxyFactoryBean ------> createAdvisorChain() -----> addAdvisor() -----> namedBeanToAdvisorOrTargetSource() ----------------------------------------> DefaultAdvisorAdapterRegistry.wrap()
    So the wrap does always make sure that your Advise is Wrapped . The following code shows that :
    public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
    if (adviceObject instanceof Advisor) {
    return (Advisor) adviceObject;
    }

    if (!(adviceObject instanceof Advice)) {
    throw new UnknownAdviceTypeException(adviceObject);
    }
    Advice advice = (Advice) adviceObject;

    if (advice instanceof Interceptor) {
    // So well-known it doesn't even need an adapter
    return new DefaultPointcutAdvisor(advice);
    }
    for (int i = 0; i < this.adapters.size(); i++) {
    // Check that it is supported
    AdvisorAdapter adapter = (AdvisorAdapter) this.adapters.get(i);
    if (adapter.supportsAdvice(advice)) {
    return new DefaultPointcutAdvisor(advice);
    }
    }
    throw new UnknownAdviceTypeException(advice);
    }
    Jeff Gager , can you post all the code here ? I mean the Interface , the Target class TestBean .

    Regards
    Vicky

  8. #8

    Default

    Quote Originally Posted by vickyk
    I have a different opinion here . I have noticed the flow as
    ProxyFactoryBean ------> createAdvisorChain() -----> addAdvisor() -----> namedBeanToAdvisorOrTargetSource() ----------------------------------------> DefaultAdvisorAdapterRegistry.wrap()
    So the wrap does always make sure that your Advise is Wrapped . The following code shows that :
    I'm not sure how what you're saying is any different than what I said? My point was that you don't have to make your own Advisor, you can just pass an Advice to ProxyFactoryBean and it will nicely wraps it in an Advisor.

    Jeff,

    I also noticed your code is not synchronized. If your TestBeanImpl was to be called in concurrent threads, then the init method might be called twice.

  9. #9
    Join Date
    Aug 2004
    Location
    India
    Posts
    16

    Default

    Quote Originally Posted by gpoirier
    I'm not sure how what you're saying is any different than what I said? My point was that you don't have to make your own Advisor, you can just pass an Advice to ProxyFactoryBean and it will nicely wraps it in an Advisor.
    Ya that is correct , I have interpreted it wrongly .In this case he could use the DefaultPointcutAdvisor.
    Quote Originally Posted by gpoirier
    ProxyFactoryBean accept interceptors/advices, and providing one directly avoid the regexp layer which wouls make things slower.
    Do you mean the slower Development or Slower Execution here ?

    Regards
    Vicky

  10. #10

    Default

    I meant slower Execution. Using a regular expression is slower then just returning true. Might not be a huge difference, especially if the method's gonna do DB stuff, but there's no real gain for using RegexpMethodPointcutAdvisor when you want to match all methods. It also adds uneeded configuration code.

Similar Threads

  1. Replies: 1
    Last Post: Jul 5th, 2005, 03:48 AM
  2. Replies: 1
    Last Post: Jun 30th, 2005, 12:56 AM
  3. pagination and continuation problem in SWF
    By yfmoan in forum Web Flow
    Replies: 6
    Last Post: Jun 29th, 2005, 03:42 AM
  4. Replies: 0
    Last Post: Feb 16th, 2005, 01:45 PM
  5. Lazy Load Problem when Doing UnitTest
    By yoshi in forum Data
    Replies: 7
    Last Post: Sep 29th, 2004, 10:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •