Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Java reflection Mechanism in Spring IOC

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how to use the Java reflection mechanism in Spring IOC, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Application of Java reflection Mechanism in Spring IOC

IOC: "inversion of control" is not a technology, but an idea. Using IOC means handing over the object you have designed to the control of the container, rather than traditional direct control within your object.

This article mainly explains the principle of the underlying implementation of IOC (reflection), the implementation of Bean container, and does not elaborate on the concept of IOC.

In the configuration file of Spring, you often see the following configuration:

So how does Spring help us instantiate the object and put it in the container with this configuration? Yes, it is through reflection!

Here is Spring instantiating the object through configuration

The pseudo code that is put into the container:

/ / parse the element's id attribute to get the string value "courseDao" String idStr = "courseDao"; / / parse the element's class attribute to get the string value "com.qcjy.learning.Dao.impl.CourseDaoImpl" String classStr = "com.qcjy.learning.Dao.impl.CourseDaoImpl"; / / use reflection knowledge to obtain the Class class object Class cls = Class.forName (classStr) through classStr; / / instantiate the object Object obj = cls.newInstance () / / container represents Spring container container.put (idStr, obj)

By parsing the xml file, we get the contents of the id attribute and the class attribute, and use the reflection principle to obtain the instance object of the class in the configuration and store it in the bean container of Spring.

When you need to apply another kind of object to one class

The configuration of Spring is as follows

We continue to simulate in the form of pseudo-code

Implement the Spring underlying processing principle / / parse the element's name attribute to get the string value "courseDao" String nameStr = "courseDao"; / / parse the element's ref attribute to get the string value "courseDao" String refStr = "courseDao"; / / generate the setter method name String setterName = "set" + nameStr.substring (0,1). ToUpperCase () + nameStr.substring (1) / / get the Bean named refStr in the spring container, which will be used as the incoming parameter Object paramBean = container.get (refStr); / / get the Method class of the setter method, where cls is the Class object Method setter = cls.getMethod (setterName, paramBean.getClass ()) obtained by the reflection code; / / call the invoke () method, where obj is the Object object setter.invoke (obj, paramBean) obtained by the reflection code just now.

Through the analysis of the underlying principle of Spring, we can find that it is not difficult, all the reflection mechanisms are used, and the objects are instantiated through reflection and stored in the bean container of Spring.

As long as you see the full path to the class (package) in the code or configuration file. Class), the underlying principle is basically the reflection mechanism of Java.

Reflection mechanism, the function of reflection mechanism, the function of reflection mechanism 1. The function of reflection mechanism

Bytecode files can be manipulated (read and modified bytecode files) through the reflection mechanism in java language.

Code snippets (class files) can be manipulated through the reflection mechanism.

2. The function of Java reflection mechanism.

Determine the class to which an object belongs at run time

Construct an object of a class at run time

Determine the member variables and methods of a class at run time

Call the method of an object at run time

3. What are the important classes related to the reflection mechanism?

Java.lang.Class: represents the entire bytecode, represents a type, represents the entire class.

Java.lang.reflect.Method: represents the method bytecode in the bytecode and the method in the class.

Java.lang.reflect.Contructor: represents the constructor in the bytecode, and represents the constructor in the class.

Java.lang.reflect.Field: represents the attribute bytecode in the bytecode and the member variables in the class.

The code is as follows (example):

/ / java.lang.Classpublic class User {/ / Field int no; / / Constructor public User () {} public User (int no) {this.no = no;} / / Method public void setNo (int no) {this.no = no;}} these are all the contents of the article "how the Java reflection mechanism works in Spring IOC". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report