依赖注入(DI)
依赖: bean对象的创建依赖于容器!
注入: bean对象中的所有属性,由容器来注入!
注入方式
构造器注入
Set方式注入【重点】
**必须要有getter和setter方法
- pojo.java
@Data
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;
}
- 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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.spring.pojo.Address">
<property name="address" value="北京"/>
</bean>
<bean id="student" class="com.spring.pojo.Student" >
<!--第一种,普通值注入,value-->
<property name="name" value="Kitty Guy"/>
<!--第二种,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="111111222222223333"/>
<entry key="银行卡" value="132123131231231312中"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>LoL</value>
<value>CS:Go</value>
<value>Apex</value>
</set>
</property>
<!--null-->
<property name="wife">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="driver">20190525</prop>
<prop key="ur1">男</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
拓展方式注入
- p命名空间(property) 定义命名空间
xmlns:p="http://www.springframework.org/schema/p"
- c命名空间(constructor) 定义命名空间,使用时需要类含有有参构造器
xmlns:c="http://www.springframework.org/schema/c"
- 样例 pojo
@Data
public class People {
int age;
String name;
public People(int age, String name) {
this.age = age;
this.name = name;
}
public People() {
}
}
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"
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">
<bean id="people1" class="com.spring.pojo.People" p:age="18" p:name="老王"/>
<bean id="people2" class="com.spring.pojo.People" c:age="48" c:name="老李"/>
</beans>
test
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("namespace.xml");
People people1=context.getBean("people1",People.class);
People people2=context.getBean("people2",People.class);
System.out.println(people1);
System.out.println(people2);
//System.out.println(exampleBean1==exampleBean);
}
}
Note
p 命名空间不如标准 XML 格式灵活。例如,用于声明属性引用的格式与以Ref结尾的属性发生冲突,而标准 XML 格式则没有。我们建议您谨慎选择方法,并与团队成员进行交流,以免产生同时使用这三种方法的 XML 文档。
Bean的作用域
Scope | Description |
---|---|
singleton | (默认值)每个 Spring IoC 容器将单个 bean 定义范围限制到单个对象实例。 |
prototype | 将单个 bean 定义的作用域限定为任意数量的对象实例。每次context.getBean() 都会获得新的对象 |
request | 将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期;也就是说,每个 HTTP 请求都有一个自己的 bean 实例,它是在单个 bean 定义的后面创建的。仅在可感知网络的 Spring ApplicationContext 中有效。 |
session | 将单个 bean 定义的范围限定为 HTTP Session 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
globalSession | 将单个 bean 定义的作用域限定为全局 HTTP Session 的生命周期。通常仅在 Portlet 上下文中使用时才有效。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
application | 将单个 bean 定义的范围限定为ServletContext 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |
websocket | 将单个 bean 定义的范围限定为WebSocket 的生命周期。仅在可感知网络的 Spring ApplicationContext 上下文中有效。 |