ITeye博客

aspectj 引入通知 实例

meiyoudao · 2010-12-12 17:51 · 企业架构 · Java · XML · AOP · Markdown

[size=large][color=blue]你懂的[/color][/size] [code="java"] package com.meiyoudao.service; public interface BuyPersonService { public void buySomebody(); } [/code] [code="java"] package com.meiyoudao.service; public interface BuyPersonBySexService { public void buyPersonBySex(); } [/code] [code="java"] package com.meiyoudao.service.impl; import com.meiyoudao.service.BuyPersonService; public class BuyPersonServiceImpl implements BuyPersonService { public void buySomebody(){ System.out.println("Get someone!"); } } [/code] [code="java"] package com.meiyoudao.service.impl; import com.meiyoudao.service.BuyPersonBySexService; public class BuyPersonBySexServiceImpl implements BuyPersonBySexService { public void buyPersonBySex() { System.out.println("male!"); } } [/code] [code="java"] package com.meiyoudao.aop; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.DeclareParents; import com.meiyoudao.service.BuyPersonBySexService; import com.meiyoudao.service.impl.BuyPersonBySexServiceImpl; @Aspect public class JoinAop { //将该BuyPersonBySexServiceImpl.class 注入 * . @DeclareParents(value="*",defaultImpl=BuyPersonBySexServiceImpl.class) public BuyPersonBySexService buyPerson; } [/code] [code="xml"] [/code] [code="java"] package com.meiyoudao.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.meiyoudao.service.BuyPersonBySexService; import com.meiyoudao.service.BuyPersonService; public class BuyPersonTest { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("MyXml.xml"); BuyPersonService buyPerson = (BuyPersonService)app.getBean("buyPerson"); buyPerson.buySomebody(); BuyPersonBySexService buyPersonSex = (BuyPersonBySexService)buyPerson; buyPersonSex.buyPersonBySex(); } } [/code]

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。