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.
, 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.
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.
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.
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.
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);
public void setFromEmail (String fromEmail);
publicString getPassword ();
public void setPassword (Stringpassword);
public String getSmtpHost ();
public voidsetSmtpHost (StringsmtpHost);
public String getUserId ();
publicvoid setUserId (StringuserId);