Spring @work Demo

定义一个接口
java 代码
 
  1. package com.tyq.spring.aop.annotation;  
  2.   
  3. public interface Say {  
  4.     void say();  
  5.     void say(String str);  
  6.     String say(String str,int i);  
  7. }  
实现接口
java 代码
 
  1. import org.apache.commons.logging.Log;  
  2. import org.apache.commons.logging.LogFactory;  
  3.   
  4. public class SayImpl implements Say {  
  5.     private static final Log log = LogFactory.getLog(SayImpl.class);  
  6.     @Override  
  7.     public void say() {  
  8.         System.out.println(this.getClass().getMethods()[0]);  
  9.   
  10.     }  
  11.   
  12.     @Override  
  13.     public void say(String str) {  
  14.         System.out.println(this.getClass().getMethods()[0]);  
  15.   
  16.     }  
  17.   
  18.     @Override  
  19.     public String say(String str, int i) {  
  20.         System.out.println(str + i);  
  21.         return str;  
  22.     }  
  23.   
  24. }  
@Aspect的定义
java 代码
 
  1. package com.tyq.spring.aop.annotation;  
  2.   
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5. import org.aspectj.lang.annotation.AfterReturning;  
  6. import org.aspectj.lang.annotation.Aspect;  
  7. import org.aspectj.lang.annotation.Pointcut;  
  8.   
  9. @Aspect  
  10. public class AopAfterReturning {  
  11.     @Pointcut("execution(* Say.*(..))")  
  12.     public void test(){  
  13.         System.out.println("hello");  
  14.     }  
  15.     @AfterReturning("test()")  
  16.     public void afterTest()  
  17.     {  
  18.         System.out.println(this.getClass());  
  19.     }  
  20. }  
applicationContex.xml配置文件
xml 代码
 
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:aop="http://www.springframework.org/schema/aop"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
  6.                         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">  
  7.     <aop:aspectj-autoproxy/>   
  8.     <bean id="aspect" class="com.tyq.spring.aop.annotation.AopAfterReturning"/>  
  9.     <bean id="sayImpl" class="com.tyq.spring.aop.annotation.SayImpl"/>  
  10. beans>  

主函数入口
java 代码
 
  1. package com.tyq.spring.aop.annotation;  
  2.   
  3. import org.apache.commons.logging.Log;  
  4. import org.apache.commons.logging.LogFactory;  
  5. import org.springframework.context.ApplicationContext;  
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  7.   
  8. public class TestMain {  
  9.   
  10.     /** 
  11.     * @param args 
  12.     */  
  13.     protected static Log log = LogFactory.getLog(TestMain.class);  
  14.     public static void main(String[] args){  
  15.         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml",TestMain.class);  
  16.         Say test = (Say)context.getBean("sayImpl");  
  17.         test.say();  
  18.         test.say("how are you!");  
  19.         test.say("who are you !"2);  
  20.         log.info("hello");  
  21.     }  
  22.   
  23. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值