Spring的<aop:aspectj-autoproxy>配置

通过配置织入@Aspect切面

虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用spring的配置自动完成创建代理织入切面的工作。

通过aop命名空间的<aop:aspectj-autoproxy />声明自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面。当然,spring

在内部依旧采用AnnotationAwareAspectJAutoProxyCreator进行自动代理的创建工作,但具体实现的细节已经被<aop:aspectj-autoproxy
/>隐藏起来了

<aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy   poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

通过在Spring的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="cn.xhx.spring.aop.xml.transaction"></context:component-scan>
	<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

PersonDaoImpl
package cn.xhx.spring.aop.xml.transaction;

import org.springframework.stereotype.Repository;

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

Transaction

package cn.xhx.spring.aop.xml.transaction;

import javax.annotation.Resource;

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;

@Component("transaction")
@Aspect
public class Transaction {
	@Pointcut("execution(* cn.xhx.spring.aop.xml.transaction.PersonDaoImpl.*(..))")
	public void aa(){}
	
	@Before("aa()")
	public void beginTransaction() {
		System.out.println("begin transaction");
	}
	@AfterReturning("aa()")
	public void commit() {
		System.out.println("commit");
	}
}

PerosnService
package cn.xhx.spring.aop.xml.transaction;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

@Service("personService")
public class PersonService {
	@Resource(name="personDao")
	private PersonDao personDao;
	public void savePerson() {
		personDao.savePerson();
	}
}

TransactionTest
package cn.xhx.spring.aop.xml.transaction;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TransactionTest {
	@Test
	public void testTransaction() {
		ApplicationContext applicationContext = 
				new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonService personService = (PersonService)applicationContext.getBean("personService");
		personService.savePerson();
		/**
		 * 如果要得到PersonDao的实现类,需要使用cglib的代理类,修改配置成:
		 * <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
		 * 
		 */
//		PersonDaoImpl personDaoImpl = (PersonDaoImpl)applicationContext.getBean("personDao");
//		personDaoImpl.savePerson();
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值