spring3.x Enterprise Application Development _IOC

Source: Internet
Author: User
Tags throwable

IOC concept:

Includes two content: Control & Reversal

For software, the selection control of a specific implementation class of an interface is removed from the calling class and transferred to a third-party decision. DI (Dependency Injection: Dependency injection) means that the invocation class's dependency on an interface implementation class is injected by a third party (container or collaboration Class) to remove the invocation class's dependency on an interface implementation class.


IOC type: From the injection method, the main can be divided into three types: constructor injection, attribute injection and interface injection.


Application of reflection in the IOC, small example:

Car.class

Package Com.wiseweb.ioc;public class Car {private String brand;p rivate string color;p rivate int maxspeed;p ublic Car () {} Public Car (string brand, string color, int maxspeed) {this.brand = brand; this.color = color; this.maxspeed = maxspeed;} public void Introduce () {System.out.println ("Brand:" + brand + "; color:" +color + "; maxspeed" + maxspeed);} Public String Getbrand () {return brand;} public void Setbrand (String brand) {This.brand = brand;} Public String GetColor () {return color;} public void SetColor (String color) {this.color = color;} public int getmaxspeed () {return maxspeed;} public void setmaxspeed (int maxspeed) {this.maxspeed = Maxspeed;}}
Reflecttest.class

Package Com.wiseweb.ioc;import Java.lang.reflect.constructor;import Java.lang.reflect.method;public class reflecttest {public static Car Initbydefaultconst () throws Throwable {ClassLoader loader = Thread.CurrentThread (). Getcontextclassloader (); Class clazz = Loader.loadclass ("Com.wiseweb.ioc.Car"); Constructor cons = Clazz.getdeclaredconstructor ((class[]) null); Car car = (car) cons.newinstance (); Method Setbrand = Clazz.getmethod ("Setbrand", String.class); Setbrand.invoke (car, "red Flag"); Method SetColor = Clazz.getmethod ("SetColor", String.class); Setcolor.invoke (car, "black"); Method setmaxspeed = Clazz.getmethod ("Setmaxspeed", Int.class); Setmaxspeed.invoke (car, $); return car;} public static void Main (string[] args) throws Throwable {Car car = initbydefaultconst (); Car.introduce ();}}

Output: Brand: Red flag; color: black; maxSpeed200


Class Loader ClassLoader: The class loader is looking for a bytecode file for a class and constructs an object component that the class represents within the JVM. In Java, the class loader loads a class into the JVM, taking the following steps:

1, loading: Find and import class files;

2, Link: Perform the calibration, preparation and resolution steps, wherein the resolution step is optional:

A, verify: check the positive determination of loading class file data;

B, prepare: Allocate storage space for static variables of class;

C, parse: Turn the symbolic reference into a direct reference;

3, initialization: The static variables of the class, static code block to perform initialization work.

Class loading work is the responsibility of ClassLoader and its subclasses, ClassLoader is an important Java Runtime system component that is responsible for locating and loading class bytecode files at run time. The JVM generates three ClassLoader at run time: the root loader, the Extclassloader (Extension class loader), and the Appclassloder (System class loader). Where the root loader is not a classloader subclass, it is written in C + +, so we do not see it in Java, the root loader is responsible for loading the JRE's core class library, such as Rt.jar, Charsets.jar, etc. under the JRE target. Both Extclassloader and Appclassloader are subclasses of ClassLoader. Where Extclassloader is responsible for loading the jar class package in the JRE extension directory ext; Appclassloader is responsible for loading the class package under the Classpath path.

There is a parent-child hierarchy between these three class loaders, that is, the root loader is the parent loader for Extclassloader, and Extclassloader is the parent loader for Appclassloader. By default, the class of the application is loaded with Appclassloader.

Package Com.wiseweb.ioc;public class Classloadertest {public static void main (string[] args) {ClassLoader loader = Thread. CurrentThread (). Getcontextclassloader (); SYSTEM.OUT.PRINTLN ("Current loader:" + loader); SYSTEM.OUT.PRINTLN ("Parent loader:" + loader.getparent ()); SYSTEM.OUT.PRINTLN ("Grandparent loader:" + loader.getparent (). GetParent ());}}


Output: current Loader:[email protected]
Parent Loader:[email Protected]
Grandparent Loader:null


The JVM loads the class using the "overall responsibility mechanism", "overall responsibility" means that when a classloader load a class, unless the other classloader is used to display the class, and the class is dependent on and referenced by the ClassLoader load; "delegation mechanism" is to delegate the parent loader to look for the target class, and to find and load the target class from its own classpath only if it is not found. This is from a security perspective, just imagine how horrible it would be if someone wrote a malicious base class (such as java.lang.String) and loaded into the JVM. However, due to the "overall accountability mechanism", java.lang.String is always loaded by the root loader, thus avoiding the occurrence of the above time.


spring3.x Enterprise Application Development _IOC

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.