spring知识点(IOC、AOP、bean生命周期以及源码等等) 重要

前言

@Autowired
@Qualifier
@Resource
在这里插入图片描述

一、组件注册

@Configuration、@Bean() 比较常用

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//告诉spring这是配置类
@Configuration
public class TestConfiguration {

    @Bean("getPerson")
    public Person getPerson() {
        return new Person("莉莉安", 29);
    }
}

@ComponentScan 制定扫描包

/**
 * ComponentScan value 制定扫描的包
 * ComponentScan excludeFilters Filter[] 排除扫描包组件
 * ComponentScan includeFilters Filter[] 包含哪些组件
 */
@ComponentScan(value = {"com.baomidou.mvc.demo02"}, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})
})
public class TestConfiguration02 {

}

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;


/**
 * ComponentScan value 制定扫描的包
 * ComponentScan excludeFilters Filter[] 排除扫描包组件
 * ComponentScan includeFilters Filter[] 包含哪些组件   useDefaultFilters = false 需要禁用默认规则
 */
//@ComponentScan(value = {"com.baomidou.mvc.demo02"}, excludeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})})
//
//
//@ComponentScan(value = {"com.baomidou.mvc.demo02"}, includeFilters = {
//        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})}, useDefaultFilters = false)

@ComponentScan(value = {"com.baomidou.mvc.demo02"}, includeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {BookService.class})} )
public class TestConfiguration02 {

}

@Scope 作用域

@Scope(“singleton ”)

@ComponentScan(value = {"com.baomidou.mvc.demo02"})
public class TestConfiguration03 {
    /**
     *  * @see ConfigurableBeanFactory#scope_prototype
     * 	 * @see ConfigurableBeanFactory#scope_singleton
     * 	 * @see org.springframework.web.context.WebApplicationContext#scope_request
     * 	 * @see org.springframework.web.context.WebApplicationContext#scope_session
     *
     * 	 默认单实例 singleton 每次从容器中拿(map中get同一个值)
     *	 prototype 容器启动时,不会创建对象,每次在调用时才会创建对象
     *   @Lazy 懒加载容器启动不创建调用第一次创建后面 还是同一个对象
     * @return
     */
    @Scope
    @Bean()
    public Person02 getPerson02() {
        System.out.println("给容器添加person");
        return new Person02("莉莉安", 28);
    }
}

在这里插入图片描述

@Scope(“prototype”)

@ComponentScan(value = {"com.baomidou.mvc.demo02"})
public class TestConfiguration03 {
    @Scope("prototype")
    @Bean()
    public Person02 getPerson02() {
        System.out.println("给容器添加person");
        return new Person02("莉莉安", 28);
    }
}

在这里插入图片描述

@Import ,MyImportSelect,@ImportBeanDefinitionRegistrar 导入组件

@Import({Color.class,Red.class,MyImportSelect.class,
MyImportBeanDefinitionRegistrar.class})
public class TestConfiguration04 {
}

public class Color {
}
public class Red {
}

public class MyImportSelect implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{"com.yang.yimall.yimallauthserver.mvc.demo02.Yellow"};
    }
}
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    /**
     * AnnotationMetadata  当前类的注解信息
     * BeanDefinitionRegistry  BeanDefinition的注册信息
     * @param importingClassMetadata
     * @param registry
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry, BeanNameGenerator importBeanNameGenerator) {
        RootBeanDefinition beanDefinition = new RootBeanDefinition(Boy.class);
        registry.registerBeanDefinition("Boy",beanDefinition);
    }
}
  @org.junit.Test
    public void test04() {
        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(TestConfiguration04.class);
        System.out.println("ioc容器启动");
        System.out.println( annotationConfigApplicationContext.getBean(Color.class));
        System.out.println( annotationConfigApplicationContext.getBean(Yellow.class));
        System.out.println( annotationConfigApplicationContext.getBean(Boy.class));
    }

FactoryBean

@Configuration
public class TestConfiguration05 {

    /**
     * Spring提供FactoryBean
     * @return
     */
    @Bean
    ColorFactoryBean factoryBean() {
        return new ColorFactoryBean();
    }

}

