spring ioc and aop

2010-03-29  来源:本站原创  分类:Java  人气:284 

IOC and AOP

Inversion of Control pattern (also known as dependency intervention) The basic idea is: Do not create objects, but the description of the way they were created. In the code is not directly connected with the objects and services, but in the configuration file which describes what a component needs a service. Container (in the Spring framework is the IOC container) is responsible for these linked.

In a typical IOC scenario, the container creates all objects, and set the necessary properties to connect them together, decide what time to call the method. The following table lists the IOC an implementation model.

Spring Framework's IOC container with type 2 and type 3 achieved.

Aspect-Oriented Programming

Aspect-Oriented Programming

, That is, AOP, is a programming technique that allows the programmer to the cross-cutting concerns, or responsibilities of a typical cross-section line of behavior (such as logging and transaction management) module. AOP's core structure is the context, it will affect more than one kind of behavior that encapsulate reusable modules.

AOP and IOC are complementary technologies, they all use a modular solution to enterprise application development in the complex issues involved. In a typical object-oriented development approach, you may want to log all statements on the methods and Java classes can be achieved in the log function. In the AOP approach, you can turn to the log service module, and to declare the manner they would be applied to the need to log components. Of course, the advantage is that Java classes do not need to know the existence of log service does not need to consider the relevant code. Therefore, applications written using Spring AOP is loosely coupled code.

AOP functionality is fully integrated into the Spring transaction management, logging and various other features of the context.

IOC Container

Spring design is the org.springframework.beans package, its design goal is used in conjunction with the JavaBean component. This package is usually not used directly by the user, but by the server be used as a function of the bottom most of the other intermediaries. The next highest level abstraction is the BeanFactory interface, it is the realization of factory design pattern that allows to create and retrieve objects by name. BeanFactory can also manage the relationship between objects.

BeanFactory support for two object model.

Single-state model provides the name of the object with a specific instance of sharing, you can query for its retrieval. Singleton is the default and most commonly used object model. For the non-state requirements are very satisfactory.

Prototype model to ensure that each search will be to create a separate object. In each user needs its own object, the prototype model is best.

bean factory concept is the basis for Spring as an IOC container. IOC will be the responsibility of dealing with things from the application code is transferred to the frame. As I will demonstrate in the next, as an example, Spring framework uses JavaBean properties and configuration data that must be set to the dependent relationship.

BeanFactory Interface

Because org.springframework.beans.factory.BeanFactory is a simple interface, so it can be achieved for a variety of underlying storage method. The most commonly used BeanFactory definition XmlBeanFactory, It is based on the definition of XML file into bean, as shown in Listing 1.

Listing 1. XmlBeanFactory

BeanFactory factory = newXMLBeanFactory (newFileInputSteam ( "mybean.xml"));

As defined in the XML file, loaded Bean is negative, which means you need bean before, bean itself will not be initialized. From the BeanFactory retrieve bean, simply call the getBean () method will be passed in the name of the bean can be retrieved, as shown in Listing 2.

Listing 2. GetBean ()

MyBean mybean = (MyBean) factory.getBean ( "mybean");

The definition of each bean can be a POJO (with the class name and JavaBean initialization attributes defined) or a FactoryBean. FactoryBean interface to use the Spring framework to build applications that add an indirect level.

IOC Example

Understanding of Inversion of Control The easiest way is to look at its practical application. In the Spring by a three-part series Part 1 of a summary, I use an example to demonstrate how Spring IOC container into the application's dependencies (rather than build them to come in).

I used to open the account online credit use cases as a starting point. For this implementation, open credit accounts, require the user to interact with the following services:

Credit grading services, query the user's credit history information.

Telematic link services, inserting customer information, customer information and credit card and banking information to link up for automatic debit (if required).

E-mail service to send e-mail messages related to credit card status.

3 Interface

For this example, I assume that the service already exists, the ideal situation is to use loosely coupled way to integrate them. The following list shows the three services, application programming interface.

Listing 3. CreditRatingInterface

public interface CreditRatingInterface publicbooleangetUserCreditHistoryInformation (ICustomer iCustomer);

Listing 3 shows the credit grading interface provides credit history information. It requires a Customer object that contains customer information. The implementation of the interface provided by the CreditRating class.

Listing 4. CreditLinkingInterface

