In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of spring dependency injection knowledge points. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
Spring and IoC
IoC: control reversal, handing control over objects manipulated by code to third-party containers, and reversing them to third-party containers. This kind of object-dependent relationship management is called
IoC . IoC is an idea, a concept. There are two famous ways:
DL (Dependency Lookup) depends on lookup, typically JNDI. Java name and service interface.
DI (Dependency Injection) dependency injection. It is the best way of coupling at present. Typical application spring.
DI of spring
Dependency injection, that is, assigning a value to an attribute.
Xml implementation
1) set value injection
The underlying setter method of the property is called for assignment, so the entity class attribute must have a setter method.
Use the property tag to assign a value to the corresponding attribute, or use "ref" to specify a reference type
First, the parameterless constructor is called to generate the object. Then call the setter method to assign a value to the object property. If you define a constructor with parameters, you need to add a constructor with no parameters to make it easier to create objects.
2) structural injection
The underlying call calls the constructor with parameters.
Using the tag, add the value of the attribute.
The use of index in is assigned in the order of the construction parameters (the second type). You can also omit the label of the index (the third type).
In practical use, name-value is still used, which looks more intuitive and clear.
3) Namespace injection
Two kinds:
P Namespace (property) injection: setter assignment
C namespace (constructor) injection: constructor assignment (note that no-parameter constructor is written)
In fact, the underlying calls are still setter and constructor methods.
First, you need to reference the constraint:
Xmlns:p= "http://www.springframework.org/schema/p"xmlns:c=http://www.springframework.org/schema/c
Idea can be introduced automatically
4) Array and set attribute injection
Arrays, collections can hold basic data types and object types; similarly: in map, keys and values can hold basic data types and objects.
In the case, the array has been defined to hold the String
Zhang San, Li Si, Wang Wu, Henan, Hebei, Master and Doctor, 123456@qq.com Civilization Avenue
You can see that tags are used to assign values to arrays; associations are used in list:; set:; map; tags can write multiple associations in Properties
Pay attention to
The difference between list and set sets, repeating elements are allowed in list, but not in set. Set is very close to the concept of "mathematical set"-disorder and non-repeatability.
The key in properties,map cannot be repeated, and if repeated, the corresponding value will be overridden.
When assigning a value to an array, you can write the value in the following shorthand:
Must be a basic data type and a comma in English is required.
5) automatic injection of domain attributes
Domain properties: an object property
Automatic injection of domain attributes: you can inject it either by specifying the type of domain attribute or by specifying the name of the domain attribute.
Properties of the student class:
The private String name;private int age;// domain property private School school;// also requires a setter method
Configuration file:
Autowire= "byName"
Automatic injection by specifying the id of the domain attribute, requiring the domain attribute name to be equal to the id value of the id= "school" as above
If you use autowire= "byType" to inject through the type of a domain property, then there can be only one object of the same type, as long as it satisfies the "is-a" relationship, that is, including the same subclass object and itself.
6) SPEL injection
Mainly use # {}, callable properties, methods.
Example: inject a value from one object into another
If you need a simple judgment, you can use the three-mode operator (comment); if the complex operation can write a method in the called object and call the
Simple implementation of annotations
First configure annotation scanning in the main configuration file:
Set value injection
@ Component () annotation, declaring a component is equivalent to defining a bean in the annotation
@ Value () annotation, which is used to set the injection equivalent of property in bean to add attributes to the bean; setter () with attributes is required
/ / if there is only one parameter "value" in the note, you can omit value and leave only the value of value; @ Component ("mySchool") public class School {@ Value ("No.1") private String sname; public void setSname (String sname) {this.sname = sname;}}
Automatic injection of domain attributes
@ Autowired: automatic injection by type
@ Qualifier () is injected through the name of the domain object
@ Component (value = "student") public class Student {@ Value ("Xiao Liu") private String name; @ Value ("18") private int age; / / byType automatic injection / / @ Autowired / / byName automatic injection / / @ Qualifier ("mySchool") private School school; public void setName (String name) {this.name = name;} public void setAge (int age) {this.age = age } public void setSchool (School school) {this.school = school;}} this is the end of the article on "sample Analysis of spring dependency injection knowledge points". 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.