Understanding of IOC and AOP

(1) ioc, which means Inversion of control, (inversion of control), inversion of control, is the meaning of exchanging control. Now generally do not use this word, generally use dependency injection (dependency injection). Dependency injection is to inject dependencies into it.
(2) Let’s put it this way, in an action or event, for example, you want to write (Action) now, then you need a pen, so you have a new pen to write, here, you use a new pen, Your action is related to the pen. Without the pen, you can't write. That is to say, your behavior depends on the pen, and they form a dependency. Or if you want to assemble a computer (Transaction) now, then you need objects such as display, motherboard, keyboard, mouse and CD-ROM. These objects are usually new, and the new object has a dependency on the current (this) object.

Spring uses injection for dependent objects, which is often referred to as dependency injection.

(3) Invert the control
to give you a simple example:
1. Without IOC: a person carrying a large bag of explosives to blow up an enemy bunker
2. Using IOC: this person runs to the enemy bunker with nothing , and then called the headquarters and said, throw the explosives at me.

 

(4) The ssh framework is one of the more popular frameworks at present. Sometimes according to the needs of the project, only struts and hibernate may be used. Sometimes it could be spring.
Start by understanding the functions of the three separate frameworks. The purpose of struts is mainly to request and corresponding distribution jump. The collection of page data is obtained. Hibernate is mainly aimed at the interaction of the DB layer. DB connections, operations on persistent objects, etc. The core content of spring should be IOC. Understand its Inversion of Control and OOP (Aspect Oriented)

(5) The so-called IoC, a simple understanding is to transfer the operation of the new object to the spring container for execution.
Moreover, the core of IoC is actually a factory mode, and the factory mode is to create (new) objects. In the factory mode, reflection is generally used to new specific objects, and then return instances.
(6)

IOC,控制反转这样理解
举个简单的例子
一个人要去砍柴。那么绝大部分时候,我们会这样设计程序
class Axe
{
   //一些字段或方法
}
class person
{
    private Axe axe = new Axe();        //自己制造斧头
    public void cut(Axe axe)
    {
        axe.cut();
    }
}
即是,我们要去砍柴,我们要自己制造斧头。
而IOC的意思就是我们需要斧头,这时候斧头就已经制造好了,我们去取就可以,不用自己制造.

class person
{
    private Axe axe = springFactory.getBean("axe");
    public void cut(Axe axe)
    {
        axe.cut();
    }
}
这些axe就是在spring的配置文件里声明的bean.

IOC和控制反转是一个意思
(7)<bean id="ss" class="A">
    <property name="dao">
        <ref bean="dbdao" />
    </property
</bean>

个人理解:以上就是ioc的核心,意思是在此创建dbdao的对象,此对象在类A中使用,在类A中使用时候,用地名字是ss。通过getbean(ss)来获取对象。

(8)我来给你个简单明了的解释。
    控制反转又成为依赖注入,主要为了降低类之间的耦合度,类A依赖类B的时候我们按传统写法就需要在类A里面调用类B对象的方法,而用SPRING的话,就相当于提供了一个接口,在类A里面调用这个接口就可以得到类B对象,不用NEW出类B的对象来。利用接口的原理来降低了耦合度(如果你熟习接口的设计和使用就会很清楚)。
    而为了实现上述原理,我们把他写成配置文件,然后在程序运行时用反射的方式来加载这个配置文件(用spring时就是用的反射机制运行时调用),找到要使用的类,并由spring给你生成对象。这样就OK了。
    最后在总结下这两个名词。控制反转,就是交换控制权的意思,比如我类A需要用到类B的时候,具体的实现方式是在类B的某个方法里,也就是说类B控制着这个业务的具体实现。而现在用IOC以后,类B交出控制权,类A来进行控制,类A里只需要调用一个接口的方法,不管你这个方法的具体实现是由类B的对象来实现,还是由其他类的对象来实现,反正类A调用这个接口的这个方法就可以搞定他需要实现的业务内容,这样一来,类A它看上去就得到了实现某个业务的控制权。而依赖注入这个词则体现得更加专业一点,就是讲在我的程序里,我从来不去构造(new HelloWorld()这样的方法)任何对象,只是在需要用到(也就是依赖)某个对象的时候,我就用spring给他注入这个对象。这个注入的方式也就降低了程序的耦合度
(9)

AOP面向切面编程 将程序中的交叉业务逻辑(比如安全,日志,事务等),封装成一个切面,然后注入到目标对象(具体业务逻辑)中去。 比如: 很多方法可能会抛异常,你要记录这个异常到日志中去,可以写个拦截器类,在这个类中记录日志,在spring.xml中配置一个对这些要记录日志的方法的aop拦截器 在这个方法执行后调用这个拦截器,记录日志。这样就不用每次抛异常都要手动记录日志。 spring的事务管理用到的就是aop 这样也可以提高程序的内聚性。
(10)
aop叫aspect oriented program,面向切面的编程 ioc是invert of control,反转控制 在spring in action那本书里有详细阐述,简单说一下,ioc就是其实就是依赖注入,即用接口编程,在程序中不出现new关键字,而是用接口来命名引用,然后通过某种方式(多数用spring,不过Google guice也是很好的ioc框架)把接口的某个实现类的实例注入到引用里,从而实现与接口具体实现类的松耦合 aop方式就理解起来就简单了,其方式很类似j2ee中的filter,就是在程序正常的业务流中间像切面一样插入很多其他需要执行的代码,比如登陆时候在进入登录页面前写入日志,登录以后查看cookie等类似的操作,很常用的,尤其是跟数据库有关的,或者跟支付有关的程序肯定会在每一步前面插入日志,还有某些国际化项目会在每次跳转时候都转换字符集之类
(11)我给你来个权威的,你答到这下面就基本不问了。 IOC(反转控制):对成员变量的赋值的控制权从代码中反转到配置文件中。 AOP:Aspect(切面) Oriented(面向) Programming(编程),面向切面编程。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325805409&siteId=291194637