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 Spring ​ reflection Technology

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points about how to use Spring reflection technology. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Without parameters

Java code

Class.forName (className) .newInstance; / / Class.forName (className) .getDeclaredConstructor (String.class) .newInstance (Dawn); / / get the attribute Introspector.getBeanInfo (Person.class). GetPropertyDescriptors () / / modify the value of the bean property Person person= (Person) Class.forName (className) .getDeclaredConstructor (String.class) .newInstance (Dawn) by reflection mechanism. PropertyDescriptor [] ps = Introspector.getBeanInfo (Person.class). GetPropertyDescriptors (); for (PropertyDescriptor p: ps) {System.out.println (p.getName ()); if (p.getName (). Equals ("name")) {Method setter=p.getWriteMethod (); if (setterworthy null) {setter.setAccessible (true); / / allow access to private attribute setter.invoke (person, "little swallow") / / modify the value of bean field Field field=Person.class.getDeclaredField ("name"); field.setAccessible (true) through reflection mechanism; / / allow access to private field field.set (person, "sss"); Class.forName (className) .newInstance; / / Class.forName (className) .getDeclaredConstructor (String.class) .newInstance ("dawn") with parameters / / get the property Introspector.getBeanInfo (Person.class) .getPropertyDescriptors () / / modify the value of the bean property Person person= (Person) Class.forName (className) .getDeclaredConstructor (String.class) .newInstance ("dawn") through reflection mechanism; PropertyDescriptor [] ps = Introspector.getBeanInfo (Person.class). GetPropertyDescriptors (); for (PropertyDescriptor p: ps) {System.out.println (p.getName ()) If (p.getName (). Equals ("name")) {Method setter=p.getWriteMethod (); if (setterless null) {setter.setAccessible (true); / / allow access to the private attribute setter.invoke (person, "little swallow"); / / modify the value of the bean field Field field=Person.class.getDeclaredField ("name"); field.setAccessible (true); / / allow access to the private field field.set (person, "sss")

Spring provides declarative transaction management

Software decoupling, not hard coding

Jar required by Spring

Dist\ spring.jar

Lib\ jakarta-commons\ commons-logging.jar

If you are using facet programming (AOP), you also need the following jar files

Lib/aspectj/aspectjweaver.jar and aspectjrt.jar lib/cglib/cglib-nodep-2.1_3.jar

If you use annotations from JSR-250, such as @ Resource/@PostConstruct/@PreDestroy, you also need the following jar files

Lib\ j2ee\ common-annotations.jar

Profile beans.xml

Java code

Http://www.springframework.org/schema/beans" beans xmlns= "http://www.springframework.org/schema/beans" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" beans "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> t Tracer beans >

How to start the spring container Java code

ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml"); Person s = (Person) context.getBean ("xx"); ApplicationContext context = new ClassPathXmlApplicationContext ("beans.xml"); Person s = (Person) context.getBean ("xx")

The default bean is instantiated when the container starts and is created only once in the container. The objects in spring always exist in the container, which is a singleton mode.

Table 3.4. Bean scope

Scope description

Singleton one bean definition corresponds to one object instance in each Spring IoC container.

Prototype one bean definition corresponds to multiple object instances.

Request in a HTTP request, a bean definition corresponds to an instance; that is, each HTTP request will have its own bean instance, which is created based on a bean definition. This scope is valid only in web-based Spring ApplicationContext cases.

Session in a HTTP Session, a bean definition corresponds to an instance. This scope is valid only in web-based Spring ApplicationContext cases.

Global session in a global HTTP Session, an bean definition corresponds to an instance. Typically, it works only when using portlet context. This scope is valid only in web-based Spring ApplicationContext cases.

Getting started with Spring-creating a bean using a factory method

Java code

Public class PersonFactory {public static Person createPerson () {return new Person ();} public Person createPerson2 () {return new Person ();} public class PersonFactory {public static Person createPerson () {return new Person ();} public Person createPerson2 () {return new Person ();}

Java code

/ / using static factory method instantiation using instance factory method instantiation / / using static factory method instantiation using instance factory method instantiation

Getting started with Spring-dependency injection

Java code

Public class Person {private Integer id; private String name= "aa"; private IDCard idcard; public IDCard getIdcard () {return idcard;} public void setIdcard (IDCard idcard) {this.idcard = idcard;} public Person (String name) {this.name = name } public Person () {} public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getName () {return name } public void setName (String name) {this.name = name;}} public class IDCard {private String no; public String getNo () {return no;} public void setNo (String no) {this.no = no } public class Person {private Integer id; private String name= "aa"; private IDCard idcard; public IDCard getIdcard () {return idcard;} public void setIdcard (IDCard idcard) {this.idcard = idcard;} public Person (String name) {this.name = name;} public Person () {} public Integer getId () {return id } public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;}} public class IDCard {private String no; public String getNo () {return no;} public void setNo (String no) {this.no = no;}}

* * methods

Java code

The second method

Java code

Configure a null value for the property

Java code

Public class Person {private String name= "ss"; public Person () {} public Person (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name } public void say () {System.out.println ("I said it");}} public class Person {private String name= "ss"; public Person () {} public Person (String name) {this.name = name;} public String getName () {return name;} public void setName (String name) {this.name = name } public void say () {System.out.println ("I said");}}

Initializing the bean executes the say method is equivalent to @ BeforeClass of the test unit

Set dependency injection

Java code

1111 2222 3333 4444 for (String s: p.getLists) {System.out.println (s) } TTT YYY for (String s: p.getSets) {System.out.println (s) } for (String key: p.getMaps () .keySet ()) {System.out.println (key+ "=" + p.getMaps () .get (key)) } Properties is the injection of value1 value2 for (Object key: p.getPropers () .keySet ()) {System.out.println (key+ "=" + p.getPropers () .get (key) } these are all the contents of the article "how to use Spring reflection Technology". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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