栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

解决静态utils里面注入mapper对象

1,utils类上面添加@Component注解

2,@Autowired注入需要使用的mapper对象,注意不是静态的

3,创建一个静态的这个utils的对象 private static Utils utils;

4,创建一个init方法,添加@PostConstruct注解,方法内容如下

@Component
public class Utils {
	
	@Autowired
	private  Mapper Mapper;
	@Autowired
	private Mapper Mapper;
	
	private static Utils Utils;
	
	@PostConstruct
	public void init(){
		Utils = this;
		Utils.Mapper = this.busiTabMapper;
		Utils.Mapper = tMapper;
	}
	
	public static void test(Integer id){
		try {
			List list =Utils.Mapper.selectById(id);
①,静态方法直接调用非静态方法和非静态属性是报错的,但是对象中的就不会去检查。所以这个类需要一个自己的对象layoutNumUtils。 ②,如果直接用这个new出的对象调用mapper会出现报null,因为这个mapper是spring控制的springbeen,由spring控制他的创建生命周期等。所以做了一个init方法,并且加上了@PostConstruct,表示在服务器启动的时候运行init。init的工作就是layoutNumUtils = this;把spring创建的springbeen赋给对象。 这样我就有对象,不会报静态调用的错,而且这个对象是springbeen,可以调用mapper这个springbeen。(要想调用springbeen只能本身也是springbeen)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/672608.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号