一、学习目标
Spring框架中的依赖注入是一种控制反转的实现方式,它允许你将类的依赖关系从类的内部代码中分离出来,并在外部(如配置文件或注解)中指定。这种方式使得代码更加模块化,提高了可维护性和可测试性。
二、依赖注入
1.介绍
在 MyBatis 中,依赖注入(Dependency Injection, DI)的概念主要体现在两个方面:
1.MyBatis 容器对 Mapper 接口的代理对象的注入:
MyBatis 通过解析配置文件和 Mapper XML 文件(或注解),生成 Mapper 接口的代理对象。这些代理对象实现了 Mapper 接口,并能够在运行时动态地执行 SQL 语句。
2.Mapper XML 或注解中参数的注入:
在 Mapper XML 文件或注解中定义的 SQL 语句,可以通过 MyBatis 提供的参数传递机制来注入参数。这些参数可以是单个简单类型、POJO、Map 等。MyBatis 会根据 SQL 语句中定义的参数占位符以及方法签名或注解中定义的参数信息,自动地将调用 Mapper 方法时传递的参数值注入到 SQL 语句中。
使用上一篇博客的项目,结构如下:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lzh</groupId>
<artifactId>spring-01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
</dependencies>
<!--maven 配置默认的JDK版本-->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</project>
2.基于构造器的依赖注入
1.通过无参构造
参考上一篇博客
2.通过有参构造
实体类
public class User {
private String name;
private int age;
public User() {
}
public User(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;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public void show(){
System.out.println(this.toString());
}
}
1.第一种根据index参数下标设置
<bean id="example1" class="com.lzh.pojo.User">
<constructor-arg type="java.lang.String" value="zsy"></constructor-arg>
<constructor-arg type="int" value="20"></constructor-arg>
</bean>
<constructor-arg>
标签:这个标签用于指定构造器参数。在这个例子中,com.lzh.pojo.User
类应该有一个接受两个参数的构造器,第一个参数是String
类型,第二个参数是int
类型。
第一个<constructor-arg>
标签指定了第一个构造器参数的类型(type="java.lang.String"
)和值(value="zsy"
)。这告诉Spring容器,在创建User
类的实例时,应该调用一个接受String
类型参数的构造器,并将这个参数的值设置为"zsy"
。第二个<constructor-arg>
标签指定了第二个构造器参数的类型(type="int"
)和值(value="20"
)。
2.第二种根据参数名字设置
<bean id="example2" class="com.lzh.pojo.User">
<constructor-arg index="0" value="jcx"/>
<constructor-arg index="1" value="21"/>
</bean>
第一个<constructor-arg>
标签通过index="0"
指定了构造器的第一个参数,其值被设置为"jcx"
第二个<constructor-arg>
标签通过index="1"
指定了构造器的第二个参数,其值被设置为"21"
3.第三种根据参数类型设置
<bean id="example3" class="com.lzh.pojo.User">
<constructor-arg name="name" value="tjj"/>
<constructor-arg name="age" value="22"/>
</bean>
第一个<constructor-arg>
标签通过name="name"
指定了构造器的第一个参数(即名为name
的参数),其值被设置为"tjj"
第二个<constructor-arg>
标签通过name="age"
指定了构造器的第二个参数(即名为age
的参数),其值被设置为"22"
。
4.测试
@Test
public void testExample(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); // 创建一个ApplicationContext对象,加载类路径下的beans.xml配置文件
User user = (User) context.getBean("example1"); // 从Spring容器中获取id为"example1"的bean,并将其转换为User类型
//User user = (User) context.getBean("example2"); // 表示从Spring容器中获取id为"example2"的bean,并将其转换为User类型
//User user = (User) context.getBean("example3"); // 表示从Spring容器中获取id为"example3"的bean,并将其转换为User类型
user.show(); // 调用user对象的show方法
}
3.基于Setter的依赖注入
实体类
public class blog {
private String title;
private Address address;
private String[] books;
private List<String> label;
private Map<String,String> card;
private Set<String> other;
private String author;
private Properties info;
public void setTitle(String title) {
this.title = title;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setLabel(List<String> label) {
this.label = label;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public void setOther(Set<String> other) {
this.other = other;
}
public void setAuthor(String author) {
this.author = author;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getTitle() {
return title;
}
public Address getAddress() {
return address;
}
public String[] getBooks() {
return books;
}
public List<String> getLabel() {
return label;
}
public Map<String, String> getCard() {
return card;
}
public Set<String> getOther() {
return other;
}
public String getAuthor() {
return author;
}
public Properties getInfo() {
return info;
}
@Override
public String toString() {
return "blog{" +
"title='" + title + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", label=" + label +
", card=" + card +
", other=" + other +
", author='" + author + '\'' +
", info=" + info +
'}';
}
public blog(String title, Address address, String[] books, List<String> label, Map<String, String> card, Set<String> other, String author, Properties info) {
this.title = title;
this.address = address;
this.books = books;
this.label = label;
this.card = card;
this.other = other;
this.author = author;
this.info = info;
}
public blog() {
}
}
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Address() {
}
public Address(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
1.常量注入
<bean id="title" class="com.lzh.pojo.blog">
<property name="title" value="学习spring的第三天"/>
</bean>
2.bean注入
<bean id="addr" class="com.lzh.pojo.Address">
<property name="address" value="长沙"/>
</bean>
3~8:
<bean id="books" class="com.lzh.pojo.blog">
<property name="books">
<array>
<value>spring官方文档</value>
<value>spring学习日记</value>
</array>
</property>
<property name="label">
<list>
<value>spring</value>
<value>学习</value>
<value>java</value>
</list>
</property>
<property name="card">
<map>
<entry key="csdn" value="20240825"/>
</map>
</property>
<property name="other">
<set>
<value>zxy</value>
<value>zsy</value>
<value>jcx</value>
</set>
</property>
<property name="author"><null/></property>
<property name="info">
<props>
<prop key="点赞">520</prop>
<prop key="访问量">1314</prop>
</props>
</property>
</bean>
3.数组注入
<property name="books">
<array>
<value>spring官方文档</value>
<value>spring学习日记</value>
</array>
</property>
4.list注入
<property name="label">
<list>
<value>spring</value>
<value>学习</value>
<value>java</value>
</list>
</property>
5.map注入
<property name="card">
<map>
<entry key="csdn" value="20240825"/>
</map>
</property>
6.set注入
<property name="other">
<set>
<value>zxy</value>
<value>zsy</value>
<value>jcx</value>
</set>
</property>
7.null注入
<property name="author"><null/></property>
8.properties注入
<property name="info">
<props>
<prop key="点赞">520</prop>
<prop key="访问量">1314</prop>
</props>
</property>
9.测试
@Test
public void test3(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
blog blog=(blog) context.getBean("books");
String[] books=blog.getBooks();
System.out.print("books:");
for (String book:books){
System.out.print("<<"+book+">>\t");
}
System.out.println();
System.out.println("label:"+blog.getLabel());
System.out.println("card:"+blog.getCard());
System.out.println("other:"+blog.getOther());
System.out.println("author:"+blog.getAuthor());
System.out.println("info:"+blog.getInfo());
}