如果我们通过 XML 文件仅注入相同 class 的原始值,那么我们可以说我们在 spring 中进行了依赖注入吗?
if we injects only primitive values of the same class through XML file then can we say we did the dependency injection in spring?
我根据 spring 的依赖引用了一些文档 injection.In 其中一些我发现如果我们使用 XML 文件插入相同 class 的原始值那么它是dependency injection.Is 对还是错呢?
或者如果我们通过 XML 文件插入其他 class 依赖项(我们插入其他 class 的引用)是依赖项 injection.Or 都是正确的吗?
Student.java
package simple;
public class Student {
private int studentId;
private int studentAge;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
}
Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="simple.Student" id="student">
<property name="studentId" value="1" />
<property name="studentAge" value="20" />
</bean>
这是不是依赖注入?
这是一个天真的例子,但你可以声称这是一个依赖注入。
A dependency is an object that can be used (a service). An injection
is the passing of a dependency to a dependent object (a client) that
would use it. The service is made part of the client's state.[1]
Passing the service to the client, rather than allowing a client to
build or find the service, is the fundamental requirement of the
pattern. [wikipedia]
所以你的注射器在这种情况下 Spring 框架通过 xml bean 定义提供学生 ID 和年龄。
在我看来,您的示例不会帮助您更好地理解依赖注入概念。
我来解释一下:
据我所知,依赖注入应该主要用于创建服务(通常是单例而不是原型bean的实例)服务依赖可以从外部注入。
通过spring创建原型不是一个常见的用例(我现在将省略解释)。
更好的示例是 StudentDAO(DAO - 数据访问对象),它允许通过 ID 查找所有学生或学生。依赖于 StudentDAO.
的 StudentFinder
假设您有两种类型的存储库,用于存储基于学生的文件和数据库。
所以你要为你的 DAO 定义一个接口。
public interface StudentDAO {
List<Student> findAll();
Student find(String studentId);
}
还有两个实现:
文件
public class StudentFileDAO implements StudentDAO {}
JDBC
public class StudentJdbcDAO implements StudentDAO {}
依赖于 DAO 的查找器:
public class StudentFinder {
private StudentDAO studentDAO;
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public List<Student> findAll() {
return studentDAO.findAll();
}
public Student find(String studentId) {
return studentDAO.find(studentId);
}
}
现在,当您想要定义要使用的 DAO 类型时,您需要将 Beans.xml 设置为
<bean class="simple.StudentFinder" id="studentFinder">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
<bean class="simple.StudentJdbcDAO" id="studentDAO ">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
或
<bean class="simple.StudentFinder" id="studentFinder">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
<bean class="simple.StudentFileDAO" id="studentDAO ">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
(请注意,spring 配置文件在这里会更好,但让我们保持这种方式,这样示例会很简单。)
根据您将随系统提供的 bean.xml 来定义您的系统使用情况 (jdbc/file)。因此,回到维基百科的定义,客户端 (studentFinder) 会构建或查找它在 Beans.xml
中定义为状态的一部分的服务 (DAO)
我根据 spring 的依赖引用了一些文档 injection.In 其中一些我发现如果我们使用 XML 文件插入相同 class 的原始值那么它是dependency injection.Is 对还是错呢? 或者如果我们通过 XML 文件插入其他 class 依赖项(我们插入其他 class 的引用)是依赖项 injection.Or 都是正确的吗?
Student.java
package simple;
public class Student {
private int studentId;
private int studentAge;
public int getStudentId() {
return studentId;
}
public void setStudentId(int studentId) {
this.studentId = studentId;
}
public int getStudentAge() {
return studentAge;
}
public void setStudentAge(int studentAge) {
this.studentAge = studentAge;
}
}
Beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="simple.Student" id="student">
<property name="studentId" value="1" />
<property name="studentAge" value="20" />
</bean>
这是不是依赖注入?
这是一个天真的例子,但你可以声称这是一个依赖注入。
A dependency is an object that can be used (a service). An injection is the passing of a dependency to a dependent object (a client) that would use it. The service is made part of the client's state.[1] Passing the service to the client, rather than allowing a client to build or find the service, is the fundamental requirement of the pattern. [wikipedia]
所以你的注射器在这种情况下 Spring 框架通过 xml bean 定义提供学生 ID 和年龄。
在我看来,您的示例不会帮助您更好地理解依赖注入概念。
我来解释一下: 据我所知,依赖注入应该主要用于创建服务(通常是单例而不是原型bean的实例)服务依赖可以从外部注入。
通过spring创建原型不是一个常见的用例(我现在将省略解释)。
更好的示例是 StudentDAO(DAO - 数据访问对象),它允许通过 ID 查找所有学生或学生。依赖于 StudentDAO.
的 StudentFinder假设您有两种类型的存储库,用于存储基于学生的文件和数据库。
所以你要为你的 DAO 定义一个接口。
public interface StudentDAO {
List<Student> findAll();
Student find(String studentId);
}
还有两个实现:
文件
public class StudentFileDAO implements StudentDAO {}
JDBC
public class StudentJdbcDAO implements StudentDAO {}
依赖于 DAO 的查找器:
public class StudentFinder {
private StudentDAO studentDAO;
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public List<Student> findAll() {
return studentDAO.findAll();
}
public Student find(String studentId) {
return studentDAO.find(studentId);
}
}
现在,当您想要定义要使用的 DAO 类型时,您需要将 Beans.xml 设置为
<bean class="simple.StudentFinder" id="studentFinder">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
<bean class="simple.StudentJdbcDAO" id="studentDAO ">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
或
<bean class="simple.StudentFinder" id="studentFinder">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
<bean class="simple.StudentFileDAO" id="studentDAO ">
<property name=”studentDAO" ref="studentDAO"/>
</bean>
(请注意,spring 配置文件在这里会更好,但让我们保持这种方式,这样示例会很简单。)
根据您将随系统提供的 bean.xml 来定义您的系统使用情况 (jdbc/file)。因此,回到维基百科的定义,客户端 (studentFinder) 会构建或查找它在 Beans.xml
中定义为状态的一部分的服务 (DAO)