com.ideaaedi.commonspring.injector.ExtAutowiredInjector Maven / Gradle / Ivy
package com.ideaaedi.commonspring.injector;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.lang.NonNull;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 对普通bean(即:非spring-bean)的autowired注入支持
*
* 使用方式一:
* User user = new User();
* // 直接使用ExtAutowiredInjector.inject注入
* ExtAutowiredInjector.inject(user);
* user.sayHello();
* 使用方式二:
* // 直接继承ExtAutowiredInjector
* public class User extends ExtAutowiredInjector {
* User user = new User();
* user.sayHello();
*
*
* @author JustryDeng
* @since 1.0.0
*/
@Slf4j
public abstract class ExtAutowiredInjector {
public ExtAutowiredInjector() {
Object obj = applyAutowired();
ExtAutowiredInjector.inject(obj);
}
/**
* 对返回的object进行autowired注入处理
*
* @return 要进行注入处理的Object
*/
protected Object applyAutowired() {
return this;
}
/**
* 对目标对象中的被@Auwired标注了的字段进行注入
*
* @param obj
* 目标obj
*/
public static void inject(Object obj) {
if (obj == null) {
log.warn("obj is null, skip inject.");
return;
}
if (ExtAutowiredInjectorProcessor.applicationContext != null) {
ExtAutowiredInjectorProcessor.applicationContext.publishEvent(new AutowiredInjectEvent(obj));
} else {
ExtAutowiredInjectorProcessor.queue.add(obj);
}
}
/**
* Autowired注入支持事件
*
* @author JustryDeng
* @since 1.0.0
*/
public static class AutowiredInjectEvent extends ApplicationEvent {
@Getter
private final Object bean;
/**
* Create a new ApplicationEvent.
*
* @param source
* the object on which the event initially occurred (never {@code null})
*/
public AutowiredInjectEvent(Object source) {
super(source);
Objects.requireNonNull(source, "source cannot be null.");
this.bean = source;
}
}
/**
* {@link ExtAutowiredInjector}处理器
*
* @author JustryDeng
* @since 1.0.0
*/
@Slf4j
public static class ExtAutowiredInjectorProcessor implements SmartInitializingSingleton, ApplicationListener {
static ApplicationContext applicationContext;
static Queue