分享

架构师面试之-------------mysql和 spring aop实现

 WindySky 2018-06-01
     1.前言。
 
如题。

2.代码。

(1)mysql中查找某字段相同的记录。

Java代码  收藏代码
  1. select distinct *  from tb A where exists(select 1 from tb B where A.id=B.id and A.content<>B.content)  


(2)main函数中实现spring aop
参考:http://hyhai7./blog/837497
Java代码  收藏代码
  1. 1.经典的基于代理的AOP   
  2. 2.@AspectJ注解驱动的切面   
  3. 3.纯POJO切面   
  4. 4.注入式AspectJ切面   
  5.   
  6. 首先看经典的基于代理的AOP:   
  7.   
  8. Spring支持五种类型的通知:   
  9. Before(前)  org.apringframework.aop.MethodBeforeAdvice   
  10. After-returning(返回后) org.springframework.aop.AfterReturningAdvice   
  11. After-throwing(抛出后) org.springframework.aop.ThrowsAdvice   
  12. Arround(周围) org.aopaliance.intercept.MethodInterceptor   
  13. Introduction(引入) org.springframework.aop.IntroductionInterceptor   
  14.   
  15. 值的说明的是周围通知,他是由AOP Alliance中的接口定义的而非Spring,周围通知相当于前通知、返回后通知、抛出后通知的结合(传说中的完全体?好吧,我看日和看多   
  16.   
  17. 了)还有引入通知怎么玩我还没搞清楚,等心无杂念的时候玩玩   
  18.   
  19. 这东西怎么玩?这么几个步骤:   
  20. 1.创建通知:实现这几个接口,把其中的方法实现了   
  21. 2.定义切点和通知者:在Spring配制文件中配置这些信息   
  22. 3.使用ProxyFactoryBean来生成代理   
  23.   
  24. 具体做法。。。大晚上的就举个睡觉的例子吧:   
  25.   
  26. 首先写一个接口叫Sleepable,这是一个牛X的接口,所有具有睡觉能力的东西都可以实现该接口(不光生物,包括关机选项里面的休眠)   
  27.   
  28. Java代码  收藏代码  
  29. package test.spring.aop.bean    
  30.     
  31. public interface Sleepable{    
  32.      
  33.     void sleep();     
  34. }    
  35.   
  36.   
  37. 然后写一个Human类,他实现了这个接口   
  38. Java代码  收藏代码  
  39. package test.spring.aop.bean    
  40.     
  41. public Human implements Sleepable{    
  42.        
  43.    /*这人莫非跟寡人差不多?  
  44.     *除了睡觉睡的比较好之外其余的什么也不会做?*/    
  45.    public void sleep(){    
  46.       System.out.println("睡觉了!梦中自有颜如玉!");    
  47.    }    
  48.     
  49. }    
  50.   
  51.   
  52. 好了,这是主角,不过睡觉前后要做些辅助工作的,最基本的是脱穿衣服,失眠的人还要吃安眠药什么的,但是这些动作与纯粹的睡觉这一“业务逻辑”是不相干的,如果把   
  53.   
  54. 这些代码全部加入到sleep方法中,是不是有违单一职责呢?,这时候我们就需要AOP了。   
  55.   
  56. 编写一个SleepHelper类,它里面包含了睡觉的辅助工作,用AOP术语来说它就应该是通知了,我们需要实现上面的接口   
  57. Java代码  收藏代码  
  58. package test.spring.aop.bean;    
  59.     
  60. import java.lang.reflect.Method;    
  61.     
  62. import org.springframework.aop.AfterReturningAdvice;    
  63. import org.springframework.aop.MethodBeforeAdvice;    
  64.     
  65. public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{    
  66.     
  67.     public void before(Method mtd, Object[] arg1, Object arg2)    
  68.             throws Throwable {    
  69.         System.out.println("通常情况下睡觉之前要脱衣服!");    
  70.     }    
  71.     
  72.     public void afterReturning(Object arg0, Method arg1, Object[] arg2,    
  73.             Object arg3) throws Throwable {    
  74.         System.out.println("起床后要先穿衣服!");    
  75.     }    
  76.         
  77. }    
  78.   
  79. 然后在spring配置文件中进行配置:   
  80. Xml代码  收藏代码  
  81. <bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper">    
  82. </bean>    
  83.   
  84. OK!现在创建通知的工作就完成了.   
  85.   
  86. 第二步是进行配置,这是很令人蛋疼的操作,尤其是这么热的天,Spring又把东西的名字起的见鬼的长!它为啥不能像usr这种风格呢?   
  87.   
  88. 首先要做的是配置一个切点,据说切点的表示方式在Spring中有好几种,但是常用的只有两种:1.使用正则表达式 2.使用AspectJ表达式 AspectJ我不是很熟悉(我也是熟悉   
  89.   
  90. 党 or 精通党?),我还是习惯用正则表达式   
  91.   
  92. Spring使用org.springframework.aop.support.JdkRegexpMethodPointcut来定义正则表达式切点   
  93. Xml代码  收藏代码  
  94. <bean id="spleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">    
  95.   <property name="pattern" value=".*sleep"/>    
  96. </bean>    
  97.   
  98. pattern属性指定了正则表达式,它匹配所有的sleep方法   
  99.   
  100. 切点仅仅是定义了故事发生的地点,还有故事发生的时间以及最重要的故事的内容,就是通知了,我们需要把通知跟切点结合起来,我们要使用的通知者是:   
  101. Java代码  收藏代码  
  102. org.springframework.aop.support.DefaultPointcutAdvisor    
  103.     
  104. <bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">    
  105.      <property name="advice" ref="sleepHelper"/>    
  106.      <property name="pointcut" ref="sleepPointcut"/>    
  107. </bean>    
  108.   
  109. 切入点和通知都配置完成,接下来该调用ProxyFactoryBean产生代理对象了   
  110. Xml代码  收藏代码  
  111. <bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">    
  112.      <property name="target" ref="human"/>    
  113.      <property name="interceptorNames" value="sleepHelperAdvisor" />    
  114.      <property name="proxyInterfaces" value="test.spring.aop.bean.Sleepable" />    
  115. </bean>    
  116.   
  117. ProxyFactoryBean是一个代理,我们可以把它转换为proxyInterfaces中指定的实现该interface的代理对象:   
  118. Java代码  收藏代码  
  119. import org.springframework.aop.framework.ProxyFactoryBean;    
  120. import org.springframework.context.ApplicationContext;    
  121. import org.springframework.context.support.ClassPathXmlApplicationContext;    
  122.     
  123. import test.spring.aop.bean.Sleepable;    
  124.     
  125.     
  126. public class Test {    
  127.     
  128.     public static void main(String[] args){    
  129.         ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");    
  130.         Sleepable sleeper = (Sleepable)appCtx.getBean("humanProxy");    
  131.         sleeper.sleep();    
  132.     }    
  133. }    
  134.   
  135. 程序运行产生结果:   
  136. 通常情况下睡觉之前要脱衣服!   
  137. 睡觉啦~梦中自有颜如玉!   
  138. 起床后要先穿衣服!   
  139.   
  140. OK!这是我们想要的结果,但是上面这个过程貌似有点复杂,尤其是配置切点跟通知,Spring提供了一种自动代理的功能,能让切点跟通知自动进行匹配,修改配置文件如下:   
  141. Xml代码  收藏代码  
  142. <bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper">    
  143.  </bean>    
  144.  <bean id="sleepAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    
  145.    <property name="advice" ref="sleepHelper"/>    
  146.    <property name="pattern" value=".*sleep"/>    
  147.  </bean>    
  148.  <bean id="human" class="test.spring.aop.bean.Human">    
  149.  </bean>    
  150.  <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>    
  151.   
  152. 执行程序:   
  153. Java代码  收藏代码  
  154. public class Test {    
  155.     
  156.     public static void main(String[] args){    
  157.         ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");    
  158.         Sleepable sleeper = (Sleepable)appCtx.getBean("human");    
  159.         sleeper.sleep();    
  160.     }    
  161. }    
  162.   
  163. 成功输出结果跟前面一样!   
  164. 只要我们声明了org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator(我勒个去的,名太长了)就能为方法匹配的bean自动创建代理!   
  165.   
  166. 但是这样还是要有很多工作要做,有更简单的方式吗?有!   
  167.   
  168. 一种方式是使用AspectJ提供的注解:   
  169. Java代码  收藏代码  
  170. package test.mine.spring.bean;    
  171.     
  172. import org.aspectj.lang.annotation.AfterReturning;    
  173. import org.aspectj.lang.annotation.Aspect;    
  174. import org.aspectj.lang.annotation.Before;    
  175. import org.aspectj.lang.annotation.Pointcut;    
  176. @Aspect    
  177. public class SleepHelper {    
  178.     
  179.     public SleepHelper(){    
  180.             
  181.     }    
  182.         
  183.     @Pointcut("execution(* *.sleep())")    
  184.     public void sleeppoint(){}    
  185.         
  186.     @Before("sleeppoint()")    
  187.     public void beforeSleep(){    
  188.         System.out.println("睡觉前要脱衣服!");    
  189.     }    
  190.         
  191.     @AfterReturning("sleeppoint()")    
  192.     public void afterSleep(){    
  193.         System.out.println("睡醒了要穿衣服!");    
  194.     }    
  195.         
  196. }    
  197.   
  198. 用@Aspect的注解来标识切面,注意不要把它漏了,否则Spring创建代理的时候会找不到它,@Pointcut注解指定了切点,@Before和@AfterReturning指定了运行时的通知,注   
  199.   
  200. 意的是要在注解中传入切点的名称   
  201.   
  202. 然后我们在Spring配置文件上下点功夫,首先是增加AOP的XML命名空间和声明相关schema   
  203. 命名空间:   
  204. xmlns:aop="http://www./schema/aop"   
  205. schema声明:   
  206. http://www./schema/aop   
  207. http://www./schema/aop/spring-aop-2.0.xsd   
  208.   
  209. 然后加上这个标签:   
  210. <aop:aspectj-autoproxy/> 有了这个Spring就能够自动扫描被@Aspect标注的切面了   
  211.   
  212. 最后是运行,很简单方便了:   
  213. Java代码  收藏代码  
  214. public class Test {    
  215.     
  216.     public static void main(String[] args){    
  217.         ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml");    
  218.         Sleepable human = (Sleepable)appCtx.getBean("human");    
  219.         human.sleep();    
  220.     }    
  221. }    
  222.   
  223. 下面我们来看最后一种常用的实现AOP的方式:使用Spring来定义纯粹的POJO切面   
  224.   
  225. 前面我们用到了<aop:aspectj-autoproxy/>标签,Spring在aop的命名空间里面还提供了其他的配置元素:   
  226. <aop:advisor> 定义一个AOP通知者   
  227. <aop:after> 后通知   
  228. <aop:after-returning> 返回后通知   
  229. <aop:after-throwing> 抛出后通知   
  230. <aop:around> 周围通知   
  231. <aop:aspect>定义一个切面   
  232. <aop:before>前通知   
  233. <aop:config>顶级配置元素,类似于<beans>这种东西   
  234. <aop:pointcut>定义一个切点   
  235.   
  236. 我们用AOP标签来实现睡觉这个过程:   
  237. 代码不变,只是修改配置文件,加入AOP配置即可:   
  238. Xml代码  收藏代码  
  239. <aop:config>    
  240.     <aop:aspect ref="sleepHelper">    
  241.     <aop:before method="beforeSleep" pointcut="execution(* *.sleep(..))"/>    
  242.     <aop:after method="afterSleep" pointcut="execution(* *.sleep(..))"/>    
  243.     </aop:aspect>    
  244. </aop:config>    


3.总结。
用AspectJ比较好,只需要写一个切类,和在xml加入一个<aop:aspectj-autoproxy/> 让程序自动识别@Aspect注解即可。   

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多