6、依赖注入
6.1、构造器注入
前面已经说过了
6.2、Set方式注入【重点】
-
依赖注入:Set注入!
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中的所有属性,由容器来注入!
-
【环境搭建】
1.复杂类型
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
2.真实测试对象
public class Student {
private String name;
private Address address;
private String[] books;
private List<String> hobbys;
private Map<String,String> card;
private Set<String> game;
private String wife;
private Properties info;
}
3.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="秦老师"/>
</bean>
</beans>
4.测试类
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.getName());
}
}
完善注入信息
<?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.kuang.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="秦老师"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--数组注入,ref-->
<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="6217"/>
</map>
</property>
<!--Set-->
<property name="game">
<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="学号">190201211</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
测试类:
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=[西游记, 水浒传, 红楼梦, 三国演义],
* hobbys=[听歌, 敲代码, 女孩],
* card={身份证=111111222222223333, 银行卡=6217},
* game=[LOL, COC, BOB],
* wife='null',
* info={学号=190201211, 性别=男}
* }
*/
}
}
6.3、拓展方式注入
我们可以使用p命名空间和c命名空间进行注入
官方解释:
使用:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--p命名空间注入,可以直接注入的属性的值:property-->
<bean id="user" class="com.kuang.pojo.User" p:name="秦老师" p:age="18" />
<!--c命名空间注入,可以直接注入的属性的值:construct-args-->
<bean id="user2" class="com.kuang.pojo.User" c:age="20" c:name="狂神"/>
</beans>
测试:
@Test
public void test2(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user", User.class);
System.out.println(user);
}
注意点:p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
6.4、Bean的作用域
1.单例模式(Spring的默认机制)
<bean id="user2" class="com.kuang.pojo.User" c:age="20" c:name="狂神" scope="singleton"/>
2.原型模式:每次从容器中get的时候,都会产生一个新的对象!
<bean id="accountService" class="com.something.DefaultAccountService" scope="prototype"/>
3.其余的request、session、application这些个只能在web开发中使用到!
7、Bean的自动装配
- 自动装配是Spring满足bean依赖的一种方式!
- Spring会在上下文中自动寻找,并自动给bean装配属性!
在Spring中有三种装配的方式
1.在xml中显示的配置
2.在java中显示配置
3.隐式的自动装配bean【重要】
7.1、测试
搭建环境:一个人有两个宠物!
7.2、ByName自动装配
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
-->
<bean id="people" class="com.kuang.pojo.People" autowire="byName">
<property name="name" value="小香蕉呀"/>
</bean>
7.2、ByType自动装配
<!--
byName:会自动在容器上下文中查找,和自己对象set方法后面的值对应的beanid
byType:会自动在容器上下文中查找,和自己对象属性类型相同的bean 但必须保证类型全局唯一
-->
<bean id="people" class="com.kuang.pojo.People" autowire="byType">
<property name="name" value="小香蕉呀"/>
</bean>
小结:
-
byname的时候,需要保证所有bean的id唯一,并且这个bean需要和自动注入的属性的set方法后名字的值一致!
-
bytype的时候,需要保证所有bean的class唯一,并且这个bean需要和自动注入的属性的类型一致!
7.4、使用注解实现自动装配
jdk1.5支持的注解,Spring2.5就支持注解了!
The introduction of annotation-based configuration raised the question of whether this approach is “better” than XML.
要使用注解须知:
1.导入约束 context约束
2.配置注解的支持 context:annotation-config/【重要】
<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
</beans>
@Autowired
直接在属性上使用即可!也可以在set方式上使用!
使用 Autowired 我们可以不用编写Set方法了 , 前提是你这个自动装配的属性在IOC(Spring)容器中存在,且符合名字byName! 如果这样找不到就会启用byType寻找!
科普:
@Nullable 字段标记了这个注解,说明这个字段可以为null;
public @interface Autowired {
boolean required() default true;
}
测试代码
public class People {
//如果显示定义了Autowired的required属性为false,说明这个对象可以为null,否则不允许为空
@Autowired(required = false)
private Cat cat;
@Autowired
private Dog dog;
private String name;
}
如果@Autowired自动装配的环境比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候、我们可以使用@Qualifier(value=“xxx”)去配置@Autowired的使用,制定一个唯一的bean对象注入!
public class People {
@Autowired
private Cat cat;
@Autowired
@Qualifier(value = "dog222")
private Dog dog;
private String name;
}
@Resource注解
public class People {
//先找id一致的,再找类型一致的,实在找不到就会报错,这时候再加一个name来额外指定
@Resource(name = "cat222")
private Cat cat;
@Resource
private Dog dog;
}
小结:
@Resource和@Autowired的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired 通过byType的方式实现,而且必须要求这个对象存在【常用】
- @Resource 默认通过byname方式实现,如果找不到名字,则通过byType实现!如果两个都找不到的情况下就报错【常用】
- 执行顺序不同:@Autowired通过byType的方式实现。@Resource默认通过byname的方式实现。