以上笔记来源:
尚硅谷Spring零基础入门到进阶,一套搞定spring6全套视频教程(源码级讲解)https://www.bilibili.com/video/BV1kR4y1b7Qc
目录
[2.1 set方法及构造器方法实现Book对象初始化](#2.1 set方法及构造器方法实现Book对象初始化)
[3.1 使用set方法进行依赖注入](#3.1 使用set方法进行依赖注入)
[3.2 使用构造器方法进行注入](#3.2 使用构造器方法进行注入)
一、引言
依赖注入:
1.类有属性,创建对象过程中,向属性设置值
第一种方式:基于set方法完成
第二种方式:基于构造器完成
二、Java方法初始化过程
2.1 set方法及构造器方法实现Book对象初始化
①生成Book实体类
javascript
package com.atguigu.spring6.iocxml.bean;
public class Book {
private String bname;
private String author;
public Book() {
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String bname, String author) {
this.bname = bname;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", author='" + author + '\'' +
'}';
}
public void run() {
System.out.println("book run...");
}
public static void main(String[] args) {
//set方法注入
Book book=new Book();
book.setBname("java");
book.setAuthor("尚硅谷");
System.out.println(book);
//通过构造器注入
Book book2=new Book("C++","尚硅谷");
System.out.println(book2);
}
}
②运行程序,如图所示
三、Spring注入依赖
3.1 使用set方法进行依赖注入
①生成Book实体类
java
package com.atguigu.spring6.iocxml.bean;
public class Book {
private String bname;
private String author;
public Book() {
}
public String getBname() {
return bname;
}
public void setBname(String bname) {
this.bname = bname;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Book(String bname, String author) {
this.bname = bname;
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"bname='" + bname + '\'' +
", author='" + author + '\'' +
'}';
}
public void run() {
System.out.println("book run...");
}
}
②创建bean-di.xml文件
创建一个名为book的bean,并使用<property></property>标签对bean进行依赖注入
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
配置HelloWorld所对应的bean,即将HelloWorld的对象交给Spring的IOC容器管理
通过bean标签配置IOC容器所管理的bean
属性:
id:设置bean的唯一标识
class:设置bean所对应类的全路径
-->
<bean id="book" class="com.atguigu.spring6.iocxml.bean.Book">
<property name="bname" value="零基础学Java"></property>
<property name="author" value="haozihua"></property>
</bean>
</beans>
③ 创建测试类
java
package com.atguigu.spring6.iocxml.bean;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestBook_di {
@Test
public void testBook(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean-di.xml");
Book book = context.getBean("book", Book.class);
book.run();
System.out.println(book);
}
}
④运行截图如下
3.2 使用构造器方法进行注入
①生成Book实体类(如3.1所示)
②添加XML配置文件
XML
<!--构造方法注入-->
<bean id="bookCon" class="com.atguigu.spring6.iocxml.bean.Book">
<constructor-arg name="bname" value="不想学Java"></constructor-arg>
<constructor-arg name="author" value="hhhhhh"></constructor-arg>
</bean>
③在测试类中创建新的测试方法
java
@Test
public void testBookCon(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean-di.xml");
Book book = context.getBean("bookCon", Book.class);
book.run();
System.out.println(book);
}
④运行结果如图所示
四、总结
本文主要介绍了Spring依赖注入的两种方式,set方法注入和构造方法注入,都需要创建被注入的实体类,之后使用bean的XML文件进行配置属性,注入依赖