Three methods of Spring dependency injection: instance explanation and spring
Three methods of Spring dependency injection (DI) are as follows:
1. Interface Injection
2. Setter method Injectio
hearsay, true and false, at least the latest In the Java Web framework, the search for more Spring MVC is enough to explain the problem.This request and method of the mapping of the annotated notation, I have to continue in-depth research, testing, and then fill in the instructions.I simply can enter the breakpoint of this method, and I also have some Mybatis dependency injected in the Spring configuration file, the actual operation, and no interface
) {self :: $instance = new Phalapi_di (); Self:: $instance->onconstruct (); } Return self:: $instance;}
It's not hard, we just use it. The operation of the new class is encapsulated into the static method of the class that we need to be new, the same as the above to judge, it can be very simple to implement the singleton mode.
2. Dependency Injectio
To understand the two concepts of PHP Dependency Injection and control reversal, you must be aware of the following issues:
Di--dependency Injection Dependency Injection
Ioc--inversion of Control inversion
1, who are the partic
Often seen but not quite understand the concept of dependency injection (DI) and controller inversion (IOC), find a few good articles, share. Self-understanding, Dependency injection is a component that obtains dependent objects through constructors, methods, or attribute fields. Take a real-life example to understand,
implementation of this class. It can persistently store Membership data to SQL Server 2005.
1. Strategy
An atypical strategy is used here ). In a typical policy mode, the Provider should be injected through the constructor. In this case, Membership is a static class, and its Provider and ProviderCollection are both read-only attributes, therefore, I guess the Provider is not injected by other classes, but obtained by the Provider itself through the application context. The role of the Policy mo
) "Circle" } [2]=> object(ReflectionMethod)#4 (2) { ["name"]=> string(4) "area" ["class"]=> string(6) "Circle" }}
You can also use getConstructor () to obtain the class constructor. The returned value is a ReflectionMethod object.
$constructor = $reflectionClass->getConstructor();
Parameters of the Reflection Method
$parameters = $constructor->getParameters();
The returned value is an array composed of
We know that spring's dependency injection has four ways, namely Get/set method injection, constructor injection, static factory method injection, instance factory method injectionLet's start by analyzing these kinds of injection
=angular.module ("MyApp", ['ngroute']);
For the second parameter of the Angular.mmodule () method, we have always been an empty array, and in the implementation of the routing function, because we want to rely on the Ngroute module in Angular-route.min.js to implement the routing function, the second parameter is set to [ ' Ngroute '], the array parameter is to place the other module name we want to rely on, if we want to rely on more than one mo
Ioc--inversion of Control inversionDi--dependency Injection Dependency InjectionTo understand the above two concepts, it is necessary to clarify the following questions:
Who are the participants?
Dependence: Who depends on who? Why do I need to rely?
Inject: Who injects into who? What exactly is injected?
Control reversal: Who Controls who? C
;
Method injection of the instance factory
The instance factory means that the method of getting an object instance is not static, so you need to first new factory class and then call the normal instance method: Java code
Public class Daofactory {
//Instance factory
Public Factorydao Getfactorydaoimpl () {
return new Factorydaoimpl ();
}
}
So the following class has nothing to say, similar to
injection is implemented in the following way:Based on the interface. Implements a specific interface for an external container to inject an object of the dependent type.Based on the set method. The public set method that implements a specific property lets the outer container invoke an object that is passed in to the dependent type.Based on the constructor function. A constructor that implements a specific parameter, passing in an object of the depe
1. Introduction
Java ee cdi mainly uses the @ Inject annotation to implement dependency injection, injecting managed beans into other resources managed by containers. In this tutorial, we will introduce several different optional policies in the CDI environment to implement dependency injection.
This tutorial is based
Spring MVC stands straight and says, "dependency injection, I am here !, Mvc waist
Study Spring MVC to distribute requests to Spring dependent injection class instances
Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)
This article follows the "signature-non-commercial use-consistency" creation public agreement
Reprinted please keep this se
activatorutilities, but there is always two ways to create an object without injecting property values.Simply call these two methods: first in the Startup.configureservices function, add the statement Services.addtransient
1. iuser User = activatorutilities. CreateInstance(serviceprovider, typeof(iuser));
2. iuser User = serviceprovider. GetService(typeof(iuser))
Both functions return the same result, and if there is an
and MyDependencyResolver. cs provided in MVC3 Beta:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Mvc;
Using Microsoft. Practices. Unity;
Namespace Demo
{
Public class MyDependencyResolver: IDependencyResolver
{
# Region IDependencyResolver Member
///
/// Dependency injection container
///
Private UnityContainer _ unityContainer;
///
///
Dependency Injection is a design pattern in PHP programming and is designed to reduce coupling and improve maintainability.Problem phenomenon: We often encounter a class in the programming process to instantiate another class, this class is instantiated another class, this layer of package form, often let us modify the code at the time, modified once, related to the upper layer will also be modified, if the
1. Control reversal (inversion of controls) and dependency injection (Dependency injection)
Control inversion, the IOC (inversion of controls), gives the container the invocation of objects traditionally manipulated directly by program code, and implements assembly and management of object components through container
Spring's second feature is dependency injection.Learning Dependency Injection, you should first understand two questions: 1, who depends on who, 2, who injected, injected what?First, look at the code:Or is this bean: PackageTestSpring.business.bean;Importorg.springframework.stereotype.Repository;ImportTestSpring.busine
, like an injection, it injects into a, which completes the control of the relationship between the various objects. A relies on connection to function properly, and this connection is injected into a by spring, and the name of the dependency injection comes in. So how is di implemented? An important feature after Java 1.3 is reflection (reflection), which allows
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.