public interface CreditLinkingInterface

public String getUrl (); public void setUrl (String url); publicvoid linkCreditBankAccount () throws Exception;

Link interface credit history credit information and bank information (if required) to connect together, and insert the user's credit card information. Credit Link interface is a remote service, it queries through the getUrl () method of the. URL of the bean from the Spring framework for configuration mechanism is set, I will discuss it. The implementation of the interface provided by the CreditLinking class.

Listing 5. EmailInterface

public interface EmailInterface

public void sendEmail (ICustomer iCustomer);

publicStringgetFromEmail ();

public void setFromEmail (String fromEmail);

publicString getPassword ();

public void setPassword (Stringpassword);

public String getSmtpHost ();

public voidsetSmtpHost (StringsmtpHost);

public String getUserId ();

publicvoid setUserId (StringuserId);

相关文章
  • spring ioc and aop 2010-03-29

    IOC and AOP Inversion of Control pattern (also known as dependency intervention) The basic idea is: Do not create objects, but the description of the way they were created. In the code is not directly connected with the objects and services, but in t

  • Spring IoC and AOP's core idea of 2010-03-04

    IoC and AOP are the core idea of Spring Of course, the most a frame-level lightweight components, a large number of configuration files is indispensable, but the core is to make these configuration files, configuration sections assembled together, an

  • Autumn,仿Spring IOC/AOP集成开发框架 2012-08-21

    为了能更为深入理解Spring原理,知道怎么用还不如会自己做,于是便动手完成了这个集成框架.功能点仿照Spring,包括IOC中的注入.延迟加载.装配集合等等一系列功能,AOP是采用jdk动态代理实现的,没用CGLIB,因此,被代理的对象必须继承某个接口. 源代码主要为: ConfigManager.java 负责解析xml.装配bean.获取bean等操作,是框架最重要的类. ParserHelper.java 辅助XML解析的类. ProxyHandler.java 代理类,用于支持AOP功

  • Learning spring IOC AOP 2010-09-03

    1. What is Inversion of Control pattern? Does not create objects, but create them in the way described. In the code is not directly connected with the objects and services, but in the configuration file which describes what a component needs a servic

  • Learning point gain IOC and AOP 2010-03-27

    All the programming methods, the ultimate aim of decoupling, this sentence is not my country of origin, but I now also strongly think so. The purpose of decoupling is why? After decoupling the code or what advantage? When each class or purpose of eac

  • Spring 2.x AOP declarative configuration 2010-08-14

    Example: In the example, we use <aop:config/> configure a section and block target Peoples of SayHello () method, in its implementation before the output message. People new target class package aop.test; public class People ( public String SayHello

  • Spring framework and AOP Research and Application of Thought 2010-09-29

    Spring framework and AOP Research and Application of Thought Spring AOP framework and the research and application of thought (1) Keywords: Spring AOP Introduction Aspect Oriented Programming (AOP) is more popular in recent years, computer technology

  • Spring refresher (f) AOP Aspect Oriented Programming <2> 2011-04-19

    The previous chapter we talked about using proxy mode to automatically manage ISpeaker interfaces in the say () method automatically in say () method before and after the addition we get / give gifts methods. But a more comprehensive idea of students

  • spring ioc 学习(1) 2012-01-15

    如果用通俗易懂的语言让一个完全不懂计算机的人理解你所谓的技术,那么说明你真的懂了.这句话是我曾经的一个师傅告诉我的... spring最大的魅力是IOC.AOP这个是业内公认的了..所以,站在巨人的肩膀上,做为菜鸟的我自然就从这两个部分去剖析spring的优美.我们搞技术的同时千万不要忘记我们是有血有肉的人类,不能因为盲目的去实现需求而去搞技术,如果我们搞了n久的spring开发,可我们紧紧停留在spring.jar与beans.xml的话,那真的是spring式的悲剧. 就我现在这个理解层次上

  • Spring Ioc (Inversion of Control) 2012-07-31

    以下整理自,如有侵权立即删除. Ioc Ioc(Inversion of Control),即控制反转.不是什么技术,而是一种设计思想.在Java开发中,Ioc意味着将你设计好的对象交给容器控制,而不是传统的在你的对象内部直接控制. 传统Java SE程序设计,我们直接在对象内部通过new进行创建对象,是程序主动去创建依赖对象:而IoC是有专门一个容器来创建这些对象,即由Ioc容器来控制对象的创建:IoC 容器控制了对象(主要控制了外部资源获取,不只是对象包括比如文件等). 传统应用程序是由我们

  • 类Spring IoC容器 2013-10-23

    在构建Tiny框架之初,想着是不是不要强依赖Spring?于是就写了一个类Spring的依赖注入容器,基本实现了Spring的依赖注入部分的功能,工作情况也良好,配置规范也是采用的Spring的规范.在前面一直是没有问题的,但是在做数据库业务的时候,出现问题了.做数据库,必须要考虑事务问题,而当时我的设计目标是对各种开源的ORMapping方案都不拒绝,这个时候就引入一个问题,要么自己再实现一套AOP,要么就复用Spring.当时预想的另外一个方案是核心框架采用非直接依赖Spring的方案,数据

  • 模拟spring IOC 实现 2014-03-02

    利用java的反射和动态代理实现IOC 在Java中,其反射和动态代理机制极其强大,我们可以通过其反射机制在运行时获取信息.而代理是一种基本的设计模式,它是一种为了提供额外的或不同的操作而插入到真实对象中的某个对象.而Java的动态代理在代理上更进一步,既能动态的创建代理对象,又能动态的调用代理方法.Java的反射和动态代理机制,使Java变得更加强大. Spring框架这几年风头正劲,虽然使用者众多,但真正了解其内部实现原理的朋友却并不是很多.其实,了解它的内部实现机制和设计思想是很有必要的大

  • Spring中的AOP(二)--AOP基本概念和Spring对AOP的支持 2014-03-17

    AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤,希望以更好的方式来组合业务逻辑的各个步骤.AOP框架并不与特定的代码耦合,AOP框架能处理程序执行中特定切入点,而不与具体某个类耦合(即在不污染某个类的情况下,处理这个类相关的切点).下面是一些AOP的一些术语: 切面(Aspect):业务流程运行的某个特定步骤,也就是应用运行过程的关注点,关注点通常会横切多个对象,因此常被称为横切关注点 连接点(JoinPoint):程序执行过程中明

  • spring IOC 实现原理模拟实现 2014-11-29

    原文系装载: http://blog.sina.com.cn/s/blog_6a7f00ed01011dyv.html 最近对spring IOC AOP 机制实现原理了解了下,在此做下整理,希望能给需要的朋友予以帮助.整理的资料来自互联网,文章开头是我写的测试代码例子,可以直接导入eclipse,别忘了导入dom4jjar包. 例子代码下载链接:http://download.csdn.net/source/630961 利用java的反射和动态代理实现IOC 在Java中,其反射和动态代理机

  • Spring IOC of the scope of the Bean property value: prototype and singleton 2010-03-29

    1. If the scope is set to scope = "singleton", will be placed in Spring IOC container Bean cache pool, and Bean reference back to the caller, spring IOC continues to follow up the lives of these Bean management. The default attribute is scope =

  • spring ioc of 2010-03-29

    To figure out three questions: 1 What is the ioc? 2 ioc advantages? 3 spring ioc concrete realization of the project? 1 What is the ioc ioc, flip the meaning of control, saying that white is in code do not want to go to new as before, an object, but

  • Step by step learning spring (6) - Annotations AOP examples of the realization of 2010-04-01

    I've just watched the next spring AOP, the next stop and organize knowledge. The following describes the use of AOP in spring are two examples: a way to implement the use of annotations, another way to implement the declaration adopted. These two exa

  • What is the spring ioc 2010-04-10

    In the actual development, we repeatedly try to avoid to minimize dependencies between objects that coupling. But how can we do that? Common business objects are dependencies between the business objects and business objects, business objects and per

  • Spring 2.0 AOP and transaction configuration breakthrough 2010-04-29

    One highlight of Spring 2.0 AOP AspectJ-style configuration. But we must by contrast, can see the 2.0-based AOP configuration is to jump out of a new sky. 1. Comparison Look at the 1.0 standard services configuration: <bean abstract="true">

  • Brief talk about the benefits of Spring IOC 2010-05-27

    Brief talk about the benefits of Spring IOC IOC: inversion of control, it is not a technology, it is a design pattern. The so-called inversion of control is the relationship between the container control program, rather than the traditional implement