A small example of annotation injection of the IOC implementation of spring

Source: Internet
Author: User

First, we'll write a custom annotation

<span style= "FONT-SIZE:14PX;" >package Com.test.aonntion;import Java.lang.annotation.elementtype;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;//Life The annotation is scoped to the attribute @target ( Elementtype.field)//indicates that the annotation is run-time injected @retention (retentionpolicy.runtime) public @interface Ioc {// obj is the type of the subclass for which the parameter type is object, and the default parameter is Object.classpublic class< Extends object> obj () default Object.class;} </span>

then we declare three classes to test

First, declare an abstract parent class

<span style= "FONT-SIZE:14PX;" >package Com.test.aonntion;public Abstract class Parent {//abstract method public abstract void Say ();} </span>

in declaring two subclasses, inheriting from our parent class

Son Sub-category

<span style= "FONT-SIZE:14PX;" >package Com.test.aonntion;public class son extends parent{@Overridepublic Void Say () {//son soy sauce go System.out.println (" I ' m my Parent ' Son ');}} </span><span style= "Font-weight:bold;" ></span>


Daughter sub-class
<span style= "FONT-SIZE:14PX;" >package Com.test.aonntion;public class Daughter extends Parent {@Overridepublic void Say () {// The daughter soy sauce goes to System.out.println ("I ' m my Parent's Daughter");}} </span>

The next step is to finish writing our functional class .

<span style= "FONT-SIZE:14PX;" >package Com.test.aonntion;import Java.lang.reflect.field;public class Ioctest {public static void Startioc (String objname) throws exception{//gets the class class<?> clazz = Class.forName (objname);//Instantiate the class object TESTIOC = Clazz.newinstance ();//Get all the properties under the class field[] fields = Clazz.getdeclaredfields ();//Determine if there is a property in the class if (Fields! = null && Fields.length > 0) {//Traverse all properties for (int i =0;i < fields.length; i++) {//Get IOC annotations for current properties IOC IOC = Fields[i].getannotation ( Ioc.class);//If the IOC annotation for the current property is not empty and the value of obj is not NULL if (IOC! = null && ioc.obj () = null) {//Gets the class that we injected obj and instantiates the class to get the instance Ojbobj ECT OJB = Ioc.obj (). newinstance ();//Let the property support forced injection, either-when the property is a private variable and there is no set method//Force Call Set ("Property name", the attribute to be injected); fields[i].setaccessible (true);//Inject the object we instantiate into the attribute Fields[i].set (TESTIOC, OJB);}}} This is my son, soy sauce. @ioc (Obj=son.class)//declares a parent class object 1private Static parent parent;//This is my daughter soy sauce (@ioc) Declares a parent class object 2private static parent parent1;/** * @param args */public static void main (string[] args) {try {//Start note startioc ("Com.test.aonntion.IocTest"),} catch (Exception e) {e.printstacktrace ();} Father a sent soy sauce people said Parent.say ();//father B sent soy sauce said Parent1.say ();}} </span>

Test Results



A small example of annotation injection of the IOC implementation of spring

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.