您的位置:首页 > 编程语言 > ASP

Spring的<aop:aspectj-autoproxy>配置

2016-04-03 17:52 801 查看
通过配置织入@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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: