Home > Java > javaTutorial > How to implement dependency injection in java Spring

How to implement dependency injection in java Spring

王林
Release: 2019-11-25 17:24:36
forward
3117 people have browsed it

How to implement dependency injection in java Spring

Generally speaking, there are three ways of dependency injection in Spring:

① Assignment through set

②Assign value through constructor

③Assign value through p tag

Recommended java related video tutorials: java video

Let’s expand on the discussion a little bit:

1. Assign value through set method

<bean id="teacher"  class="org.szh.entity.Teacher" p:age="24" p:name="lq">
<property name="name" value="zs"></property>
<property name="age" value="23"></property>
</bean>
Copy after login

Mainly use containment tag To pass the value

The name here is the variable name declared in the Teacher class, and the value is the value you want to assign to this variable.

Teacher class:

public class Teacher {
 private String name;
 private int age;
 
 public Teacher() {
}
 
public Teacher(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Copy after login

2. Assign value through constructor

<bean id="teacher"  class="org.szh.entity.Teacher">
<constructor-arg  value="zs" ></constructor-arg>
<constructor-arg  value="24" ></constructor-arg>
</bean>
Copy after login

Note that there must be a corresponding constructor method in the above Teacher class.

If there are many values ​​in the parameter list in the constructor, it will be very tiring for us to write them one by one. At this time, there are several ways to help us accurately assign values ​​to variables:

1. Use index (subscript value)

2.name (name of declared variable)

3.type (type of declared variable) // The type declared in the parameter list of the constructor is required You can pass the value if it is inconsistent

The specific format is as follows:

<constructor-arg  value="zs" 你要用来做辨认的属性(index/name/type)="..."></constructor-arg>
<constructor-arg  value="24" 你要用来做辨认的属性(index/name/type)="..."></constructor-arg>
Copy after login

There is another point to note here: if you want to pass a reference within the tag Type, you cannot use value, you have to use ref:

<constructor-arg  ref="teacher"></constructor-arg>
Copy after login

3. Assign value through p tag

You need to pay attention here, if your compiler does not If you install sts, you need to add this sentence to the configuration statement of your applicationContext.xml:

xmlns:p="http://www.springframework.org/schema/p"
Copy after login

If you have installed sts, just check it directly in Namespaces:

How to implement dependency injection in java Spring

The specific writing method is as follows:

<bean id="teacher"  class="org.szh.entity.Teacher" p:age="24" p:name="lq">
</bean>
Copy after login

Just add p:xxx="xxx" in the tag.

Recommended java related articles and tutorials: java introductory tutorial

The above is the detailed content of How to implement dependency injection in java Spring. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template