Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Combined with reflection to illustrate the principle of SpringIOC implementation.

Shulou Source: shulou.com Published: 2022-06-01 23:56:13 09月12日 Update

This article is about the combination of reflection to explain how the implementation principle of SpringIOC is, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

IOC has been explained earlier, and it is also known that the principle of IOC is a reflection mechanism. So let's take a look at how to illustrate IOC through reflection mechanism.

Take a look at the code:

Package com.sinoair.gtms.controller.web.IOC

Import javax.annotation.Resource

Import javax.xml.ws.Service

Import java.lang.reflect.Field

Import java.util.HashMap

Import java.util.List

Import java.util.Map

Public class MyIOC {

/ * *

* simulate the comment package of Spring scan

, /

Private final static String packageName = "com.albert.AnnotationTest"

/ * *

* bean object where annotations are stored

, /

Private static Map beanMap = new HashMap ()

Static {

Try {

PackageToScan ()

} catch (InstantiationExceptione) {

E.printStackTrace ()

} catch (IllegalAccessExceptione) {

E.printStackTrace ()

}

}

/ * *

* get the bean object from the container according to beanName

* @ param beanName

* @ return

, /

Public static Object getBean (String beanName) {

Object obj = null

Obj = beanMap.get (beanName)

Return obj

}

/ * *

* iterate through the annotation classes in the package

* @ throws IllegalAccessException

* @ throws InstantiationException

, /

Private static void packageToScan () throws InstantiationException,IllegalAccessException {

List clsList = ClassByPackage.getClasssFromPackage (packageName)

/ / load dao first

For (Class cls: clsList) {

/ / determine whether the class has @ Respository annotations

If (cls.isAnnotationPresent (Respository.class)) {

String clsName = AlbertUtil.getBeanName (cls.getName ())

/ / bean object is stored in map

BeanMap.put (clsName,cls.newInstance ())

}

}

/ / load service and assign values to the attributes annotated by @ Resource

For (Class cls: clsList) {

If (cls.isAnnotationPresent (Service.class)) {

String clsName = cls.getName ()

ClsName = clsName.substring (clsName.lastIndexOf (".") + 1, clsName.length ()

BeanMap.put (AlbertUtil.toLowerCaseFirstOne (clsName), cls.newInstance ())

/ / scan the properties of the annotation @ Resource and assign values

Field [] field = cls.getDeclaredFields ()

For (Field f: field) {

/ / determine whether the attribute is annotated with @ Resource

If (f.isAnnotationPresent (Resource.class)) {

F.setAccessible (true)

F.set (beanMap.get (AlbertUtil.getBeanName (cls.getName (), beanMap.get (f.getName ()

}

}

}

}

}

}

The above is combined with reflection to illustrate the implementation principle of SpringIOC. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Annotations reflections principles objects attributes more mechanisms knowledge articles utility next code at the same time containers that is work meetings articles see knowledge points industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Shulou Information OPPO Reno Apple Docker