Spring依赖注入
依赖注入:
- 依赖:所有对象的创建依赖spring容器。
- 注入:对象的属性值由spring容器注入。
1.构造器注入
有参构造方法传参的方式注入。
1.1 实体类:
package com.cj.pojo;
public class User2 {
private int id;
private String name;
public User2(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User2{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
1.2配置注入文件:
<?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="user2" class="com.cj.pojo.User2">
<constructor-arg index="0" value="0"/>
<constructor-arg index="1" value="wcj"/>
</bean>
<!-- 有参构造初始化对象方式二:使用参数类型自动匹配,不建议使用-->
<bean id="user2" class="com.cj.pojo.User2">
<constructor-arg type="int" value="1"/>
<constructor-arg type="String" value="wcj1"/>
</bean>
<!-- 有参构造初始化对象方式三:使用参数名-->
<bean id="user2" class="com.cj.pojo.User2">
<constructor-arg name="id" value="2"/>
<constructor-arg name="name" value="wcj2"/>
</bean>
</beans>
2.set方法注入
2.1实体类
Student类:
package com.cj.pojo;
import java.util.*;
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 Properties info;
private String wife;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", info=" + info +
", wife='" + wife + '\'' +
'}';
}
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 Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
}
Address类:
package com.cj.pojo;
public class Address {
private String address;
@Override
public String toString() {
return '\''+ address + '\'' ;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.2配置注入文件:
<?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.cj.pojo.Address">
<property name="address" value="中国-中南海"/>
</bean>
<bean id="student" class="com.cj.pojo.Student">
<!-- 普通值注入-->
<property name="name" value="文长江"/>
<!-- Bean注入-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books">
<array>
<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="1111111"/>
<entry key="学号" value="201732344"/>
</map>
</property>
<!-- Set注入-->
<property name="games">
<set>
<value>csgo</value>
<value>LOL</value>
<value>PUBG</value>
</set>
</property>
<!-- null注入,注意""是无法替代null的-->
<property name="wife">
<null/>
</property>
<!-- properties注入-->
<property name="info">
<props>
<prop key="年龄">18</prop>
<prop key="性别">男</prop>
<prop key="成绩">合格</prop>
</props>
</property>
</bean>
</beans>
2.3测试类:
import com.cj.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
2.4输出结果:
Student{name='长江', address='中国-中南海', books=[红楼梦, 西游记, 三国演义], hobbys=[听歌, 打游戏, 看书], card={身份证=1111111, 学号=201732344}, games=[csgo, LOL, PUBG], info={性别=男, 成绩=合格, 年龄=18}, wife='null'}
3.其他方式注入
3.1 p命名空间注入
本质是通过set注入,其实p相当于<property>标签
<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">
<bean id="user0" class="com.cj.pojo.User" p:name="某人" p:age="18"/>
</beans>
3.2 c命名空间注入
本质是通过构造器注入,必须要有无参构造器和有参构造器。其实p相当于<constructor-arg>标签
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user1" class="com.cj.pojo.User" c:name="长江" c:age="18"/>
</beans>