Announcement Announcement Module
Collapse
No announcement yet.
Passing an argument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Passing an argument

    Hi, i have several classes:

    Code:
    package springtest;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    import springtest.fight.Borek;
    import springtest.fight.Warrior;
    
    public class Main {
    
        public static void main(String[] args) {
            ApplicationContext beanFactory =
            new FileSystemXmlApplicationContext("src\\springtest\\config.xml");
            Borek bor = (Borek) beanFactory.getBean("borek");
            Warrior war = (Warrior) beanFactory.getBean("warrior");
            war.fight();
        }
    
    }
    Code:
    package springtest.fight;
    
    public class Borek
    {
    
        public void beforeFight()
        {
            System.out.println("Let's fight");
        }
    
        public void beforeFight(Warrior war)
        {
            System.out.println("Let's fight " + war.getName());
        }
    
        public void afterFight()
        {
            System.out.println("After fight");
        }
    }
    Code:
    package springtest.fight;
    
    public class Warrior
    {
        public void fight()
        {
            System.out.println("Kadzia!");
        }
    
        public String getName()
        {
            return "Arthur";
        }
    }
    And an spring config:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans ...>
    
        <aop:aspectj-autoproxy />
    
        <bean id="borek" class="springtest.fight.Borek" />
        <bean id="warrior" class="springtest.fight.Warrior" />
        
        <aop:config>
            <aop:aspect ref="borek">
                <aop:pointcut id="fighth" expression="execution(* *.fight(..))" />
    
                <aop:before method="beforeFight" pointcut-ref="fighth" arg-names="borek" />
                
                <aop:after-returning method="afterFight" pointcut-ref="fighth" />
            </aop:aspect>
        </aop:config>
    
    
    
    </beans>
    And it invokes "beforeFight()" method. I would like to invoke beforeFight(Warrior w) method with "war" argument from main() method. Is this possible?

    Regards

  • #2
    You can with this() or target() argument (depending on if you want the proxy or the original bean). Check Spring documentation for more details.

    -Ramnivas

    Comment


    • #3
      Thanks for answer, but i can't find any example in documentation. Where should i enter that getTarget() method?

      Could you please modify my example?

      Thanks in advance.

      Comment


      • #4
        We have examples and explanation in http://static.springframework.org/sp...-advice-params

        You need execution(* *.fight(..)) && this(warrier).

        However, I strongly recommend that you read the documentation.

        -Ramnivas

        Comment

        Working...
        X