Spring官方文档:https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core
Spring中文文档:https://www.docs4dev.com/docs/zh/spring-framework/5.1.3.RELEASE/reference/
Spring依赖注入主要分为三种
1.构造器注入
2.set注入(敲重点):
依赖主要是指bean对象的创建要依赖容器,注入是指bean对象的属性由容器来注入
3.其他注入:p和c的命名空间注入
搭建依赖环境
创建Maven项目
1.先写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.kuang</groupId>
<artifactId>Spring-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>spring-01-ioc1</module>
<module>spring-02-hellospring</module>
<module>spring-03-ioc2</module>
<module>spring-04-di</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
</dependencies>
</project>
2.创建两个复杂类型
Student类(其中包括了一些集合,注意集合依赖注入的方式)
package com.kuang.pojo;
import java.awt.*;
import java.util.*;
import java.util.List;
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Address getAddress(){
return address;
}
public void setAddress(Address address){
this.address = address;
}
public String[] getBooks(){
return books;
}
public void setBooks(String[] books){
this.books = books;
}
public List<String> getHobbys(){
return hobbys;
}
public void setHobbys(List<String> hobbys){
this.hobbys = hobbys;
}
public Map<String,String> getCard(){
return card;
}
public void setCard(Map<String,String> card){
this.card = card;
}
public Set<String> getGames(){
return games;
}
public void setGames(Set<String> games){
this.games = games;
}
public String getWife(){
return wife;
}
public void setWife(String wife){
this.wife = wife;
}
public Properties getInfo(){
return info;
}
public void setInfo(Properties info){
this.info = info;
}
@Override
public String toString(){
return "Student{" +
"name='" + name + '\'' +
", adress=" + address.toString() +
",books=" + Arrays.toString(books) +
",hobby=" + hobbys +
",card=" + card +
",games= " + games +
",wife= " + wife + '\'' +
",Info=" + info +
'}';
}
}
Address类
package com.kuang.pojo;
public class Address {
private String address;
public String getAddress(){
return address;
}
public void setAddress(String address){
this.address = address;
}
@Override
public String toString(){
return "Address{"+
"address='" + address + '\''+
'}';
}
}
3.在resource目录下创建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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.kuang.pojo.Address"/>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种普通值注入 value-->
<property name="name" value="秦疆"/>
<!--第二种 bean注入 ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books">
<array>
<value>三国演义</value>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
<!--List注入-->
<property name="hobbys">
<list>
<value>打篮球</value>
<value>游泳</value>
<value>听歌</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="12371823718738"></entry>
<entry key="银行卡" value="123456789"></entry>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
<value>COC</value>
</set>
</property>
<!--Null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">127812</prop>
<prop key="性别">男</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
4.写测试类:
import com.kuang.pojo.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
p命名空间注入 ,可以直接注入属性的值
先去官网导入p命名空间,代码如下
xmlns:p="http://www.springframework.org/schema/p"
导入包后,可以看到p的相关内容 如图3-1
图3-1
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p就相当于property属性,-->
<bean id="user" class="com.kuang.pojo.User" p:name="狗蛋" p:age="99"/>
</beans>
导入c命名空间
xmlns:c="http://www.springframework.org/schema/c"
注意这里导入了c命名空间。但是“c:”报错是因为User类种没有有参构造器
添加有参构造器和无参构造器
有参,无参构造器:参考以下文档
https://blog.csdn.net/weixin_47163086/article/details/120632222
package com.kuang.pojo;
public class User {
private String name;
private int age;
public User(){
}//无参构造器
public User(String name,int age){
this.name=name;
this.age=age;
}//有参构造器
c命名空间构造器,通过构造器注入construct-args
<bean id="user2" class="com.kuang.pojo.User" c:name="二狗子" c:age="1"/>