配置bean基于xml
- 实例化方式
1.默认构造必须提供默认构造,
2.静态工厂 :a.整合其他框架
b.用于生产实例对象,所有方法必须是static(懒汉,恶汉方式)
c.
3.工厂
实现类
public class UserServiceimpl implements UserService {
@Override
public void run() {
System.out.println("大家好我是菜虚昆,喜欢崩跳,rap");
}
public class Mybeanfactory {
/*
* 创建实例
*/
public static UserService creatUserService(){
return new UserServiceimpl();
}
}
2.bean种类
3.作用域
4.生命周期11个过程
-
intantiate bean对象实例化
-
public properties封装属性
-
如果bean实现 BeanNameAware 执行 setBeanName
-
如果Bean实现BeanFactoryAware 或者 ApplicationContextAware设置工厂setBeanFactory或者上下文对象setApplicatinContext
-
如果粗子啊类实现BeanPostProcessor,执行postProcessBeforeinitialization
-
如果Bean实现InitializaingBean执行afterPropertiesSet
-
调用知道初始化方法init
-
如果存在类实现BeanpostProcessor,执行postProcessAfterInitialization
-
执行业务处理
-
如果Bean实现DisposableBean执行destory
-
调用指定销毁方法customerDestory
4.1初始化和销毁
5.属性的依赖注入
1. seet属性注入
bean的xml配置
2p命名空间
3.spel(spring语言表达式)
4.集合注入
属性注入集合
private String[] arraydate;
private List<String> list;
private Set<String> setdata;
private Map<String,String> mapdata;
private Properties propsdata;
1.array
2.list集合
3.set注入
4.map
5.properties属性
装配bean基于注解
在xml中配置好后,spring会进行扫描带有注解的类
<?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-3.2.xsd">
<!-- 注解配置,主键扫描,含有注解的类-->
<context:component-scan base-package="com.wenhe.zhujie"></context:component-scan>
</beans>