反射生成的类无法进行自动注入的解决方案

news/2024/5/19 2:44:04 标签: 反射, 自动注入, spring

核心思想是无法自动注入就自己手动new
和单例模式写法一致

创建一个私有的属性

  private WaybillUtils waybillUtils;

提供一个

@Override
    public R check(List<WaybillImportVo> waybillVo) {
        this.waybillUtils = new WaybillUtilsImpl();
        //本类中的其他方法
        check();
        }

check(){
//这里就能使用该属性了
waybillUtils.xxxx();
}

这样就能用new出来的类了
用重载的方式写方便代码重用

但是这样就存在一个问题
手动new出来的类依然是脱离spring
这里new出来的类依然是不能自动注入里面的xxxService或xxxDao


那么正确的思路是什么呢?

**

springapplicationContext_40">使用spring的applicationContext,来获取你想要使用的类

**
比如这里我想用WaybillUtils,但是WaybillUtils是null,
就可以使用下面的代码

ApplicationContext applicationContext = SpringContextHolder.getApplicationContext();
        this.waybillUtils = applicationContext.getBean("waybillUtilsImpl", WaybillUtilsImpl.class);
       

需要注意的是spring自动创建的类名首字母是小写

另外想使用上面的这个方法,需要把下面这个类在自己项目中创建,并且要能被spring扫描到.(@Component注解),否则也是无效的

/**
 * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext.
 *
 */
@Component
public class SpringContextHolder implements ApplicationContextAware, DisposableBean {

	private static ApplicationContext applicationContext = null;

	private static Logger logger = LoggerFactory.getLogger(SpringContextHolder.class);

	/**
	 * 取得存储在静态变量中的ApplicationContext.
	 */
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	@SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}

	/**
	 * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型.
	 */
	public static <T> T getBean(Class<T> requiredType) {
		return applicationContext.getBean(requiredType);
	}

	/**
	 * 清除SpringContextHolder中的ApplicationContext为Null.
	 */
	public static void clearHolder() {
		logger.debug("清除SpringContextHolder中的ApplicationContext:" + applicationContext);
		applicationContext = null;
	}

	/**
	 * 实现ApplicationContextAware接口, 注入Context到静态变量中.
	 */
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) {
		logger.debug("注入ApplicationContext到SpringContextHolder:" + applicationContext);

		if (SpringContextHolder.applicationContext != null) {
			logger.warn("SpringContextHolder中的ApplicationContext被覆盖, 原有ApplicationContext为:"
					+ SpringContextHolder.applicationContext);
		}

		SpringContextHolder.applicationContext = applicationContext; //NOSONAR
	}

	/**
	 * 实现DisposableBean接口, 在Context关闭时清理静态变量.
	 */
	@Override
	public void destroy() throws Exception {
		SpringContextHolder.clearHolder();
	}
}

http://www.niftyadmin.cn/n/1343036.html

相关文章

centos 最小化安装+apache+php+mysql+gd+zend+phpmyadmin

把我这10天的笔记总结到一个文档里&#xff0c;这次centos5.1是采用最小化的安装&#xff0c;全部的软件都是采用编译安装&#xff0c;软件基本都是最新版本。整个编译的过程&#xff0c;我都重复了不下10次。应该是没有问题的&#xff0c;改天我再按照我自己的文档&#xff0c…

Python之Cubes框架使用

本文主要内容包含Cubes框架的介绍和简单使用。 一、 介绍和安装 Cubes是一个轻量级的Python框架和一套工具&#xff0c;用于开发报告和分析应用程序&#xff0c;在线分析处理&#xff08;OLAP&#xff09;&#xff0c;多维分析和聚合数据的浏览。它是Data Brewery的一部分。 官…

java实现顺序表的插入

package cn.eskyzdt.kaoyan.table;import java.util.Scanner; import java.util.regex.Pattern;/*** 顺序表的操作*/ public class Table {public static int[] arrays {10, 20, 30, 40, 50, 60, 70, 80};public static void main(String[] args) {System.out.println("输…

PS部分笔记

1、1英寸2.54厘米 2、户外喷绘&#xff1a;23-72像素/英寸&#xff0c;写真&#xff08;室内P的画&#xff09;&#xff1a; 72-150像素/英寸&#xff0c;屏幕&#xff1a;72或96像素/英寸&#xff0c;报纸&#xff1a;150像素/英寸&#xff0c;杂志&#xff1a;300像素/英寸&a…

不要用dubbo-spring-boot-starter-0.2.1

这个jar包的parent标签中的version写的是${revision} 不能这样写的 去用0.2.1-RELEASE 或者用0.2.0

修改session的生存时间

1.session回收//session的实效时间session.gc_maxlifetime 86400//垃圾回收的概率#概率是gc_probability/gc_divisorsession.gc_probability 1session.gc_divisor 100<?phpif(!isset($_SESSION[last_access])||(time()-$_SESSION[last_access])>60)$_SESSION[last_ac…

数组、数组的遍历、排序算法

int x[3] { 1,2,3 }; int x[3] { 1,2 };//未初始化的元素&#xff0c;默认0 int x[] { 1,2,3 };//省略下标&#xff0c;依据元素个数自动变更下标 int x[2][2] { 11,12,21,22 };//二维数组&#xff0c;效果同下 int x[2][2] { {11,12},{21,22} }; 数组的遍历 #include<…

潭州课堂25班:Ph201805201 django 项目 第八课 注册功能分析,图片验证码视图设计 (课堂笔记)...

1&#xff0c;用户名       是否已注册 2&#xff0c;手机号       是否被注册 3&#xff0c;图形验证码 4&#xff0c;短信验证码&#xff0c; 5&#xff0c;验证成功后&#xff0c;向后台提交数据&#xff1a;用户名&#xff0c;密码&#xff0c;手机号&…