依赖注入(Dependency Injection,DI)
- 依赖:指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入:指Bean对象所依赖的资源 , 由容器来设置和装配 .
- 构造器注入
- set注入 (重点)
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 是 is .
1.pojo类 :
Address
package com.codeyancy.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 + '\'' +
'}';
}
}
Student
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbies;
private Map<String,String> card;
private Set<String> games;
private String wife;
private Properties info;
get,set,toString
2.编写beans.xml
<?xml version="1.0" encoding="UTF8"?>
<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.codeyancy.pojo.Address">
<property name="address" value="河南"/>
</bean>
<bean id="student" class="com.codeyancy.pojo.Student">
<!--第一种:普通注入,value-->
<property name="name" value="张三"/>
<!--Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入-->
<property name="books" >
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>三国演义</value>
</array>
</property>
<!--List注入-->
<property name="hobbies">
<list>
<value>听歌</value>
<value>看电影</value>
<value>敲代码</value>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="111527199909206010"/>
<entry key="银行卡" value="62487788654389"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!--null注入-->
<property name="wife">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="driver">20160515</prop>
<prop key="url">男</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
3.编写测试类(MyTest)
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());
/*运行结果:
* Student{
* name='张三',
* address=Address{address='河南'},
* books=[红楼梦, 西游记, 三国演义],
* hobbies=[听歌, 看电影, 敲代码],
* card={身份证=111527199909206010, 银行卡=62487788654389},
* games=[LOL, COC, BOB],
* wife='null',
* info={password=123456, url=男, driver=20160515, username=root}
* }
*
*
* */
}
拓展注入实现
User.java : 【注意:这里没有有参构造器!】
package com.codeyancy.pojo;
public class User {
private String name;
private int 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 +
'}';
}
}
1、P命名空间注入 : 需要在头文件中加入约束文件
导入约束:xmlns:p="http://www.springframework.org/schema/p"
<!--p命名空间注入:可以直接注入属性的值:property-->
<bean id="user" class="com.codeyancy.pojo.User" p:name="张三" p:age="18"/>
2、C 命名空间注入 : 需要在头文件中加入约束文件
导入约束:xmlns:c="http://www.springframework.org/schema/c"
<!--c命名空间注入:通过构造器注入:construct-args-->
<bean id="user2" class="com.codeyancy.pojo.User" c:name="李四" c:age="20"/>
问题:c命名空间注入时爆红了,刚才我们没有写有参构造!
解决:把有参构造器加上,这里也能知道,c 就是所谓的构造器注入!
注意:把有参构造器加上之后,无参构造也需要加上。
测试代码:
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
//p:id="user" c:id="user2"
User user = (User) context.getBean("user2");
System.out.println(user);
}
Bean的作用域
Spring中的bean默认都是单例的
Bean的自动装配
- 自动装配是使用spring满足bean依赖的一种方法
- spring会在上下文中自动寻找,并自动给bean装配属性。
Spring中bean有三种装配机制,分别是:
- 在xml中显式配置;
- 在java中显式配置;
- 隐式的自动装配bean【重点】
测试环境搭建
- 新建一个项目
- 新建两个实体类,Cat Dog 都有一个叫的方法
Cat
package com.codeyancy.pojo;
public class Cat {
public void shout(){
System.out.println("miao~");
}
}
Dog
package com.codeyancy.pojo;
public class Dog {
public void shout(){
System.out.println("wang~");
}
}
- 新建一个People类
public class People {
private Cat cat;
private Dog dog;
private String name;
get set toString
- 编写Spring配置文件(beans.xml)
<?xml version="1.0" encoding="UTF8"?>
<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="cat" class="com.codeyancy.pojo.Cat"/>
<bean id="dog" class="com.codeyancy.pojo.Dog"/>
<bean id="people" class="com.codeyancy.pojo.People" autowire="byType">
<property name="name" value="张三"/>
<property name="dog" ref="dog"/>
<property name="cat" ref="cat"/>
</bean>
</beans>
- 测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
People people = context.getBean("people", People.class);
people.getCat().shout();
people.getDog().shout();
}
结果:
byName
autowire byName (按名称自动装配)
由于在手动配置xml过程中,常常发生字母缺漏和大小写等错误,而无法对其进行检查,使得开发效率降低。
采用自动装配将避免这些错误,并且使配置简单化。
测试:
- 修改bean配置,增加一个属性 autowire=“byName”
<bean id="cat" class="com.codeyancy.pojo.Cat"/>
<bean id="dog" class="com.codeyancy.pojo.Dog"/>
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id
-->
<bean id="people" class="com.codeyancy.pojo.People" autowire="byName">
<property name="name" value="张三"/>
</bean>
- 再次测试,结果依旧成功输出!
- 我们将 cat 的bean id修改为 catXXX
- 再次测试, 执行时报空指针java.lang.NullPointerException。因为按byName规则找不对应set方法,真正的setCat就没执行,对象就没有初始化,所以调用时就会报空指针错误。
byType
autowire byType (按类型自动装配)
使用autowire byType首先需要保证:同一类型的对象,在spring容器中唯一。如果不唯一,会报不唯一的异常。
- 将user的bean配置修改一下
<bean id="cat" class="com.codeyancy.pojo.Cat"/>
<bean id="dog11" class="com.codeyancy.pojo.Dog"/>
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的bean id
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean
-->
<bean id="people" class="com.codeyancy.pojo.People" autowire="byType">
<property name="name" value="张三"/>
</bean>
- 测试,正常输出
小结:
- byName的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法的值一致
- byType的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致