文章目录
把对象创建交给spring管理
spring对bean的管理细节
一、创建bean的三种方式
一、使用默认构造函数创建
在spring的配置文件中使用bean标签,只配以id和class属性。
采用的时默认构造函数构造对象,若对象类中没有默认构造函数(如不写构造函数则默认有默认的构造函数;如写了带参的构造函数而不写默认构造函数则没有默认构造函数),则报错。
<bean id="accountService" class="com.yonmin.service.impl.AccountServiceImpl"></bean>
二、使用某个类中的方法创建对象,并存入spring容器
<!-- 先创建工厂的bean对象 -->
<bean id="factoryService" class="com.yonmin.factory.InstanceFactory"></bean>
<!-- 再调用工厂bean对象的非静态方法 -->
<bean factory-bean="factoryService" factory-method="getAccountService"></bean>
三、使用工厂中的静态方法创建对象,并存入spring容器
<!-- 第三种方法:只有静态方法可以这样配置 -->
<bean id="staticFactory" class="com.yonmin.factory.StaticFactory" factory-method="getAccountService"></bean>
二、bean对象的作用范围
bean标签的scope
属性
作用:用于指定bean的作用范围
取值:
- singleton:单例的(默认值)
- prototype:多例的
- request:作用于web应用的请求范围
- session:作用于web应用的会话范围
- global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
三、bean对象的生命周期
-
单例对象:
出生:容器创建时
活着:容器还在
死亡:容器销毁
总结:单例对象的生命周期和容器相同 -
多例对象:
出生:当我们使用对象时,创建
活着:对象只要是在使用过程中就一直活着
死亡:对象被Java的垃圾回收器回收
spring中的依赖注入
能注入的数据:有三类
- 基本类型和String
- 其他bean类型(在配置文件中或者注解配置过的bean)
- 复杂bean类型/集合类型
注入的方式:有三种
- 使用构造函数提供
- 使用set方法提供
- 使用注解提供
一、构造函数注入(除非必须,一般不用)
-
使用的标签:
constructor-arg
-
标签出现的位置:bean标签内部
-
标签的属性:
type
:按参数的类型赋值index
:按位置找参数name
:按参数名称赋值(常用)
以上三个用于指定给构造函数中的哪个参数赋值,以下两个决定所赋的值value
:给基本类型和String类型等非对象参数赋值ref
:给其他指定的bean类型赋值。用于引用关联的bean对象,例如日期
-
优势
在获取bean对象时,注入数据是必须的,否则对象无法创建成功 -
弊端
改变了bean对象的实例化方式,使我们在创建对象时如果用不到也必须创建对象
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
// 构造函数,用于构造函数注入数据
public AccountServiceImpl(String name,Integer age,Date birthday){
this.age = age;
this.birthday = birthday;
this.name = name;
}
}
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
<bean id="accountService" class="com.yonmin.service.impl.AccountServiceImpl">
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
<constructor-arg name="name" value="YonminMa"></constructor-arg>
</bean>
二、set方法注入
-
标签:
property
-
位置:bean标签的内部
-
标签的属性:
name
:找注入时的set方法名称,即成员属性,而不是成员变量value
:给基本类型和String类型等非对象参数赋值ref
:给其他指定的bean类型赋值。用于引用关联的bean对象,例如日期
-
优势
创建对象时没有明确的限制,可以直接使用默认的构造函数 -
弊端
如果某个成员必须有值,则获取对象时set方法可能没有使用
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<bean id="now" class="java.util.Date"></bean>
<bean id="accountService" class="com.yonmin.service.impl.AccountServiceImpl">
<property name="age" value="18"></property>
<property name="birthday" ref="now"></property>
<property name="name" value="YonminMa"></property>
</bean>
复杂类型的注入
-
标签:
- 用于给List结构集合注入的标签:
list
array
set
- 用于给Map结构集合注入的标签:
map
props
结构相同,可以互换
- 用于给List结构集合注入的标签:
-
位置
property
标签的内部
public class AccountServiceImpl implements IAccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,Object> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, Object> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
@Override
public void saveAccount() {
System.out.println("myStrs" + Arrays.toString(myStrs));
System.out.println("myMap" + myMap);
System.out.println("myList" + myList);
System.out.println("myProps" + myProps);
System.out.println("mySet" + mySet);
}
}
<bean id="accountService" class="com.yonmin.service.impl.AccountServiceImpl">
<property name="myStrs">
<array>
<value>AAA</value>
<value>BBB</value>
</array>
</property>
<property name="myList">
<list>
<value>AAA</value>
<value>BBB</value>
</list>
</property>
<property name="mySet">
<set>
<value>AAA</value>
<value>BBB</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="testA" value="AAA"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="testA">AAA</prop>
<prop key="testB">BBB</prop>
</props>
</property>
</bean>
使用注解
首先在bean.xml
中告诉spring在创建容器时需要扫描的包
<!-- 告知spring在创建容器时要扫描的包 -->
<context:annotation-config/>
<context:component-scan base-package="com.yonmin"></context:component-scan>
然后就可以在包中的Java类中添加注释
一、用于创建对象的注解
它的作用和在xml中编写的bean标签作用相同
-
@Component
:- 作用:把当前类对象存入spring容器中
- 属性:
value:指定bean的id,value可省略。默认为当前类名首字母小写
-
@Controller
:一般用于表现层 -
@Service
:一般用于业务层 -
@Respository
:一般用于持久层
以上三个注解的作用和属性与@Component
注解一模一样,只是规定注解的位置不一样
@Service("accountService")
// 或者@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
// ...
}
二、用于注入数据的注解
和在xml中bean标签中的<property>
标签一样
-
@Autowired
:- 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的数据类型匹配,就可以注入成功
如果ioc容器中没有匹配的,则报错
如果ioc容器中有多个数据类型匹配的bean时,变量名称和bean的id相同的bean匹配,如果没有匹配的,则失败 - 出现位置:
可以变量上,也可以在方法上 - 细节:
在使用注解注入时,set方法不是必须的
- 作用:自动按照类型注入,只要容器中有唯一的一个bean对象类型和要注入的数据类型匹配,就可以注入成功
-
@Quarifier
:必须和Autowired一起使用- 作用:在按照类中注入的基础上再按照名称注入。它在给类成员注入时不能单独使用。但是在给方法参数注入时可以
- 属性:
value:按照bean的id注入
-
@Resource
- 作用:按照bean的id注入
- 属性:
name:作用和value一样,但是不可以省略了
以上三个注解只能注入其它bean类型的数据,但是不能注入其他基本类型
-
@Value
- 作用:用于注入其他基本类型和String类型的数据
- 属性:
value:用于指定数据的值。它可以使用spring中的spEL(也就是spring的el表达式)
spEL的写法:${表达式}
public class AccountServiceImpl implements IAccountService { // @Autowired:在容器中找到id为accountDao1的bean对象进行注入 // @Qualifier("accountDao2"):在容器中找到id为accountDao2的bean对象进行注入 @Resource(name="accountDao3") // 在容器中找到id为accountDao3的bean对象进行注入 private IAccountDao accountDao1; }
三、用于改变作用范围的注解
和<bean>
中的scope属性一样
@Scope
- 作用:用于指定bean的作用范围
- 属性:
value:指定范围的取值,通常有singleton,prototype
@Scope("singleton")
public class AccountServiceImpl implements IAccountService {
// ...
}
四、和生命周期相关的注解
和bean标签中的init-method和destroy-method作用一样
@PostConstruct
作用:用于指定初始化方法@PreDestory
作用:用于指定销毁方法
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@PostConstruct
public void init(){
System.out.println("初始化成功");
}
@PreDestroy
public void destory(){
System.out.println("销毁了");
}
}