依赖注入DI
什么是DI依赖注入?
spring动态的向某个对象提供它所需要的其他对象。这一点是通过DI(Dependency Injection,依赖注入)来实现的。比如对象A需要操作数据库,以前我们总是要在A中自己编写代码来获得一个Connection对象,有了 spring我们就只需要告诉spring,A中需要一个Connection,至于这个Connection怎么构造,何时构造,A不需要知道。在系统运行时,spring会在适当的时候制造一个Connection,然后像打针一样,注射到A当中,这样就完成了对各个对象之间关系的控制。A需要依赖 Connection才能正常运行,而这个Connection是由spring注入到A中的,依赖注入的名字就这么来的。那么DI是如何实现的呢? Java 1.3之后一个重要特征是反射(reflection),它允许程序在运行的时候动态的生成对象、执行对象的方法、改变对象的属性,spring就是通过反射来实现注入的。
简单来说什么是依赖注入,就是给属性赋值(包括基本数据类型和引用数据类型)
注入:指Bean对象所依赖的资源,由容器来设置和装配。
构造器注入
- 有参
- 无参
setter注入
要求被注入的属性,必须有set方法。set方法的名字需要规范
set+属性名(属性名字母大写);
1 常量注入
<!--普通字段-->
<property name="name" value="小明"/>
2 Bean注入
<!--引用其他bean使用ref-->
<property name="address" ref="addr"/>
1.3 数组注入
<!--数组的注入-->
<property name="books">
<array>
<value>西游记</value>
<value>水浒传</value>
<value>红楼梦</value>
<value>三国演义</value>
</array>
</property>
4 List注入
<!--List注入-->
<property name="hobbys">
<list>
<value>女孩</value>
<value>代码</value>
<value>电影</value>
<value>音乐</value>
</list>
</property>
5 Map注入
标签:entry
键:使用key
值: 使用value
<!--Map的注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
6 Set注入
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>贪玩蓝月</value>
<value>绝地求生</value>
<value>LOL</value>
</set>
</property>
7 空值注入
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
8 Properties注入
props标签
键使用key
值,在标签中间;
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">201932301</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
p命名空间注入
注意点:需要导入对应的约束文件】
属性的注入
<!--p:property属性,命名空间注入-->
<bean id="user1" class="com.kuang.pojo.User" p:name="小明" p:age="18"/>
c命名空间注入
构造器的注入
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="com.kuang.pojo.User" c:name="小明" c:age="19"/>
Spring就是一个粘合剂,托管所有的对象;
案列
实体类
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;
}
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 +
'}';
}
}
package com.kuang.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 String girlFriend; //null
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 getGirlFriend() {
return girlFriend;
}
public void setGirlFriend(String girlFriend) {
this.girlFriend = girlFriend;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", girlFriend='" + girlFriend + '\'' +
", info=" + info +
'}';
}
}
package com.kuang.pojo;
public class Address {
private String address;
//默认存在无参构造
public void setAddress(String address) {
this.address = address;
}
public String getAddress() {
return address;
}
}
配置文件
<?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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--Address-->
<!--
作用域scope:
Web相关
request
session
singleton:单例模式
prototype: 原型
bean中不填写作用域默认, scope="singleton",单例
单例:在内存中只有一个备份
-->
<bean id="addr" class="com.kuang.pojo.Address">
<property name="address" value="西安"/>
</bean>
<!--Student-->
<bean id="student" class="com.kuang.pojo.Student">
<!--普通字段-->
<property name="name" value="小明"/>
<!--引用其他bean使用ref-->
<property name="address" ref="addr"/>
<!--数组的注入-->
<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>
<value>音乐</value>
</list>
</property>
<!--Map的注入-->
<property name="card">
<map>
<entry key="IdCard" value="666666888888884444"/>
<entry key="银行卡" value="111122223333444"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>王者荣耀</value>
<value>贪玩蓝月</value>
<value>绝地求生</value>
<value>LOL</value>
</set>
</property>
<!--Null空值注入-->
<property name="girlFriend">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">201932301</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
<!--p:property属性,命名空间注入-->
<bean id="user1" class="com.kuang.pojo.User" p:name="小明" p:age="18"/>
<!--c:constructor构造器:命名空间注入-->
<bean id="user2" class="com.kuang.pojo.User" c:name="小明" c:age="19" />
</beans>
测试
package com.kuang.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) context.getBean("user1");
User user1 = (User) context.getBean("user1");
User user2 = (User) context.getBean("user1");
User user3 = (User) context.getBean("user1");
System.out.println(user.hashCode());
System.out.println(user1.hashCode());
System.out.println(user2.hashCode());
System.out.println(user3.hashCode());
}
}