In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail the example analysis of DI dependency injection in Spring. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
1. What is DI dependency injection?
Spring dynamically provides an object with other objects it needs. This is achieved through DI (Dependency Injection). For example, object A needs to operate the database. In the past, we always had to write code in A to get a Connection object. With spring, we only need to tell spring,A that we need a Connection. As for how and when the Connection is constructed, A does not need to know. When the system is running, spring will make a Connection at the right time, and then inject it into A like an injection, thus completing the control of the relationship between the various objects. A relies on Connection to function properly, and this Connection is injected into A by spring, hence the name of dependency injection. So how does DI work? An important feature after Java 1.3is reflection, which allows programs to dynamically generate objects, execute object methods, and change object properties while running. Spring is injected through reflection.
To put it simply, what is dependency injection is to assign values to attributes (including basic data types and reference data types)
2. Use set method to assign values to attributes.
Step 1: create the project and import the corresponding jar package
Step 2: create the entity class Person
Package com.ys.di; import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set; public class Person {private Long pid; private String pname; private Student students; private List lists; private Set sets; private Map maps; private Properties properties; public Long getPid () {return pid;} public void setPid (Long pid) {this.pid = pid } public String getPname () {return pname;} public void setPname (String pname) {this.pname = pname;} public Student getStudents () {return students;} public void setStudents (Student students) {this.students = students;} public List getLists () {return lists;} public void setLists (List lists) {this.lists = lists } public Set getSets () {return sets;} public void setSets (Set sets) {this.sets = sets;} public Map getMaps () {return maps;} public void setMaps (Map maps) {this.maps = maps;} public Properties getProperties () {return properties;} public void setProperties (Properties properties) {this.properties = properties }}
We see that this entity class includes the reference type Student class, the basic data class, and the collection data type.
Step 3: assign values in applicationContext.xml
1 vae 1 vae p1 p2
Step 4: test
/ / use set method to assign @ Test public void testSet () {/ / 1, start spring container / / 2, fetch data from spring container / / 3, call method ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml") through object; Person person = (Person) context.getBean ("person"); System.out.println (person.getPname ()) / / vae} 3. Use constructor to assign values to attributes
Step 1: add two constructors to the entity class Per'son.java: with and without parameters
/ / default constructor public Person () {} / / parameter constructor public Person (Long pid,Student students) {this.pid = pid; this.students = students;}
Step 2: assign values in applicationContext.xml
Step 3: test
/ / use the constructor to assign @ Test public void testConstrutor () {ApplicationContext context = new ClassPathXmlApplicationContext ("applicationContext.xml"); Person person = (Person) context.getBean ("person_con"); System.out.println (person.getPid ()) / / 1} this is the end of the article on "sample Analysis of DI dependency injection in Spring". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.