Announcement Announcement Module
Collapse
No announcement yet.
trouble with simple BeanNameAutoProxyCreator example Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • trouble with simple BeanNameAutoProxyCreator example

    I'm new to Spring, and tried to set up a BeanNameAutoProxyCreator example just to see the AOP stuff work, and I'm having some trouble.

    My xml looks like this:

    Code:
        <bean id="sampleSpringObject" class="com.agentscape.associate.test.SampleSpringObjectImpl">
            <property name="name" value="Joe"/>
            <property name="age" value="28"/>
        </bean>
    
        <bean id="autoLogger" class="com.agentscape.associate.test.AutoLoggerAdvice"/>
    
        <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="interceptorNames">
                <value>autoLogger</value>
            </property>
            <property name="beanNames">
                <value>*</value>
            </property>
        </bean>
    From the examples I've seen, I would've expected my before method on my AutoLoggerAdvice class to get called for any methods on my SampleSpringObject, but nothing seems to get called. I call the getters on my SampleSpringObject, and I don't see my before method get called.

    My Advice class is very simple, just:
    Code:
    public class AutoLoggerAdvice implements MethodBeforeAdvice &#123;
    
        public void before&#40;Method method, Object&#91;&#93; args, Object target&#41; throws Throwable &#123;
            System.out.println&#40;"method.getName&#40;&#41; = " + method.getName&#40;&#41;&#41;;
        &#125;
    
    &#125;
    I know the advice and the bean work, because I was able to get an example working with this xml:
    Code:
        <bean id="sampleSpringObjectTarget" class="com.agentscape.associate.test.SampleSpringObjectImpl">
            <property name="name" value="Joe"/>
            <property name="age" value="28"/>
        </bean>
    
        <bean id="autoLogger" class="com.agentscape.associate.test.AutoLoggerAdvice"/>
    
        <bean id="sampleSpringObject" class="org.springframework.aop.framework.ProxyFactoryBean">
            <property name="proxyInterfaces">
                <list>
                    <value>com.agentscape.associate.test.SampleSpringObject</value>
                </list>
            </property>
            <property name="interceptorNames">
                <list>
                    <value>autoLogger</value>
                </list>
            </property>
            <property name="target">
                <ref bean="sampleSpringObjectTarget"/>
            </property>
        </bean>
    Do I have something wrong in my BeanNameAutoProxyCreator config that it's not picking up the methods in my SampleSpringObject?

    Thanks in advance.

  • #2
    Re: trouble with simple BeanNameAutoProxyCreator example

    How are you loading it? You need to use the ApplicationContext (not the XmlBeanFactory). E.g.

    final BeanFactory beanFactory = new ClassPathXmlApplicationContext(appContextFile);

    Comment


    • #3
      Re: trouble with simple BeanNameAutoProxyCreator example

      Originally posted by hucmuc
      How are you loading it? You need to use the ApplicationContext (not the XmlBeanFactory). E.g.

      final BeanFactory beanFactory = new ClassPathXmlApplicationContext(appContextFile);
      That must be it. I was trying it with the XmlBeanFactory. Thanks for the tip.

      Comment

      Working...
      X