Spring AOP引入切面,如何实现业务逻辑与系统服务解耦?
- 内容介绍
- 文章标签
- 相关推荐
本文共计568个文字,预计阅读时间需要3分钟。
介绍层面是一种独特的增强类型,它并非在目标方法周围绕入增强,而是为目标类创建新的方法和属性。因此,引介增强的链接点属于类别别而非方法级别。通过引介增强,我们“
引介切面是一种比较特殊的增强类型,它不是在目标方法周围织入增强,而是为目标类创建新的方法和属性,所以引介增强的链接点是类级别的,而非方法级别的。通过引介增强,我们可以为一个目标类添加一个接口实现。即原来的目标类未实现某个接口,通过引介增强可以为目标类创建某个接口代理。这种功能富有吸引力,因为他能够在横向定义接口实现的方法。思考问题的角度发生了很大的变化。这也是切面编程魅力所在!!!- o -。/**要侵入的接口*/ package com.book.service; public interface Monitorable { public void setMonitorActive(Boolean flage); } package com.book.serviceImpl; import com.book.entity.User; /**目标类*/ public class UserServiceImpl { public User findByName(String name) { // TODO Auto-generated method stub System.out.println("进入了service层了哦!!!"); return new User(name,"26"); } } /**增强*/ package com.book.aop; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.support.DelegatingIntroductionInterceptor; import com.book.service.Monitorable; public class PerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable{ private ThreadLocal<Boolean> laosn=new ThreadLocal<Boolean>(); public void setMonitorActive(Boolean flage) { // TODO Auto-generated method stub laosn.set(flage); } public Object invoke(MethodInvocation mi) throws Throwable{ Object obj=null; if(laosn.get()!=null&&laosn.get()){ System.out.println("-------------哈哈!骚年!你被织入了接口了----------开始-----"); obj=super.invoke(mi); System.out.println("-------------哈哈!骚年!你被织入了接口了----------结束-----"); }else { obj=super.invoke(mi); } return obj; } } @Controller public class TianController { @Resource(name="userServiceProxyfactorybean") private UserServiceImpl userServiceProxyfactorybean; @RequestMapping("/tian2") public String tian2(){ /**转换成侵入接口然后再调用接口方法改变行为*/ Monitorable mo=(Monitorable)userServiceProxyfactorybean; mo.setMonitorActive(true); User user=proxyfactorybean.findByName("zhangtian"); return "user/index"; } } /**最后配置applicationContext.xml*/ <!-- 此配置演示了 引介切点 --> <bean id="performancemonitoradvice" class="com.book.aop.PerformanceMonitor"></bean> <bean id="userserviceTarget" class="com.book.serviceImpl.UserServiceImpl"></bean> <bean id="userServiceProxyfactorybean" p:interfaces="com.book.service.Monitorable" p:interceptorNames="performancemonitoradvice" p:target-ref="userserviceTarget" p:proxyTargetClass="true"class="org.springframework.aop.framework.ProxyFactoryBean"/>
本文共计568个文字,预计阅读时间需要3分钟。
介绍层面是一种独特的增强类型,它并非在目标方法周围绕入增强,而是为目标类创建新的方法和属性。因此,引介增强的链接点属于类别别而非方法级别。通过引介增强,我们“
引介切面是一种比较特殊的增强类型,它不是在目标方法周围织入增强,而是为目标类创建新的方法和属性,所以引介增强的链接点是类级别的,而非方法级别的。通过引介增强,我们可以为一个目标类添加一个接口实现。即原来的目标类未实现某个接口,通过引介增强可以为目标类创建某个接口代理。这种功能富有吸引力,因为他能够在横向定义接口实现的方法。思考问题的角度发生了很大的变化。这也是切面编程魅力所在!!!- o -。/**要侵入的接口*/ package com.book.service; public interface Monitorable { public void setMonitorActive(Boolean flage); } package com.book.serviceImpl; import com.book.entity.User; /**目标类*/ public class UserServiceImpl { public User findByName(String name) { // TODO Auto-generated method stub System.out.println("进入了service层了哦!!!"); return new User(name,"26"); } } /**增强*/ package com.book.aop; import org.aopalliance.intercept.MethodInvocation; import org.springframework.aop.support.DelegatingIntroductionInterceptor; import com.book.service.Monitorable; public class PerformanceMonitor extends DelegatingIntroductionInterceptor implements Monitorable{ private ThreadLocal<Boolean> laosn=new ThreadLocal<Boolean>(); public void setMonitorActive(Boolean flage) { // TODO Auto-generated method stub laosn.set(flage); } public Object invoke(MethodInvocation mi) throws Throwable{ Object obj=null; if(laosn.get()!=null&&laosn.get()){ System.out.println("-------------哈哈!骚年!你被织入了接口了----------开始-----"); obj=super.invoke(mi); System.out.println("-------------哈哈!骚年!你被织入了接口了----------结束-----"); }else { obj=super.invoke(mi); } return obj; } } @Controller public class TianController { @Resource(name="userServiceProxyfactorybean") private UserServiceImpl userServiceProxyfactorybean; @RequestMapping("/tian2") public String tian2(){ /**转换成侵入接口然后再调用接口方法改变行为*/ Monitorable mo=(Monitorable)userServiceProxyfactorybean; mo.setMonitorActive(true); User user=proxyfactorybean.findByName("zhangtian"); return "user/index"; } } /**最后配置applicationContext.xml*/ <!-- 此配置演示了 引介切点 --> <bean id="performancemonitoradvice" class="com.book.aop.PerformanceMonitor"></bean> <bean id="userserviceTarget" class="com.book.serviceImpl.UserServiceImpl"></bean> <bean id="userServiceProxyfactorybean" p:interfaces="com.book.service.Monitorable" p:interceptorNames="performancemonitoradvice" p:target-ref="userserviceTarget" p:proxyTargetClass="true"class="org.springframework.aop.framework.ProxyFactoryBean"/>