二、bean生命周期init destory

在这里插入图片描述

(一)@Bean(initMethod = “init”,destroyMethod = “destroy”)

@ComponentScan(value = {"com.yang.yimall.yimallauthserver.mvc.demo03"})
public class TestConfiguration06 {

    @Bean(initMethod = "init",destroyMethod = "destroy")
    public Car getCar() {
        return new Car();
    }
}

public class Car {
    public Car() {
        System.out.println("Car:ArgsConstructor---------");
    }
    //对象创建并赋值之后调用
    public void init() {
        System.out.println("Car:init---------");
    }
	//容器已处对象之前调用
    public void destroy () {
        System.out.println("Car:destroy ---------");
    }
}

在这里插入图片描述

(二)implements InitializingBean , DisposableBean

@Component
public class Pig implements InitializingBean , DisposableBean {

    public Pig() {
        System.out.println("Pig:ArgsConstructor---------");
    }
    /**
     *对象创建并赋值之后调用
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("Pig:afterPropertiesSet---------");
    }
	容器已处对象之前调用
    @Override
    public void destroy() throws Exception {
        System.out.println("Pig:destroy ---------");
    }
}

(三)@PostConstruct,@PreDestroy

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
@Component
public class Dog {

    public Dog() {
        System.out.println("Dog:ArgsConstructor---------");
    }
    //对象创建并赋值之后调用  JSR250
    @PostConstruct
    public void init() {
        System.out.println("Dog:init---------");
    }
    容器已处对象之前调用
    @PreDestroy
    public void destroy () {
        System.out.println("Dog:destroy ---------");
    }

}

三、bean生命周期

(重要接口)
在这里插入图片描述

在这里插入图片描述


@Component
public class Book implements InitializingBean, DisposableBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    public Book() {
        System.out.println("一、Book:ArgsConstructor---------");
    }

    @PostConstruct
    public void init() {
        System.out.println("四、Book:init---------");
    }

    /**
     * 对象创建并赋值之后调用
     *
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("五、Book:afterPropertiesSet---------");
    }


    @Override
    public void destroy() throws Exception {
        System.out.println("Book:destroy---------");
    }


    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        System.out.println("Book:二、setApplicationContext---------");
        this.applicationContext = applicationContext;
    }
}

@Component
public class MyBeanPostProcessor implements BeanPostProcessor
{

    /**
     * 在对象初始化之前工作
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("三、Book:postProcessBeforeInitialization---------");
        return bean;
    }

    /**
     * 在对象初始化之后工作
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("六、Book:postProcessAfterInitialization---------");
        return bean;
    }
}

四、什么是BeanDefinition

在这里插入图片描述

BeanDefinition中重要注解

在这里插入图片描述

五、什么是BeanFactory

在这里插入图片描述
在这里插入图片描述

BeanFactory的核心接口和实现类

ListableBeanFactory、ConfigurableBeanFactory、AutowireCapableBeanFactory、
AbstractBeanFactory、DeaultListableBeanFactory
DeaultListableBeanFactory最重要:

六、Bean生命周期(二)

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

七、FactoryBean是什么

在这里插入图片描述
在这里插入图片描述

八、ApplicationContext 是什么

在这里插入图片描述

在这里插入图片描述

九、BeanPostProcessor是什么

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

十、前方高能:三级缓存 DefaultSingletonBeanRegistry (重要)类 AbstractBeanFactory、AbstractAutowireCapableBeanFactory

doGetBean->createBean->doCreateBean-> createBeanInstance->(createBeanInstance、populateBean、initializeBean)->(invokeAwareMethods、applyBeanPostProcessorsBeforeInitialization、invokeInitMethods、(afterPropertiesSet)、applyBeanPostProcessorsAfterInitialization)

在这里插入图片描述
在这里插入图片描述

十一、前方高能:getBean( ) bean的创建源码

2.0.2版本的Spring代理不仅没有考虑早期依赖,是不支持循环依赖的!所以,2.0.3为了支持循环依赖,

在这里插入图片描述

doCreateBean bean的生命周期源码

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

十二、前方高能:AOP源码 切入点 AbstractAutoProxyCreator

在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值