![]() What is commonly used is setter Injection mode, Both can be used together if necessary ![]() If the parameters injected during the creation of the object are optional, You can use setter Inject If the object must be injected with the specified parameters during the creation process, Then you can use the constructor to inject rows, Because use setter Injection has the probability that failure to inject results in null The object appears Create employee class Employee And department Dept, And add a parametric construction method ( The key !) /* The employee class Employee*/public class Employee ģ、. Here is an example of how to use constructor injection Example 1ġ. Stay Use within elements Elements, Assign a value to an attribute in the constructor ,Bean How many parameters are there in the constructor of, Just how many Elements. Stay Spring Defined in the configuration file of Bean ![]() Using the constructor to implement attribute injection, the general steps are as follows :Īdd a parameterized constructor to the class, Each parameter in the constructor represents an attribute to be injected Let's first introduce Constructor Injection :Spring By calling Construction method Assign values to member variables and Child elements, Used to directly specify constant values. and Subelement cable of, Used to specify by reference bean example, namely In the element id or name attribute. Of this element name Property is used to specify Bean Class ( Member variable name ). Of this element index Property specifies the index of the construction parameter ( from 0 Start ),type Property specifies the type of construction parameter (type Or not ,spring Automatically identify the type of parameter ) The following table shows attribute injection bean Common elements in tags Element name DescriptionĬonstructor Injection. ), It can also be a Reference data type ( Mainly objects ), Or a aggregate (list、map、set etc. The injected attribute can be General attribute ( Basic data types and String etc. Spring Attribute injection (DI Dependency injection ) There are two ways :s etter Inject, Constructor Injection. and 、 How to select the injection method Attribute injection concept
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |