码迷,mamicode.com
首页 > 其他好文 > 详细

使用Schema配置切面

时间:2017-09-17 22:52:26      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:color   rda   schema   一个   cat   返回   tostring   contex   exp   

除了只用接口和注解定义增强处理,还可以在Spring配置文件中通过aop命名空间将一个普通的JavaBean中的方法声明为增强处理

1.UserBizLogger

 1 //使用Schema 不用注解,不用实现接口
 2 public class UserBizLogger {
 3     private static final Logger log = Logger.getLogger(UserBizLogger.class);
 4     //前置增强 
 5     public void before(JoinPoint jp){
 6         log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+
 7                 "方法,方法的参数是:"+Arrays.toString(jp.getArgs()));
 8     }
 9     //后置增强
10     public void afterReturing(JoinPoint jp,Object returnValue){
11         log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+
12                 "方法,方法的返回值是:"+returnValue);
13     }
14 }

2.spring配置文件,引入aop命名空间

 1     <bean id="dao" class="com.dao.impl.IUserDaoImpl"></bean>
 2     <bean id="biz" class="com.biz.impl.IUserBizImpl">
 3         <property name="dao" ref="dao"></property>
 4     </bean>
 5     <!-- 声明增强方法所在的类 -->
 6     <bean id="thelogger" class="com.aop.UserBizLogger"></bean>
 7     <!-- 配置切面 -->
 8     <aop:config>
 9     <!-- 定义切入点 -->
10         <aop:pointcut expression="execution(* com.biz.IUserBiz.* (..))" id="pointcut"/>
11         <!-- 引入包含增强的bean -->
12         <aop:aspect ref="thelogger">
13         <!-- 将before方法定义为前置增强并引入切入点 -->
14             <aop:before method="before" pointcut-ref="pointcut"/>
15         <!-- 将afterReturing方法定义为后置增强并引入切入点 -->
16             <!-- 操作返回值 -->
17             <aop:after-returning method="afterReturing" pointcut-ref="pointcut" returning="returnValue"/>
18         </aop:aspect>
19     </aop:config>

3.从测试类

 1 /**
 2  * 
 3  * @author Mr
 4  * aop测试类
 5  */
 6 public class Test {
 7 
 8     public static void main(String[] args) {
 9         //解析配置文件
10         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
11         
12         IUserBiz biz = (IUserBiz) ac.getBean("biz");
13         User user = new User();
14         user.setUname("小老虎");
15         biz.save(user);
16     }
17 
18 }

4.测试效果

技术分享

 

使用Schema配置切面

标签:color   rda   schema   一个   cat   返回   tostring   contex   exp   

原文地址:http://www.cnblogs.com/myhzb/p/7538292.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!