# 普通类注入SpringBoot

  • 方法一:利用PostConstruct注解去处理
public class MinZhuAssessImpl implements IAssessService {
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    private static MinZhuAssessImpl MinZhuAssessImpl;

    @PostConstruct
    public void init() {
        MinZhuAssessImpl = this;
        MinZhuAssessImpl.platformTransactionManager = this.platformTransactionManager;
        // ...有更多的依赖就如此继续注入
    }

    @Override
    public Result<?> start(BusInstanceGroup busInstanceGroup) {
        DefaultTransactionDefinition definition = new DefaultTransactionDefinition();
        definition.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        // 重点:用静态方法去调用
        TransactionStatus status = MinZhuAssessImpl.platformTransactionManager.getTransaction(definition);
        // ...
    }
}
  • 方法二:使用工具类去注入
// 工具类
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;

    public SpringUtil() {
    }

    public void setApplicationContext(ApplicationContext arg0) throws BeansException {
        if (applicationContext == null) {
            applicationContext = arg0;
        }

    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setAppCtx(ApplicationContext webAppCtx) {
        if (webAppCtx != null) {
            applicationContext = webAppCtx;
        }
    }

    /**
     * 拿到ApplicationContext对象实例后就可以手动获取Bean的注入实例对象
     */
    public static <T> T getBean(Class<T> clazz) {
        return getApplicationContext().getBean(clazz);
    }

    public static <T> T getBean(String name, Class<T> clazz) throws ClassNotFoundException {
        return getApplicationContext().getBean(name, clazz);
    }

    public static final Object getBean(String beanName) {
        return getApplicationContext().getBean(beanName);
    }

    public static final Object getBean(String beanName, String className) throws ClassNotFoundException {
        Class clz = Class.forName(className);
        return getApplicationContext().getBean(beanName, clz.getClass());
    }

    public static boolean containsBean(String name) {
        return getApplicationContext().containsBean(name);
    }

    public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().isSingleton(name);
    }

    public static Class<?> getType(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().getType(name);
    }

    public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
        return getApplicationContext().getAliases(name);
    }
}


// 使用
IAssessService assessService = (IAssessService) SpringUtil.getBean("MinZhuAssessImpl");

# 集合去重

listByTaskProperties.getRecords().stream().filter(distinctByKey(p -> p.getName())).forEach(newList::add);

static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    //putIfAbsent方法添加键值对,如果map集合中没有该key对应的值,则直接添加,并返回null,如果已经存在对应的值,则依旧为原来的值。
    //如果返回null表示添加数据成功(不重复),不重复(null==null :TRUE)
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

# To Be Continued!😎

Last Updated: 9/26/2022, 4:36:50 PM