Spring02
DI 依赖注入
1.1 构造器注入
在Spring01中记录过了。
1.2 set方法注入 重点
- 依赖注入:Set注入!
- 依赖:bena对象的创建依赖于容器
- 注入:bean对象中的所有属性由容器来注入
public class Address {
private String address;
}
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;
}
<?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.kun.pojo.Address"/>
<bean id="student" class="com.kun.pojo.Student">
<!-- 第一种,普通值注入-->
<property name="name" value="wrk"/>
<!-- 第二种,Bean注入-->
<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="11111111111"/>
<entry key="银行卡" value="22222222222"/>
</map>
</property>
<!-- 第六种,set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!-- 第七种,空值或者null注入-->
<!-- 空值 -->
<!-- <property name="wife" value=""/> -->
<!-- null -->
<property name="wife">
<null />
</property>
<!-- 第八种,properties(配置)注入-->
<property name="info">
<props>
<prop key="driver">123456789</prop>
<prop key="url">男</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
1.3 扩展方法注入
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;
}
}
- p命名空间注入,需要在xml中添加约束
xmlns:p="http://www.springframework.org/schema/p"
可以使用p命名空间进行属性注入
<bean id="user" class="com.kun.pojo.User" p:name="wrk" p:age="25" />
- c命名空间注入,也需要添加约束
xmlns:c="http://www.springframework.org/schema/c"
可以使用c命名空间进行构造器注入
<bean id="user2" class="com.kun.pojo.User" c:name="wrk" c:age="25" />
注意点:p命名空间和c命名空间不能直接使用,需要添加约束。重要
Bean作用域
Scope | Description |
---|---|
singleton | (Default) Scopes a single bean definition to a single object instance for each Spring IoC container. |
prototype | Scopes a single bean definition to any number of object instances. |
request | Scopes a single bean definition to the lifecycle of a single HTTP request. That is, each HTTP request has its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext . |
session | Scopes a single bean definition to the lifecycle of an HTTP Session . Only valid in the context of a web-aware Spring ApplicationContext . |
application | Scopes a single bean definition to the lifecycle of a ServletContext . Only valid in the context of a web-aware Spring ApplicationContext . |
websocket | Scopes a single bean definition to the lifecycle of a WebSocket . Only valid in the context of a web-aware Spring ApplicationContext . |
2.1 Singleton(单例)
Spring默认机制
2.2 Prototype(原型模式)
每次从容器中get都会产生一个新的对象
Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式
- Spring会在上下文中自动寻找,并自动给bean注入属性
在Spring中有三种装配的方式:
- 在xml中显式的配置
- 在java中显式的配置
- 隐式的自动装配重要
public class People {
private Cat cat;
private Dog dog;
private String name;
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<bean id="cat" class="com.kun.pojo.Cat"/>
<bean id="dog" class="com.kun.pojo.Dog"/>
<!--
byName: 会自动在容器上下文中查找,和自己对象set后面的值对应的bean id
byType: 会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.kun.pojo.People" autowire="byName">
<!-- <property name="cat" ref="cat"/>-->
<!-- <property name="dog" ref="dog"/>-->
<property name="name" value="张三"/>
</bean>
小结:
- 使用byName时,需要保证所有bean的id唯一,并且这个bean的id需要与自动注入的属性的set方法的值一致
- byType时,需要保证所有的bean的class是唯一的,并且这个bean需要和自动注入的属性一致