学习目标:
Bean
学习内容:
一、Bean的作用域
二、Bean的自动装配
学习时间:
学习产出:
一、Bean的作用域
六种:singleton、prototype、request、session、application、websocket,后两种常常合在一起说。
1、singleton(单例模式):所有创建的实例只有一个,Spring默认为单例模式
<bean id="user1" class="com.study.pojo.User" p:age="20" p:name="张三" scope="singleton"/>
2、prototype(原型模式):每次获取都会产生一个新的对象
<bean id="user1" class="com.study.pojo.User" p:age="20" p:name="张三" scope="prototype"/>
3、request、session、application、websocket只能在web开发中使用
二、Bean的自动装配
自动装配是Spring满足Bean依赖的一种方式
Spring会在上下文中自动寻找,并自动给Bean自动装配属性
Spring中自动装配的方式:
- 在XML中显式的配置
- 在Java中显式配置
- 隐式的自动装配Bean
byName:会自动在容器上下文中查找和自己对象set方法后面的值对应的bean的id(如SetPerson后面的Person),需要保证所有bean的id必须唯一且bean需要和Set后面的值一致
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="dog" class="com.study.pojo.Dog"/>
<!--byName:会自动在容器上下文中查找和自己对象set方法后面的值对应的bean的id(如SetPerson后面的Person )-->
<bean id="person" class="com.study.pojo.Person" autowire="byName">
<property name="name" value="张三"/>
</bean>
byType:会自动在容器上下文查找和自己对象属性类型相同的Bean,如Dog、Cat、Person,byType可以不写id。buType必须保证class唯一,且和Set注入的属性一致
<bean class="com.study.pojo.Cat"/>
<bean class="com.study.pojo.Dog"/>
<!-- <bean id="dog" class="com.study.pojo.Cat"/>-->
<!--byType:会自动在容器上下文查找和自己对象属性类型相同的Bean,如Dog-->
<bean id="person" class="com.study.pojo.Person" autowire="byType">
<property name="name" value="张三"/>
</bean>
自动装配
一、@Autowired
1、导入约束:
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置注解支持-->
<context:annotation-config/>
</beans>
使用@Autowired直接在属性前面加上就行,也可以在Set方法前加上(使用@Autowired
可以不写Set方法,但是这个自动装配属性必须在IOC容器中存在)
public class Person {
private String name;
@Autowired
private Dog dog;
@Autowired
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "com.study.pojo.Person{" +
"name='" + name + '\'' +
", dog=" + dog +
", cat=" + cat +
'}';
}
}
@Autowired中可以设置require=false使属性可以为空(默认require为true)
//如果定义了required=false,说明这个对象可以为空,否则不允许为空
@Autowired(required = false)
private Dog dog;
<bean id="cat" class="com.study.pojo.Cat">
<property name="name" value=""/>
</bean>
@Autowire中可以使用@Nullable设置属性为空
public void Person(@Nullable String name){
this.name=name;
}
当@Autowired自动装配环境比较复杂,自动装配无法通过一个@Autowired完成时,
可以使用@Qualifier(value="")配合@Autowired使用,指定一个唯一的Bean对象注入
private String name;
@Autowired
@Qualifier(value = "cat111")
private Dog dog;
@Autowired
private Cat cat;
public String getName() {
return name;
}
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="dog111" class="com.study.pojo.Dog"/>
<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="person" class="com.study.pojo.Person">
<property name="name" value="张三"/>
</bean>
二、@Resource
@Resource默认通过byName实现,如果byName找不到就会通过byType寻找
导入包:import javax.annotation.Resource;
private String name;
@Resource
private Dog dog;
@Resource
private Cat cat;
public String getName() {
return name;
}
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="dog1" class="com.study.pojo.Dog"/>
<bean id="dog" class="com.study.pojo.Dog"/>
<bean id="person" class="com.study.pojo.Person">
<property name="name" value="张三"/>
</bean>
@Resource也可以指定Bean
private String name;
@Resource(name = "dog1")
private Dog dog;
@Resource
private Cat cat;
<bean id="cat" class="com.study.pojo.Cat"/>
<bean id="dog1" class="com.study.pojo.Dog"/>
<bean id="dog2" class="com.study.pojo.Dog"/>
<bean id="person" class="com.study.pojo.Person">
<property name="name" value="张三"/>
</bean>
@Autowired和@Resource的区别:
- 都是用来自动装配的,都可以放在属性字段上
- @Autowired通过byType的方式实现,而且必须要求这个对象存在
- @Resource默认通过byName方式实现,如果找不到名字,则通过byType方式实现
- 执行顺序不同:@Autowired通过byType的方式,@Resource默认通过byName方式