spring ioc study notes

ioc is the core DI, the purpose is to provide a simpler mechanism to set the component dependencies, and manage the entire life cycle of these dependencies. Dependencies require some assembly is generally referred to as a dependent object, or target object is referred to in a case where the ioc. Ioc generally can be decomposed into two subtypes: DI and look, these are further broken down into sub-types ioc services implementation. We can clearly see by this definition, when talking about DI, usually talking about ioc, ioc when talking about, not always talking about DI (dependent lookup is also a form of ioc). When using the look-dependent, the formation must dependencies referenced using dependency injection, the injection assembly by dependency ioc container, dependent lookup There are two types: pulling dependent, context-dependent lookup. There are two types of dependency injection: constructor injection and setter injection. The following introduced one
dependent pull
-dependent pull ioc is the most common type, in dependence pull extracted from the registry dependencies as needed.
spring ioc study notes
Spring provides dependent pull mechanism as a retrieval framework managed components:
public class DependencyPull {
public static void main (String [] args) {
the ApplicationContext applicationContext the ClassPathXmlApplicationContext new new = ( "Spring / context.xml-App");
MessageRenderer = to applicationContext.getBean of Mr ( "the renderer", MessageRenderer.class);
mr.render ();
}
}
context-dependent lookup
Zhao Zhao in some ways dependent on context and dependence pull similar lookup is performed for the management of container resources, rather than from a central registry, and usually performed at a set point.
spring ioc study notes
Dependent lookup to work to achieve the following components through interfaces, by implementing this interface, a component can send a signal that it wants to acquire the dependency of the container.
interface ManagedComponent {public
void performLookup (Container Container);
}
public interface {Container
Object getDependency (String Key);
}
public class ContextualizedDependencyLookup the implements ManagedComponent {
Private Depency depency;
public void performLookup (Container Container) {
this.depency = (Depency) Container .getDependency ( "MyDependency");
}

@Override
public String toString() {
    return depency.toString();
}

}
When the container is ready to be passed to the component dependencies, each component in turn calls performLookup () method, and then use the container assembly may interface to find the dependencies.
Constructor injection
when provided in the constructor dependency assembly, constructor dependency injection occurs. First, the life of one or a set of constructors, and its entries dependency as a parameter, then the dependency ioc container when the assembly is passed to the component instantiation. One obvious result of using constructor injection is that if no dependencies can not create an object, you must have a dependency.
{class ConstructorInjection public
Private the Dependency dependency;

public ConstructorInjection(Dependency dependency) {
    this.dependency = dependency;
}

@Override
public String toString() {
    return dependency.toString();
}

}
Setter injection
in the setter injection, ioc container assembly dependencies can be injected by the setter method. The method of assembly disclosed setter dependency ioc container may be managed. Use setter injection of an obvious consequence is that you can create objects without dependencies, then the dependencies can be provided by calling the setter. In fact, setter injection is the most widely used injection mechanism, is also one of the easiest ioc mechanism.
{class SetterInjection public
Private the Dependency dependency;

public void SetterInjection(Dependency dependency) {
    this.dependency = dependency;
}

@Override
public String toString() {
    return dependency.toString();
}

}
Injection and find
now the question is, if you can choose which method to use should inject or look? The answer is absolutely injection. On the one hand, the injection of the components has no effect. On the other hand, rely pull must take the initiative to get a reference to the registry and interact to get the dependencies.
If you use injection, it is free to use and completely separate class ioc container, ioc container and provide for their collaborators dependent objects manually; but if you use search, then your class is always dependent on the container defined classes and interfaces . Another disadvantage is the difficulty to find independent test container class, injection can be used to test their easy assembly, because dependencies can be provided by using an appropriate constructor or setter.
setter injection and constructor injection
is now the question again, we have chosen to use the injection, but the choice is setter injection or constructor injection? The answer is the flexibility to use
when you must have an instance of a dependent class before using the component, constructor injection is particularly useful, but if you use constructor injection, you may not be related to a container by way of a statement on the dependent demand items, in addition constructor injection also contributes to the use of immutable objects.
setter injection is useful in various situations. If a component exposes its dependencies to the container, and are happy to provide their own defaults, then the setter injection is usually the best method of achieving this. Another benefit of setter injection is that it allows a dependency on the interface statement, although this method is not useful to the previous method. setter injection also allows real-time exchange of dependencies for different implementations without having to create a new instance of the parent component. Spring's JMX support makes this function possible. Perhaps the greatest benefit of setter injection is that it is the injection mechanism in the least intrusive.
Generally, the injection should be selected according to the type of usage. Allow the exchange of the setter-based dependency injection without creating a new object, and can also allow to select appropriate default value of the class, without explicit implanted object. When it is desired to ensure that the dependency passed to components and design immutable object constructor injection is a good choice.

Guess you like

Origin blog.51cto.com/14760318/2481354