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配置文件中配置如下:
PersonDaoImpl
Transaction
PerosnService
TransactionTest
虽然可以通过编程的方式织入切面,但是一般情况下,我们还是使用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(); } }
相关文章推荐
- win7+iis7+asp+.net+php环境配置
- ASP.NET MVC EF 02 数据库访问层的设计
- 【dotnet跨平台】最新版dotnet-cli下的ASP.NET Core和asp.net mvc【RC2尝鲜】
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第二篇:第一个页面
- ASP.NET MVC案例教程(基于ASP.NET MVC beta)——第一篇:准备工作
- 动画队列,第一个动画的字体变大和第二个动画的元素高度增加同时进行,两个动画完成之后,才触发第3个动画
- ASP.NET生成验证码
- 【java】解决JDK出现Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=gasp的问题
- 个人网站实现扫码登录asp.net 扫码登录
- asp.net MVC控制器返回json问题总结
- asp 后台获取 input 的值
- ASP.NET MVC HtmlHelper之Html.ActionLink
- ssh整合context:component-scan包名写了*号:Failed to parse configuration class [org.springframework.cache.aspectj.AspectJJCacheConfiguration]
- [Asp.net 开发系列之SignalR篇]专题一:Asp.net SignalR快速入门
- raspberry pi 2 install Ubuntu 14.04 LTS
- Win10 兼容性 Visual studio web应用程序 ASP.NET 4.0 尚未在 Web 服务器上注册
- ASP.NET MVC EF 01 框架搭建
- 2016-AspNet-MVC教学-8-异步Controller的应用
- asp.net mvc源码分析-ModelValidatorProviders 客户端的验证
- ASP.NET总结