dependencies before and after the inversion occurs:Dependency Injection:The manager class above is no longer directly dependent on any specific logging type, but in essence, we create the log class object or inside the manager (catch), if we want to log in a different way, or the manager class code, there is no way to Can we change the way we log records without having to change the manager's code? Of course there is, "dependency
{greet: function(msg) {alert(msg);}}}).controller('MyController',function($scope, greeter) {$scope.sayHello = function() {greeter.greet("Hello!");};});
When AngularJS instantiates this module, it searches for greeter and naturally passes its reference to it:
The internal processing process of AngularJS is as follows:
// Use the injector to load the application var injector = angular. injector (['ng ', 'myapp']); // load $ controller service through
Spring Dependency Injection Setter injection (via get and set method injection)Import the necessary jar packages (Spring.jar and Commonslogging.jar)Build Applicationcontext.xml (Spring Management Bean configuration file) in src directoryXML version= "1.0" encoding= "UTF-8"?>DOCTYPE Beans Public "-//speing//dtd bean//en
the application's dependency on the plug-in to be dispelled. You can also do this by using the service locator mode, and after discussing dependency injection we'll talk about service locator services location mode. the form of dependency injection
The basic idea of
PHP Dependency Injection and control inversion, PHP injection inversion
Di--dependency Injection Dependency Injection
Ioc--inversion of Control inversion
To understand the above two co
variable to a real Array. So far, it is not bad. Our test passed. The problem with this implementation is that we need to write the required parts twice and we cannot confuse their order. The additional custom parameters are always after the dependency.
Reflection Method
Definition reflection in Wikipedia refers to the ability of a program to check and modify the structure and behavior of an object at runtime. Simply put, in the context of JavaScri
Facultybean () { return new Faculty (2, "Bob", "A"); } Of course, if you have other ways to acquire entity classes, such as your project integrating Hibernate ORM or other tools to connect to the database, you do not need to add the corresponding bean to the spring container, and the implementation of the Getbean method can be changed accordingly. I'm just using these two entity-class beans as an example.Then when we invoke the test method of
productrepository, and once the latter's API changes, the former will inevitably change as a result.2. It is difficult to test the Productservice method, because Productrepository is not really connected to the database, and also relies on HttpContext, which is tightly coupled.3. HttpContext is currently used for caching, and Productservice will change if a caching mechanism (such as velocity or memcached) is to be replaced.Based on the unreasonable
Dependency injection or inverse of control (control reversal) design patterns come from a basic principle of Architecture Design:
1. Dependency inverse Policy (dependency inversion/dependency inversion)What is dependency inversio
. We cannot solve the problem by editing the function body. First, we can solve the dependency through the function parameters. That is:Copy codeThe Code is as follows:Var doSomething = function (service, router, other ){Var s = service ();Var r = router ();};We implement the functions we want by passing additional parameters. However, this will bring new problems. Imagine if our doSomething method is scattered in our code. If we need to change the
model data is modified;
$interval (function () {$scope. count++;}, 1000)Equivalent toSetInterval (function () {$scope. count++; $DIGETST ()/$apply (); }, 1000)
2. Dependency InjectionDependency: If a function call requires additional objects as formal parameters, this function relies on the parameter object.function Driver (CAR) {//driver relies on a car objectCar.start ();Car.run
(services); // routes existing Services to the IContainer container = builder in the Autofac management set. build (); return container. resolve
(); // Returns the IServiceProvider implemented by AutoFac}
In this way, you can use the Autofac method to manage dependency types, as in the past. The example is as follows:
public class AutofacModule : Module{ protected override void Load(ContainerBuilder builder) { builder.Register(c
. prototype. slice. call (arguments, 0) is required to convert the arguments variable to a real Array. So far, it is not bad. Our test passed. The problem with this implementation is that we need to write the required parts twice and we cannot confuse their order. The additional custom parameters are always after the dependency.Reflection methodDefinition reflection in Wikipedia refers to the ability of a program to check and modify the structure and behavior of an object at runtime. Simply put,
JavaScript, this refers specifically to the source code of the object or function being read and parsed. Let's complete the dosomething function mentioned at the beginning of the article. If you control it earlier,Output dosomething.tostring () log. You will get the following string: nbsp; "function (service, router, other) {nbsp; nbsp; var s = service (); nbsp; nbsp; var r = router (); "The string returned through this method gives us the ability to traverse the parameters and, more importantl
write down some of the goals that our dependency injection solution should achieve: nbsp; We should be able to register dependencies 1. Injection should accept a function and return a function we need 2. We can't write too many things--we need to streamline the beautifulSyntax 3.
actually a angular way to implement its dependency injection. I stole a little lazy, directly intercept the regular expression that gets the parameter in the angular code.
Copy Code code as follows:
/^function\s*[^\ (]*\ (\s* ([^\)]*) \)/M
We can modify the resolve code as follows:
Copy Code
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.