博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring IOC、DI、AOP原理和实现
阅读量:6877 次
发布时间:2019-06-26

本文共 3126 字,大约阅读时间需要 10 分钟。

hot3.png

1)Spring IOC原理

         IOC的意思是控件反转也就是由容器控制程序之间的关系,把控件权交给了外部容器,之前的写法,由程序代码直接操控,而现在控制权由应用代码中转到了外部容器,控制权的转移是所谓反转。网上有一个很形象的比喻:

(2)DI(Dependency Injection,依赖注入)
        IoC的一个重点是在系统运行中,动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。方法instanceSpring1为Spring中的实现用到ClassPathXmlApplicationContext类,要实现IOC的原理读取注入属性实体业务接口和实现类beans.

(3)AOP面向切面

    AOP是OOP的延续,是(Aspect Oriented Programming)的缩写,意思是面向切面编程。要理解AOP首先得弄明白代理的概念。对于代理看下这篇文章。

  AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,如事务管理、安全检查、缓存、对象池管理等。 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,AOP 代理则可分为静态代理和动态代理两大类,其中静态代理是指使用 AOP 框架提供的命令进行编译,从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中"临时"生成 AOP 动态代理类,因此也被称为运行时增强。

知道这些其他的就是些配置了。

简单的实现annotations和xml对AOP的实现。

首先看下目录结构

MyInterceptor、MyInterceptor2分别是以annotations和xml定义的切面类

[java]  

  1. package com.service;  

  2.   

  3. import org.aspectj.lang.annotation.Aspect;  

  4. import org.aspectj.lang.annotation.Before;  

  5. import org.aspectj.lang.annotation.Pointcut;  

  6.   

  7.   

  8. public class MyInterceptor {  

  9.     @Pointcut("execution (* com.serviceImpl.PersonServiceImpl.*(..))")  

  10.     private void myMethod(){};  

  11.       

  12.     @Before("myMethod()")  

  13.     public void doAccessCheck(){  

  14.         System.out.println("before");  

  15.     }  

  16.       

  17. }  

[java]  

  1. package com.service;  

  2.   

  3. public class MyInterceptor2 {  

  4.     public void doAccessCheck(){  

  5.         System.out.println("before");  

  6.     }  

  7. }  

业务和接口

[java]  

  1. package com.service;  

  2.   

  3. public interface PersonService {  

  4.     public void save(String name);  

  5.     public void update(String name);  

  6. }  

[java]  

  1. package com.serviceImpl;  

  2.   

  3. import com.service.PersonService;  

  4.   

  5. public class PersonServiceImpl implements PersonService {  

  6.   

  7.     @Override  

  8.     public void save(String name) {  

  9.         // TODO Auto-generated method stub  

  10.         System.out.println("保存");  

  11.     }  

  12.   

  13.     @Override  

  14.     public void update(String name) {  

  15.         // TODO Auto-generated method stub  

  16.         System.out.println("修改");  

  17.     }  

  18.   

  19. }  

简单做个方法前通知,其他的都一样。

[java]  

  1. <?xml version="1.0" encoding="UTF-8"?>  

  2. <beans xmlns="http://www.springframework.org/schema/beans"  

  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  

  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans  

  5.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  

  6.             http://www.springframework.org/schema/aop  

  7.            http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">  

  8.   

  9.     <aop:aspectj-autoproxy/>  

  10.       

  11.     <bean id="personServiceImpl" class="com.serviceImpl.PersonServiceImpl"></bean>  

  12.     <bean id="personInterceptor" class="com.service.MyInterceptor2"></bean>  

  13.       

  14.     <aop:config>  

  15.         <aop:aspect id="asp" ref="personInterceptor">  

  16.             <aop:pointcut id="myCut" expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))"/>  

  17.             <aop:before pointcut-ref="myCut" method="doAccessCheck"/>  

  18.         </aop:aspect>       

  19.     </aop:config>  

  20. </beans>  

测试类

[java]  

  1. package com.test;  

  2.   

  3.   

  4.   

  5. import org.junit.Test;  

  6. import org.springframework.context.ApplicationContext;  

  7. import org.springframework.context.support.ClassPathXmlApplicationContext;  

  8.   

  9. import com.service.PersonService;  

  10.   

  11. public class AopTest {  

  12.       

  13.     @Test  

  14.     public void interceptorTest(){  

  15.         ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");  

  16.         PersonService ps = (PersonService) ac.getBean("personServiceImpl");  

  17.         ps.save("aa");  

  18.     }  

  19. }  

转载于:https://my.oschina.net/lenolong/blog/650082

你可能感兴趣的文章
CentOS服务器最新分区方案
查看>>
Linux下chkconfig命令详解
查看>>
我的友情链接
查看>>
android Support Annotations(注解支持)
查看>>
如何对待用户需求的几点思考
查看>>
团队进展报告(3)
查看>>
沈阳市房地产市场信息系统数据容灾与异地备份
查看>>
nlp Task2
查看>>
我的友情链接
查看>>
简单干净的C#方法设计案例:SFCUI.AjaxLoadPage()之一
查看>>
XMPP协议的原理介绍
查看>>
Undo管理
查看>>
jsp简易留言板
查看>>
3.15好水指数N1能否让饮水健康不失控?
查看>>
Linux防火墙的设置
查看>>
人人商城PHP网站打开网页空白
查看>>
redhat修改源
查看>>
ruby 访问权限
查看>>
linux磁盘管理命令--df
查看>>
cocos2d: 单击,双击,及双指捏合事件的判断
查看>>