Skip to content

Instantly share code, notes, and snippets.

@victorjspinto
Created November 3, 2016 13:04
Show Gist options
  • Save victorjspinto/2383793155b775f319b3a53239e10cce to your computer and use it in GitHub Desktop.
Save victorjspinto/2383793155b775f319b3a53239e10cce to your computer and use it in GitHub Desktop.
AOP - Sample
package com.springaction.knights;
public class BraveKnight implements Knight {
private Quest quest;
private Minstrel minstrel;
public BraveKnight(Quest quest, Minstrel minstrel) {
this.quest = quest;
this.minstrel = minstrel;
}
/* should Knight manage his own minstrel??? */
public void embarkQuest() throws QuestException {
minstrel.singBeforeQuest();
quest.embark();
minstrel.singAfterQuest();
}
}
===================== ApplicationContext.xml =====================
<beans>
<bean id="knight" class="com.springaction.knights.BraveKnight">
<constructor-arg ref="quest"/>
</bean>
<bean id="quest" class="com.springaction.knights.SlayDragonQuest">
<constructor-arg value="#(T(System.out)"/>
</bean>
<bean id="minstrel" class="com.springaction.knights.Minstrel">
<constructor-arg value="#(T(System.out)"/>
</bean>
<aop:config>
<aop:aspect ref="minstrel">
<aop:point-cut id="embark"
expression="execution (* *.embarkOnQuest(..))"/>
<aop:before pointcut-ref="embark" method="singBeforeQuest"/>
<aop:after pointcut-ref="embark" method="singAfterQuest"/>
</aop:aspect>
</aop:config>
</beans>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment