mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2024-12-23 05:00:25 +08:00
feat(youlai-laboratory-spring): 添加bean生命周期中初始化相关的aware接口回调以及容器开始和结束的回调接口
添加bean生命周期中初始化相关的aware接口回调以及容器开始和结束的回调接口
This commit is contained in:
parent
210378e04d
commit
2caf105e71
@ -0,0 +1,20 @@
|
||||
package com.youlai.laboratory.spring.bean;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.FactoryBean;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
|
||||
/**
|
||||
* 说明描述
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 21:21
|
||||
*/
|
||||
public class IBeanPostProcessor implements BeanPostProcessor {
|
||||
@Override
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
System.out.println("初始化后,开始代理对象");
|
||||
return bean;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.youlai.laboratory.spring.bean;
|
||||
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
|
||||
import java.sql.SQLOutput;
|
||||
|
||||
/**
|
||||
* 说明描述
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 20:52
|
||||
*/
|
||||
public class IInitializingBean implements InitializingBean {
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
System.out.println("初始化bean---InitializingBean");
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.youlai.laboratory.spring.bean;
|
||||
|
||||
import org.springframework.context.Lifecycle;
|
||||
|
||||
/**
|
||||
* 容器生命周期回调,必须容器显示调用strat方法启动
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 16:06
|
||||
*/
|
||||
public class ILifecycle implements Lifecycle {
|
||||
|
||||
boolean isRunning;
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
isRunning = true;
|
||||
System.out.println("容器开始启动");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning = false;
|
||||
System.out.println("容器结束");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.youlai.laboratory.spring.bean;
|
||||
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.context.SmartLifecycle}继承了Lifecycle和Phased
|
||||
* 相比Lifecycle多了一个优先级功能,并且可以控制是否自动调用
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 16:34
|
||||
*/
|
||||
public class ISmartLifecycle1 implements SmartLifecycle {
|
||||
boolean isRunning;
|
||||
@Override
|
||||
public void start() {
|
||||
isRunning = true;
|
||||
System.out.println("ISmartLifecycle1启动");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning = false;
|
||||
System.out.println("ISmartLifecycle1结束");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
System.out.println("容器停止时调用的方法");
|
||||
stop();
|
||||
callback.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
System.out.println("值越大优先级越低,默认最低");
|
||||
return 1;
|
||||
}
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
package com.youlai.laboratory.spring.bean;
|
||||
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
|
||||
/**
|
||||
* {@link org.springframework.context.SmartLifecycle}继承了Lifecycle和Phased
|
||||
* 相比Lifecycle多了一个优先级功能,并且可以控制是否自动调用
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 16:34
|
||||
*/
|
||||
public class ISmartLifecycle2 implements SmartLifecycle {
|
||||
boolean isRunning;
|
||||
@Override
|
||||
public void start() {
|
||||
isRunning = true;
|
||||
System.out.println("ISmartLifecycle2启动");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
isRunning = false;
|
||||
System.out.println("ISmartLifecycle2结束");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRunning() {
|
||||
return isRunning;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean isAutoStartup() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(Runnable callback) {
|
||||
System.out.println("容器停止时调用的方法");
|
||||
stop();
|
||||
callback.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getPhase() {
|
||||
System.out.println("值越大优先级越低,默认最低");
|
||||
return 2;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.ApplicationStartupAware;
|
||||
|
||||
/**
|
||||
* 获取ApplicationContext
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 20:36
|
||||
*/
|
||||
public class IApplicationContextAware implements ApplicationContextAware {
|
||||
private ApplicationContext applicationContext;
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
System.out.println("\n--------------------");
|
||||
System.out.println("ApplicationContext:"+applicationContext);
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.context.ApplicationStartupAware;
|
||||
import org.springframework.core.metrics.ApplicationStartup;
|
||||
|
||||
/**
|
||||
* 获取ApplicationStartup
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 20:35
|
||||
*/
|
||||
public class IApplicationStartupAware implements ApplicationStartupAware {
|
||||
|
||||
private ApplicationStartup applicationStartup;
|
||||
@Override
|
||||
public void setApplicationStartup(ApplicationStartup applicationStartup) {
|
||||
this.applicationStartup = applicationStartup;
|
||||
System.out.println("\n--------------------");
|
||||
System.out.println("ApplicationStartupAware:"+applicationStartup);
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||
|
||||
/**
|
||||
* 获取bean的类加载器
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 19:30
|
||||
*/
|
||||
public class IBeanClassLoaderAware implements BeanClassLoaderAware {
|
||||
|
||||
private ClassLoader classLoader;
|
||||
@Override
|
||||
public void setBeanClassLoader(ClassLoader classLoader) {
|
||||
System.out.println("类加载器:"+classLoader);
|
||||
this.classLoader= classLoader;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
|
||||
/**
|
||||
* 获取当前bean的beanFactory
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 19:32
|
||||
*/
|
||||
public class IBeanFactoryAware implements BeanFactoryAware {
|
||||
private BeanFactory beanFactory;
|
||||
@Override
|
||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||
System.out.println("当前beanFactory:"+beanFactory);
|
||||
this.beanFactory = beanFactory;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.beans.factory.BeanNameAware;
|
||||
|
||||
/**
|
||||
* 获取bean的名字
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 19:25
|
||||
*/
|
||||
public class IBeanNameAware implements BeanNameAware {
|
||||
|
||||
private String name;
|
||||
|
||||
@Override
|
||||
public void setBeanName(String name) {
|
||||
System.out.println("bean的名字:"+name);
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.context.EmbeddedValueResolverAware;
|
||||
import org.springframework.util.StringValueResolver;
|
||||
|
||||
/**
|
||||
* 说明描述
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 20:32
|
||||
*/
|
||||
public class IEmbeddedValueResolverAware implements EmbeddedValueResolverAware {
|
||||
|
||||
private StringValueResolver stringValueResolver;
|
||||
@Override
|
||||
public void setEmbeddedValueResolver(StringValueResolver resolver) {
|
||||
this.stringValueResolver = resolver;
|
||||
System.out.println("\n--------------------");
|
||||
System.out.println("EmbeddedValueResolverAware:"+resolver);
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.youlai.laboratory.spring.bean.aware;
|
||||
|
||||
import org.springframework.context.EnvironmentAware;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.util.EventListener;
|
||||
|
||||
/**
|
||||
* 获取当前环境
|
||||
*
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/20 0020 20:29
|
||||
*/
|
||||
|
||||
public class IEnvironmentAware implements EnvironmentAware {
|
||||
|
||||
private Environment environment;
|
||||
@Override
|
||||
public void setEnvironment(Environment environment) {
|
||||
this.environment = environment;
|
||||
System.out.println("\n--------------------");
|
||||
System.out.println("当前环境:"+environment);
|
||||
System.out.println("--------------------");
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import java.lang.reflect.Constructor;
|
||||
* SmartInstantiationAwareBeanPostProcessor是spring内部接口主要作用为
|
||||
* 预测Bean的最终类型
|
||||
* 推断构造函数
|
||||
* 提前曝光工厂对象,解决循环对象
|
||||
* 提前曝光工厂对象,解决循环依赖
|
||||
* @author <a href="mailto:2256222053@qq.com">zc</a>
|
||||
* @Date 2022/3/19 0019 19:28
|
||||
*/
|
||||
|
@ -1,8 +1,9 @@
|
||||
package com.youlai.laboratory.spring;
|
||||
|
||||
import com.youlai.laboratory.spring.bean.*;
|
||||
import com.youlai.laboratory.spring.bean.aware.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.beans.factory.xml.XmlBeanFactory;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
/**
|
||||
@ -26,8 +27,69 @@ public class BeanTests {
|
||||
System.out.println(car);
|
||||
}
|
||||
|
||||
void createBean(){
|
||||
new DefaultListableBeanFactory();
|
||||
/**
|
||||
* 容器开始和结束的回调接口
|
||||
* Lifecycle接口必须显示调用容器start方法,才能执行预先定义的回调
|
||||
* start的spring底层调用{@link org.springframework.context.support.DefaultLifecycleProcessor#doStart}
|
||||
* stop的spring底层调用{@link org.springframework.context.support.DefaultLifecycleProcessor#doStop}
|
||||
*/
|
||||
@Test
|
||||
void lifecycle(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ILifecycle.class);
|
||||
context.start();
|
||||
context.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 容器开始和结束的回调接口
|
||||
* {@link org.springframework.context.SmartLifecycle}继承了Lifecycle和Phased,
|
||||
* 相比Lifecycle多了自定义优先级功能和控制是否需要显示调用start
|
||||
*
|
||||
*/
|
||||
@Test
|
||||
void smartLifecycle(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ISmartLifecycle1.class, ISmartLifecycle2.class);
|
||||
context.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 先执行BeanNameAware,BeanClassLoaderAware,BeanFactoryAware三个aware,再执行其他aware
|
||||
* spring底层实现:{@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#invokeAwareMethods}
|
||||
*/
|
||||
@Test
|
||||
void aware(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IBeanNameAware.class, IBeanClassLoaderAware.class, IBeanFactoryAware.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理上面三个aware再执行其他aware
|
||||
*参考{@link org.springframework.context.support.ApplicationContextAwareProcessor#invokeAwareInterfaces}
|
||||
*/
|
||||
@Test
|
||||
void atherAware(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IApplicationContextAware.class, IApplicationStartupAware.class, IEnvironmentAware.class, IEmbeddedValueResolverAware.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行完aware接口后执行初始化方法
|
||||
* 先执行实现InitializingBean接口的afterPropertiesSet方法,后执行initMethod方法
|
||||
*/
|
||||
@Test
|
||||
void initializingBean(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IInitializingBean.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 初始化方法执行完之后执行初始化后方法,AOP在这个地方完成
|
||||
* 参考{@link org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#applyBeanPostProcessorsAfterInitialization}
|
||||
*/
|
||||
@Test
|
||||
void afterInitialization(){
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IInitializingBean.class,IBeanPostProcessor.class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user