码迷,mamicode.com
首页 > 编程语言 > 详细

SpringAop_注解

时间:2015-11-05 15:22:33      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
      <context:component-scan base-package="com.spring.aop.xml.transaction">
      </context:component-scan>
      <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
package com.spring.aop.xml.transaction;

public interface PersonDao {
    public void savePerson();
}

 

 

package com.spring.aop.xml.transaction;

import org.springframework.stereotype.Repository;

@Repository("personDao")
public class PersonDaoImpl implements PersonDao{
    public void savePerson() {
        System.out.println("save person");
    }
}

 

 

package com.spring.aop.xml.transaction;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Aspect
 * @Pointcut("execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))")
 * private void aa(){}==
 * <aop:config>
          <aop:pointcut 
              expression="execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))" 
              id="aa()"/>
   </aop:config>
 * @author zd
 *
 */
@Component("transaction")
@Aspect
public class Transaction {
    @Pointcut("execution(* com.spring.aop.xml.transaction.PersonDaoImpl.*(..))")
    private void aa(){}  //方法签名
    
    @Before("aa()")
    public void beginTransaction(){
        System.out.println("begin transaction");
    }
    
    @AfterReturning("aa()")
    public void commit(){
        System.out.println("commit");
    }
}

 

SpringAop_注解

标签:

原文地址:http://www.cnblogs.com/thinkpad/p/4939353.html

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