目录放在这里太长了,附目录链接大家可以自由选择查看--------Java学习目录
第一篇---->初识Spring
第二篇---->深入SpringIoC容器(一)
第三篇---->深入SpringIoC容器(二)
第四篇---->依赖注入的方式
第五篇---->基于xml装配Bean
第六篇---->基于注解装配Bean
第七篇---->Spring Bean之间的关系
第八篇---->SpringBean的作用域
第九篇---->Spring 加载属性(properties)文件
第十篇---->Spring表达式(SpEL)
第十一篇---->Spring在xml中配置组件扫描
第十二篇—>认识SpringAOP及底层原理
第十三篇—>使用@AspectJ注解开发AOP
第十四篇—>使用xml配置开发AOP
第十五篇—>数据库编程JdbcTemplate
BeanFactory和ApplicationContext
两个接口,其中ApplicationContext是BeanFactory的子接口之一,BeanFactory
是SpringIOC容器所定义的最底层接口,而ApplicationContext是其高级接口之一,并且对BeanFactory功能做了很多扩展,绝大部分情况下,都会使用ApplicationContext作为SpringIOC容器
.存在使用父类类型无法准确获得实例的异常
,比如学生类,它有男学生和女学生两个子类,如果通过学生类的Class就无法得到具体的实例,因为容器无法判断到底要选用哪个实现类ClassPathXmlApplicationContext:从 类路径下加载配置文件,FileSystemXmlApplicationContext: 从文件系统中加载配置文件
于ApplicationContext
,是专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中装载配置文件完成初始化工作.从WebApplicationContext中可以获得ServletContext的引用,整个Web应用上下文对象作为属性放在ServletContext中
,以便于Web应用环境可以访问Spring应用上下文Bean的初始化和依赖注入
在SpringIOC容器中是两大步骤,先初始化才会进行依赖注入
.Resource定位
,这一步是SpringIOC容器根据开发者的配置,进行资源定位,在Spring开发中,通过xml或者注解
都是十分常见的方式,定位的内容是由开发者提供的BeanDefinition的载入
,这个过程就是Spring根据开发者的配置获取对应的POJO,用以生成对应实例的过程BeanDefinition的注册
,将之间载入的POJO往SpringIOC容器中注册,这样,开发人员就可以通过描述从中得到SpringIOC容器的Bean了.Bean就在SpringIOC容器中得到了初始化,但是没有完成依赖注入
.也就是说没有注入其配置的资源给Bean,它现在还不能使用.对于依赖注入,SpringBean还有一个配置选项—lazy-init,其含义就是是否初始化SpringBean.在没有任何配置的情况下,它默认值为default,实际值为false,也就是SpringIOC默认会自动初始化Bean.如果将其设置为true,那么只有当我们使用SpringIOC容器的getBean方法获取Bean实例的时候,它才会进行初始化,完成依赖注入.管理Bean
.对于Bean而言,在容器中存在其生命周期,它的初始化和销毁也需要一个过程,在一些需要自定义的过程中,我们可以插入代码去改变他们的一些行为,以满足特定的需求,这就需要了解SpringBean的生命周期的知识了生命周期主要是为了了解SpringIOC容器初始化和销毁Bean的过程
,熟悉整个生命周期可以方便我们在初始化和销毁的特定流程中加入自定义方法,满足特定需求SpringIOC容器对象也必须是一个ApplicationContext接口的实现类
,那么才会调用这个方法,否则不调用Bean就完成了初始化,那么Bean就生存在SpringIOC容器中了
,我们就可以容器中获取对象了特别说明:
请仔细看上面的生命周期图,大部分接口是针对单个Bean而言的,而BeanPostProcessor接口是针对所有Bean而言,接口DisposableBean是针对SpringIOC容器本身,因此在后面代码中,我将这两个接口单独创建了实现类,我们只需要在SpringIOC容器中(配置文件)以bean的形式进行配置,SpringIOC容器就会自动识别.package com.spring.two;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
public class BeanPostProcessorImpl implements BeanPostProcessor {
// BeanPostProcessor接口中的方法
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("【" + bean.getClass().getSimpleName() + "】调用BeanPostProcessor接口中的postProcessBeforeInitialization方法,对象" + beanName + "开始实例化");
return bean;
}
// BeanPostProcessor接口中的方法
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("【" + bean.getClass().getSimpleName() + "】调用BeanPostProcessor接口中的postProcessAfterInitialization方法,对象" + beanName + "实例化完成");
return bean;
}
}
2) DisposableBean实现类
package com.spring.two;
import org.springframework.beans.factory.DisposableBean;
public class DisposableBeanImpl implements DisposableBean {
//DisposableBean接口的destroy方法
@Override
public void destroy() throws Exception {
System.out.println("调用接口DisposableBean的destroy方法");
}
}
3) Person–一个Bean类
package com.spring.two;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.*;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class Person implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, InitializingBean{
private String name = null;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("【Setter】为Person类的name属性赋值");
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
System.out.println("【Setter】为Person类的id属性赋值");
this.id = id;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", id=" + id +
'}';
}
//自定义初始化方法
public void myinit() {
System.out.println("【" + this.getClass().getSimpleName() + "】执行自定义初始化方法");
}
//自定义销毁方法
public void mydestroy() {
System.out.println("【" + this.getClass().getSimpleName() + "】执行自定义销毁方法");
}
//BeanNameAware接口中方法
@Override
public void setBeanName(String arg0) {
System.out.println("【" + this.getClass().getSimpleName() + "】调用BeanNameAware接口的setBeanName方法");
}
//BeanFactoryAware接口中方法
@Override
public void setBeanFactory(BeanFactory arg0) throws BeansException {
System.out.println("【" + this.getClass().getSimpleName() + "】调用BeanFactoryAware接口的setBeanFactory方法");
}
//ApplicationContextAware接口中方法
//特别强调,必须使用ApplicationContext及其实现类来创建SpringIOC容器,否则即便实现了该方法也不会调用
@Override
public void setApplicationContext(ApplicationContext arg0) throws BeansException {
System.out.println(
"【" + this.getClass().getSimpleName() + "】调用ApplicationContextAware接口的setApplicationContext方法");
}
//InitializingBean接口中方法
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("【" + this.getClass().getSimpleName() + "】调用InitializingBean接口的afterPropertiesSet方法");
}
}
4) 全局配置文件applicationContext.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 http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!--BeanPostProcessor定义 该接口是针对所有Bean而言的 -->
<bean id="beanPostProcessor" class="com.spring.two.BeanPostProcessorImpl"/>
<!--DisposableBean定义 该接口是针对SpringIOC容器本身的-->
<bean id="disposableBean" class="com.spring.two.DisposableBeanImpl"/>
<!-- 自定义bean类,通过init-method,destroy-method指定自定义初始化方法和自定义销毁方法 -->
<bean id="person" class="com.spring.two.Person"
destroy-method="mydestroy" init-method="myinit">
<property name="name" value="root"/>
<property name="id" value="1"/>
</bean>
</beans>
5) 测试类
package com.spring.two;
import com.spring.one.JuiceMaker;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
// 1. 获取SpringIOC容器,这里使用ClassPathXmlApplicationContext来接收
// 原因是该实现类中含有close方法,在手动关闭时,可以调用生命周期中的销毁方法
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 2. 获取Bean实例
Person person =ctx.getBean(Person.class);
// 3. 输出Bean对象
System.out.println("【生命周期内】"+person);
// 4. 关闭IOC容器,调用销毁方法
ctx.close();
}
}