Bean的生命周期

初始化init 和 销毁destory

对象创建完成并完成赋值后,调用init方法

@Bean中配置initMethod和destoryMethod

@Component
public class Apple {
    private String name;

    public void initApple(){
        System.out.println("initApple初始化方法");
    }
    public void destoryApple(){
    System.out.println("destoryApple销毁方法");
    }
}

@Configuration
public class SpringConfigC {
    @Bean(initMethod ="initApple",destroyMethod = "destoryApple")
    public Apple apple(){
        return new Apple();
    }
}

组件实现接口IntializingBean,DisposableBean

@Component
public class Orange implements InitializingBean,DisposableBean {
    private String name;
    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

@PostConstruct @PreDestory

满足JSR250规范的java注解
@Component
public class Apple {
    private String name;

    @PostConstruct
    public void initApple(){
        System.out.println("initApple初始化方法");
    }

    @PreDestory
    public void destoryApple(){
        System.out.println("destoryApple销毁方法");
    }
}

实现接口 BeanPostProcessor bean后置处理器

初始化前后进行处理
BeanPostProcessor (spring5内的源码)
// 包含默认的实现方法
// BeanPostProcessor源码
public interface BeanPostProcessor {

    @Nullable
    default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Nullable
    default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
自定义BeanPostProcessor的实现类
//添加@Component注解,交给IOC管理
@Component
public class MyBeanPostProcessor implements BeanPostProcessor{
    Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("bean初始化之前");
        return bean;
    }

    Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("bean初始化之后");
        return bean;
    }
}
BeanPostProcessor在Spring中的原理和底层使用
  1. polulateBean(beanname,mbd,instanceWrapper);//给bean属性赋值
//大概过程
{
    applyBeanPostProcessBeforeInitialization(...);
    invokeInitMethod(...);
    applyBeanPostProcessAfterInitialization(...);
}

Bean属性赋值

@Value @PropertySource

1.基本数据类型
2.SpEL表达式 #{}
3.取配置文件值 ${} [properties]运行环境变量里的值
# 配置文件
# 配置文件 ghApp.properties (位于resources开发目录下
animal.type=dog
env.name=golphin
// 组件bean
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Animal {
    @Value("狗")
    private String name;

    @Value("${animal.type}")
    private String type;

    @Value("#{30-4}")
    private int age;
}

// 配置类
@Configuration
@PropertySource(value={"classpath:/ghApp.properties"})
@Import({Animal.class})
public class SpringConfigD {
}

//测试类
public class Test{
    // 测试类内的方法
    @Test
    public void test5(){
        AnnotationConfigApplicationContext applicationContext=new AnnotationConfigApplicationContext(SpringConfigD.class);
        Animal animal = (Animal) applicationContext.getBean("spring.ioc.pojo.Animal");
        System.out.println(animal);
        //env 获取运行时变量
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String s = environment.getProperty("env.name");
        System.out.println(s);
    }
}

Golphin
10 声望1 粉丝