In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.